if (requireNamespace("ggplot2") && interactive()) {
## Create a new workbook
wb <- wb_workbook()
## Add a worksheet
wb$add_worksheet("Sheet 1", grid_lines = FALSE)
## create plot objects
require(ggplot2)
p1 <- ggplot(mtcars, aes(x = mpg, fill = as.factor(gear))) +
ggtitle("Distribution of Gas Mileage") +
geom_density(alpha = 0.5)
p2 <- ggplot(Orange, aes(x = age, y = circumference, color = Tree)) +
geom_point() + geom_line()
## Insert currently displayed plot to sheet 1, row 1, column 1
print(p1) # plot needs to be showing
wb$add_plot(1, width = 5, height = 3.5, file_type = "png", units = "in")
## Insert plot 2
print(p2)
wb$add_plot(1, dims = "J2", width = 16, height = 10, file_type = "png", units = "cm")
}
Run the code above in your browser using DataLab