面板背景的自定义颜色:数据点被遮罩

时间:2019-07-10 09:13:40

标签: r ggplot2 plot colors background

我想制作一个带有自定义面板背景的情节,以三个不同颜色的区域作为背景(例如,黄色,金色和无颜色)。

我可以通过使用ggplot中的 geom_rect()来以所需的方式设置面板背景。

但是...背景色并不是真正的背景色。确实,颜色在我的数据点之前。这使该图有点怪异且难以阅读。 我还尝试使用 alpha = 0.2 设置透明度,但这不能解决问题。

以下是可重现的示例:

df <- data.frame(ID=seq(1,5,1),
                      trait=rep(c("A", "B", "C"), each=5),
                      mm=c(0.123, 0.89416, 0.4541, 0.233, 0.7794, 0.32554, 0.5554, 0.64122, 0.115, 0.5441, 0.3332, 0.1452, 0.5413, 0.418, 0.1852))

这是情节:

  geom_point(shape=21) + 
  scale_size_area(max_size=10, guide=guide_legend(title.position="top", nrow=4, title="%"))+
  scale_fill_manual(name=NULL,values=c("lightskyblue","limegreen","purple"))+
  geom_rect(data=NULL,aes(xmin=0,xmax=1.5,ymin=-Inf,ymax=Inf), fill="yellow", alpha=0.05)+
  geom_rect(data=NULL,aes(xmin=1.5,xmax=2.5,ymin=-Inf,ymax=Inf), fill="gold", alpha=0.05)

wrong plot enter image description here

如何强制geom_rect真正位于后面而我的数据点位于前面?

0 个答案:

没有答案