【问题标题】:Show corresponding legend for second geom_point()显示第二个 geom_point() 的相应图例
【发布时间】:2021-07-03 04:01:34
【问题描述】:

我在同一个图中用geom_point 绘制了三次,但第二次和第三次geom_point 没有出现在图例中,我不明白为什么。我看了很多帖子,但找不到真正有效的解决方案。我的 2 个原始数据框不同,我更愿意保持这种状态而不加入它们。

所有图例项目上都出现十字(或颜色),或者图例中未提及第二个geom_point()。这是我当前的代码:

ggplot () +
  geom_point(data = data_1[data_1$River != "Roya",], aes(x = W_AC, y = BRI_mean_XS, shape = River, col = Type), size = 3) +
  geom_point(data = data_2, aes(x = W_m, y = BRI_norm, color = "Second geom_point "), shape = 4, size = 2.5) + 
  scale_shape_manual(values = c(15, 18, 17, 16)) +
  scale_colour_manual(values = c("blue4", "chocolate1", "darkcyan")) +
  geom_text(data = data_1[data_1$River != "Roya",], aes(x = W_AC, y = BRI_mean_XS, label = Year, vjust = -0.5, hjust = -0.5), show.legend = FALSE, size = 4) +
  scale_x_continuous(name = "aa") +
  scale_y_continuous(name = "bb") + 
  geom_point(data = data_1[data_1$River == "Roya",], aes(x = W_AC, y = BRI_mean_XS), shape = 16, col = "cornflowerblue", size = 3) +
  geom_text(data = data_1[data_1$River == "Roya",], aes(x = W_AC, y = BRI_mean_XS, label = Year, vjust = -0.5, hjust = -0.5), show.legend = F, size = 4) +
  theme_bw()

我还尝试在scale_*_manual 中指定颜色和形状,但图中未使用该形状。例如:scale_shape_manual(values = c(15, 18, 17, 16, 4))4 代表第二个 geom_point。

正如我在其他帖子中所读到的,当在 scale_*_manual 中指定值时,它应该出现在图例的后面,但这里似乎混淆了。是不是不能在Type 部分或River 部分显示蓝十字?第三个geom_point()(Roya)也一样,可以在River部分画一个蓝色圆圈吗?

这是我的 2 个数据框的输入:

数据_1:

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", "Witness", "Under restoration",  "Under restoration", "Target", "Under restoration", "Target",  "Target", "Under restoration", "Under restoration", "Under restoration",  "Target", "Target", "Under restoration"), Year = c(2017, 2017,  2012, 2012, 2018, 2005, 2011, 2011, 2018, 2018, 2012, 2018, 2005,  2013, 2005, 2013, 2012), BRI_mean_XS = c(0.0028, 0.0017, 0.0033, 
0.0018, 0.009, 0.0097, 0.0182, 0.0026, 0.0032, 0.0035, 0.0053, 
0.0016, 0.005, 0.0041, 0.0058, 0.0082, 0.0037), W_AC = c(296.522411128284, 
411.01376146789, 263.944186046512, 363.288990825688, 88.6420798065296, 
155.868223887175, 41.4430051813472, 138.772532188841, 105.525906735751, 
108.848484848485, 144.78431372549, 314.685764654033, 29.8791208791209, 
52.8, 33.6949152542373, 34.4661016949153, 149.838539375275)), row.names = c(NA, 
-17L), class = c("tbl_df", "tbl", "data.frame"))

数据_2:

structure(list(SITE = c("AIGUES-C", "BUECH-AV", "ARRONDINE",  "ARVAN", "ASSE-BLIEUX", "ASSE-BLIEUX", "ASSE-AV", "ASSE-AM",  "ASSE-AM", "BES-BLEONE", "BES-BLEONE", "BES-DROME", "BES-DROME",  "BLEONE-AM", "BLEONE-AV", "BUECH-AV", "BUECH-AV", "DRANSE-MORZINE",  "DRANSE-MORZINE", "DROME-RECOUBEAU", "DROME-PONTQUART", "DROME-PONTQUART",  "EYGUES-R", "FIER", "GIFFRE", "JABRON", "PETIT-BUECH", "PETIT-BUECH",  "ROUBION", "SASSE", "SASSE", "SASSE", "SEVERAISSE", "SEVERAISSE",  "UBAYE", "VANCON", "VAR", "VENEON", "VERDON", "DRAC LIDAR1",  "DROME LIDAR1", "DROME LIDAR2", "DROME LIDAR3"), W_m = c(180, 
333.593594745271, 56, 78, 102, 58.8, 171.9, 202, 170, 132, 148,  76, 98, 99.9, 171.3, 214, 125, 63, 64, 137, 181, 39, 66, 91,  220, 120, 90, 103, 114, 182, 123, 139, 152, 38, 144, 58, 183,  193, 193,
103.3287640678, 170.959110791, 168.800988262, 133.13237116 ), BRI_norm = c(0.00238417932933424, 0.0019586918252209, 0.00592873427970417, 
0.00496998572378169, 0.00257768461027533, 0.00594823420965329, 
0.00196839869925125, 0.00110647254938539, 0.00129282468469599, 
0.00255415705319677, 0.00189636185539575, 0.00272918083912772, 
0.00362898036424863, 0.00240398604859984, 0.00158558538418367, 
0.00179786294819712, 0.0024557284827885, 0.00910376733031885, 
0.00534776094775883, 0.00392598083437978, 0.00240750522409946, 
0.00514878385829154, 0.00254279813562245, 0.00563806058644668, 
0.00119537585167636, 0.00458318564753459, 0.00595336684669024, 
0.00487895959784154, 0.00292014352039067, 0.00238157929363119, 
0.0025305005154242, 0.00243334672392244, 0.00211201916980846, 
0.007410716639085, 0.0035528851408779, 0.00991898865707844, 0.00350148630982832, 
0.00181290043814611, 0.00277563808425691, 0.00766507891511477, 
0.000968375521552066, 0.000999909065815609, 0.00107669687622853 )), row.names = c(NA, -43L), class = c("tbl_df", "tbl", "data.frame" ))

【问题讨论】:

    标签: r ggplot2 geom-point


    【解决方案1】:

    我真的不是以下方法的忠实拥护者(使用适当的映射来解决这个问题会更惯用)但是您可以通过覆盖 River 中的 color / shape 美学和Type你喜欢的传说:

    1. 通过将shape 美学移动到aes 中并从River 映射来修改您的第三个点图层:
    aes(x = W_AC, y = BRI_mean_XS, shape = River)
    
    1. 调整形状比例,使Roya 用圆圈 (16) 表示:
    scale_shape_manual(values = c(15, 18, 17, 16, 16))
    
    1. 使用guides() 覆盖图例中的美学:
     + guides(
        color = guide_legend(
            override.aes = list(shape = c(4, 16, 16))
        ),
        shape = guide_legend(
            override.aes = list(color = c(rep("black", 3), "cornflowerblue", "black"))
        )
      )
    

    结果:

    【讨论】:

      【解决方案2】:

      我希望这是向前迈出的有益一步,但不能完全确定它是否完成了您正在寻找的情节。从本质上讲,您应该采取两个进一步的步骤来获得图例中的所有内容并按顺序排列:

      • 您想出现在图例中的任何内容都应在每个aes() 调用中为每个geom_point()
      • scale_*_manual() 调用中使用breaks = 参数可以同时指定应显示的所有内容及其应显示的顺序。

      与这两者一起工作,这可能是您正在寻找的开始:

      library(ggplot2)
      
      # the parts of the plot I haven't changed (separated for clarity)
      
      p_base <- ggplot () +
        geom_text(
          data = data_1[data_1$River != "Roya", ],
          aes(
            x = W_AC,
            y = BRI_mean_XS,
            label = Year,
            vjust = -0.5,
            hjust = -0.5
          ),
          show.legend = FALSE,
          size = 4
        ) +
        scale_x_continuous(name = "aa") +
        scale_y_continuous(name = "bb") +
        geom_text(
          data = data_1[data_1$River == "Roya", ],
          aes(
            x = W_AC,
            y = BRI_mean_XS,
            label = Year,
            vjust = -0.5,
            hjust = -0.5
          ),
          show.legend = F,
          size = 4
        ) +
        theme_bw()
      
      # Parts I've changed
      
      p_base + geom_point(
        data = data_1[data_1$River != "Roya",],
        aes(
          x = W_AC,
          y = BRI_mean_XS,
          shape = River,
          col = Type
        ),
        size = 3
      ) +
        geom_point(
          data = data_2,
          aes(
            x = W_m,
            y = BRI_norm,
            shape = "Second geom_point",
            color = "Second geom_point"
          ),
          size = 2.5
        ) +
        scale_shape_manual(
          breaks = c("Drac", "Durance", "Mareta", "Roya", "Var", "Second geom_point"),
          values = c(15, 18, 17, 16, 10, 4)
        ) +
        scale_colour_manual(
          breaks = c("Target", "Under restoration", "Second geom_point", "Roya"),
          values = c("blue4", "chocolate1", "darkcyan", "cornflowerblue")
        ) +
        geom_point(
          data = data_1[data_1$River == "Roya",],
          aes(
            x = W_AC,
            y = BRI_mean_XS,
            shape = "Roya",
            col = "Roya"
          ),
          size = 3
        )
      

      reprex package (v2.0.0) 于 2021-04-07 创建

      【讨论】:

      • 另外,如果真的需要将图例中的 Roya 设置为矢车菊蓝色圆圈,可以使用 guides() 函数实现,描述为 here。不过感觉有点乱。
      猜你喜欢
      • 2018-03-29
      • 2013-10-10
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2021-07-07
      • 2017-05-23
      相关资源
      最近更新 更多