微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅在一个带有相应图例的多面图上添加第二个 geom_layer

如何解决仅在一个带有相应图例的多面图上添加第二个 geom_layer

我正在创建一个带有第一个数据框的分面图,并向其中一个面板添加第二个 geom_point。我的问题是我想显示添加点的相应图例。

为了在我想要的面板上绘制第二个 geom_point,我创建了一个具有相应值的新 data frame,并修改River 列以在正确的面板上绘制新的 geom_point但是这个传说是不正确的。我想在河流部分有一个蓝色圆圈。感谢回复的人,我在 another post 上学到了一种处理传奇的新方法,但在这里它不起作用,因为这个情节是分面的。

enter image description here

df2        <- as_tibble(df1[5,])
df2$River = "Var"

ggplot(data = df1[df1$River != "Roya",],aes(x = Date_plot,y = W_norm,shape = River,col = Type)) +  
  geom_point(size = 3) + 
  scale_shape_manual(values = c(15,18,17,16,16)) +
  scale_colour_manual(values = c("chocolate1","darkcyan"),guide = guide_legend(order = 2)) +  
  scale_y_continous("W*") +
  scale_x_date("Years") + 
  geom_point(data = df2,shape = River),colour = "cornflowerblue",size = 3,inherit.aes = F) +
  facet_wrap(vars(River)) 

这里是 df1 和 df2 的 dput

structure(list(River = c("Durance","Durance","Roya","Var","Drac","Mareta","Var"),Type = c("Under restoration","Target","Under restoration","Under restoration"),Date_plot = structure(c(17167,17167,15340,17532,12784,14975,15706,15340),class = "Date"),W_norm = c(5.7321,7.9454,5.1023,7.0228,5.0938,4.7277,2.7783,9.303,7.0742,7.297,10.2625,9.5448,2.83,5.0009,3.1914,3.2644,4.5448
    )),row.names = c(NA,-17L),class = c("tbl_df","tbl","data.frame"
))

structure(list(River = "Var",Type = "Under restoration",Date_plot = structure(17532,W_norm = 5.0938),-1L),"data.frame"))

解决方法

虽然这不能回答编码问题,但它可能是可视化的解决方案。

library(ggplot2)

df1$River[5] = "Var"
df1$Type[5] = "Roya,under restoration"

ggplot(data = df1,aes(x = Date_plot,y = W_norm,col = Type)) +  
  geom_point(size = 3) + 
  scale_colour_manual(values = c("chocolate1","darkcyan","cornflowerblue")) +  
  labs(y = "W*",x = "Years") + 
  facet_wrap(~River) 

reprex package (v2.0.0) 于 2021 年 4 月 12 日创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。