Plot
Thêm nền giấy
Gói ggpomological còn có tính năng thêm nền giấy vàng hoặc trắng.
Viết hàm
add_background <- function(gg, width = 800, height = 600, res = 100, bg_plot="overlay"){
# chuyen do thi sang nen trong suot
gg <- gg + theme(plot.background = element_rect(fill = "transparent", colour = NA))
# luu thanh file png
png(filename = "plot1.png", res = res,width = width, height = height, bg = "transparent")
print(gg)
dev.off()
# load file
gg_fig <- image_read("plot1.png")
# Load file background
exts <- c("background" = ".png", "overlay" = ".jpg")
bg_image <- system.file("images", paste0("pomological_",bg_plot,exts[bg_plot]),
package = "ggpomological")
pomo_bg <- image_read(bg_image) %>%
image_resize(paste0(width, "x", height, "!")) %>%
image_crop(paste0(width, "x", height))
# ghep do thi vao background
pomo_img <- image_composite(pomo_bg, gg_fig)
return(pomo_img)
}Nền vàng
Nền trắng
Ví dụ khác
gg <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(color = Species)) +
geom_smooth(aes(color = Species, fill = Species)) +
facet_wrap(~Species, ncol = 3, nrow = 1) +
scale_color_pomological() +
scale_fill_pomological() +
theme_pomological("Homemade Apple", 16)
add_background(gg, bg_plot = "background")gg <- ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill") +
scale_fill_pomological() +
theme_pomological("Homemade Apple", 16)
add_background(gg, bg_plot = "background")gg <- ggplot(ToothGrowth, aes(x = dose, y = len, fill = supp )) +
geom_boxplot(notch = TRUE, alpha = 0.6) +
scale_color_pomological() +
scale_fill_pomological() +
theme_pomological("Homemade Apple", 16)
add_background(gg, bg_plot = "background")