【问题标题】:Add second geom_layer on only one faceted plot with corresponding legend仅在一个带有相应图例的多面图上添加第二个 geom_layer
【发布时间】:2021-07-07 10:42:27
【问题描述】:

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

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

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, aes(x = Date_plot, y = W_norm, shape = River), colour = "cornflowerblue", size = 3, inherit.aes = F) +
  facet_wrap(vars(River)) 

这里是df1和df2的dput

structure(list(River = c("Durance", "Durance", "Durance", "Durance", 
"Roya", "Var", "Drac", "Drac", "Drac", "Drac", "Var", "Var", 
"Mareta", "Mareta", "Mareta", "Mareta", "Var"), Type = c("Under restoration", 
"Target", "Under restoration", "Target", "Under restoration", 
"Under restoration", "Under restoration", "Target", "Under restoration", 
"Target", "Target", "Under restoration", "Under restoration", 
"Under restoration", "Target", "Target", "Under restoration"), 
    Date_plot = structure(c(17167, 17167, 15340, 15340, 17532, 
    12784, 14975, 14975, 17532, 17532, 15340, 17532, 12784, 15706, 
    12784, 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, class = "Date"), 
    W_norm = 5.0938), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame"))

【问题讨论】:

  • 河流为什么需要有单独的形状符号;河流是通过刻面面板和刻面条中的文本来区分的?
  • @Peter 是的,这是真的,我从来没有这样想过!我想这会更容易,但我仍然需要在图例中添加一个蓝色圆圈,以显示我认为 Var 图中河流的差异
  • dput(df2) Type = "Under restoration" 和计算出的df2 之间似乎存在Type 值不匹配,其中Type = "Witness"。能否请您解释或更正?
  • 抱歉已更正,我更改了第一个数据帧 dput 的 dput
  • 您是否有理由需要将 Var 河的 df2 点设为蓝色而不标识其 Type 名称:“恢复中”。这似乎不一致。所有其他点要么是“恢复中”,要么是“目标”。这不是 ggplot 问题,但可以帮助我理解图表试图显示的内容。根据其他点的逻辑,瓦尔河的df2 点应该是绿色的。

标签: r dplyr facet-wrap geom-point


【解决方案1】:

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

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-04-12 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多