带有 geom_line 和 geom_ribbon 和 geom_point 的图例

巴甫洛

我已经搜索了两天的答案,但仍然找不到如何做到这一点。我在这里这里找到的最接近的案例但前者没有情节,后者没有答案。事不宜迟,如何为我的传奇加分?这是我的数据:

Year <- c(2003:2020)
TheData <- c(72.6, 72.7, 72.6, 72.5, 72.4, 72.1, 71.8, 71.7, 71.8, 72.3, 72.7,
              72.9, 73.1, 73.3, 73.8, 74.7, 75.7, 77.1)
Lower <- c(72.33316, 72.05961, 71.8218, 71.62303, 71.46657, 71.35567, 71.29362,
           71.28368, 71.32915, 71.43331, 71.59947, 71.83096, 72.13113, 72.50333,
           72.95092, 73.47728, 74.08581, 74.77989)
Upper <- c(73.46626, 73.24078, 73.05676, 72.91817, 72.82899, 72.79323, 72.81489,
           72.89794, 73.04639, 73.26418, 73.55528, 73.92363, 74.37315, 74.90775,
           75.53132, 76.24776, 77.06094, 77.97473)
Model <- c(72.89971, 72.65020, 72.43928, 72.27060, 72.14778, 72.07445, 72.05425,
           72.09081, 72.18777, 72.34874, 72.57738, 72.87730, 73.25214, 73.70554,
           74.24112, 74.86252, 75.57337, 76.37731)
MyDF <- data.frame(Year, TheData, Lower, Upper, Model) 

这是我的代码:

library("ggplot2")
ggplot(MyDF, aes(x = Year, y = TheData)) +
    geom_point(aes(y = TheData), size = 2.5) +
    geom_line(aes(x = Year, y = Model, color = "Model", fill = "Model")) +
    geom_ribbon(aes(ymin = Lower, ymax = Upper, x = Year,
                    fill = "Confidence Interval"), alpha = 0.15) +
    scale_colour_manual(
        name = "", values = c("Confidence Interval" = "transparent",
                             "Model" = "black")) +
    scale_fill_manual(
        name = "",  values = c("Confidence Interval" = "grey12",
                               "Model" = "transparent")) +
    theme(legend.position = "bottom")

这是我的阴谋。如何给图例加分?

斯蒂芬

如果你想得到一个图例,你必须在美学上进行映射,例如,你可以在shapeaes 上进行映射以获得你的点的图例:

library("ggplot2")

ggplot(MyDF, aes(x = Year, y = TheData)) +
  geom_point(aes(y = TheData, shape = "TheData"), size = 2.5) +
  geom_line(aes(x = Year, y = Model, color = "Model")) +
  geom_ribbon(aes(ymin = Lower, ymax = Upper, x = Year,
                  fill = "Confidence Interval"), alpha = 0.15) +
  scale_colour_manual(
    name = "", values = c("Confidence Interval" = "transparent",
                          "Model" = "black")) +
  scale_fill_manual(
    name = "",  values = c("Confidence Interval" = "grey12",
                           "Model" = "transparent")) +
  theme(legend.position = "bottom") +
  labs(shape = "")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带geom_line和geom_ribbon的图例

ggplot2中带有geom_line和geom_point的图例

ggplot:为什么为geom_ribbon和geom_line都打印图例?

如何为我的ggplot添加图例?我正在使用geom_line和geom_ribbon

ggplot2:合并geom_line,geom_point和geom_bar的图例

带有ggplot2和geom_point的图例

ggplot2-结合了geom_point和geom_line的图例

结合ggplot geom_point和geom_line图产生的冗余图例

使用geom_point()和geom_line()进行多个系列的ggplot中的图例错误

ggplot:如何将图例添加到由几个geom_ribbon()和geom_line()组成的绘图中?

带有geom_text_repel和geom_point的颜色图例

将 geom_line 和 geom_point 与 ggplotly 一起使用时的奇怪图例和工具提示文本

抖动geom_line和geom_point

添加geom_point和geom_line进行绘图

分别调整geom_point()和geom_line()

使用geom_line和geom_ribbon绘制两个均值和sd的df

在R中绘制geom_line和geom_ribbon时如何添加不连续点?

geom_ribbon设置坡度和截距

ggplot2:将geom_line和geom_point组合在一起的图形中的坐标图例

如何在具有多个geom_point图层的图中为图例设置样式和位置?

如何使geom_line和geom_point抖动相同的幅度?

ggplot:两组的geom_line和geom_point重叠顺序?

将gganimate与geom_point和geom_line一起使用

绘制 geom_line() 和 geom_point() - 不同长度的数据

使用带有因子颜色和手动添加线的 geom_line 向 ggplot 添加图例

如何将geom_bar,geom_point和geom_line合并为一个图形?

是否可以为geom_point和geom_segment添加图例?

使用来自不同数据集的geom_point和geom_smooth添加图例

使用geom_bar和geom_point控制ggplot中的图例