我想使用ggplot2在R中创建一个条形图,这样条形图就是透明的,允许背景图像可见,而图形的其余部分是不透明的并覆盖背景图像.
我可以将图像添加到背景中,如下所示,但我找不到只在条形图中显示背景图像的方法.基本上,我希望创建我在这里的反转.
library(ggplot2)
library(jpeg)
library(grid)
library(scales)
montage <- readJPEG("AcanthMontage.jpg")
mont <- rasterGrob(montage,
width = unit(1,"npc"),
height = unit(1,"npc"))
montplot <- ggplot(frequencyDF, aes(x=depth, y= perLiter)) +
annotation_custom(mont, -Inf, Inf, -Inf, Inf) +
scale_fill_continuous(guide = FALSE) +
geom_bar(stat = "identity", color="black", fill="white", alpha=0.5) +
coord_flip() +
scale_y_continuous(limits= c(0,1.25), expand = c(0, 0)) +
scale_x_continuous(limits= c(-1000,0), expand = c(0,0)) +
theme_bw() +
theme(text=element_text(size=16)) +
xlab("Depth (m)") +
ylab("Cells per Liter")
montplot
Run Code Online (Sandbox Code Playgroud)
