使用ggpubr::background_image 中的示例,这可以通过设置panel.background = element_blank() 和panel.ontop = TRUE 来实现。试试这个:
library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)
img.file <- system.file(file.path("images", "background-image.png"),
package = "ggpubr")
img <- png::readPNG(img.file)
# Plot with background image
ggplot(iris, aes(Species, Sepal.Length))+
background_image(img)+
geom_boxplot(aes(fill = Species), color = "white")+
theme(panel.background = element_blank(),
panel.ontop = TRUE)
由reprex package (v0.3.0) 于 2020-05-25 创建
编辑:另一种方法是使用geom_vline 和geom_hline 手动绘制网格线:
library(ggpubr)
#> Loading required package: ggplot2
library(ggplot2)
img.file <- system.file(file.path("images", "background-image.png"),
package = "ggpubr")
img <- png::readPNG(img.file)
# Plot with background image
ggplot(iris, aes(Species, Sepal.Length))+
background_image(img) +
geom_vline(aes(xintercept = Species), color = "white") +
geom_hline(yintercept = c(5, 6, 7, 8), color = "white") +
geom_boxplot(aes(fill = Species), color = "white")
由reprex package (v0.3.0) 于 2020 年 5 月 26 日创建