0

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

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

在此处输入图像描述

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)) 

这是dputdf1和df2的:

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"))
4

1 回答 1

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 包于 2021-04-12 创建 (v2.0.0 )

于 2021-04-12T13:12:04.170 回答