简体   繁体   English

带有 geom_line 和 geom_ribbon 和 geom_point 的图例

[英]Legend with geom_line and geom_ribbon and geom_point

I've been searching the answer for two days and still can't find how to do this.我已经搜索了两天的答案,但仍然找不到如何做到这一点。 The closest cases I found here and here .我在这里这里找到的最接近的案例。 But the former has no points on the plots and the latter has no answer.但前者没有情节,后者没有答案。 Without much ado, how to add points to my legend?事不宜迟,如何为我的传奇加分? This is my data:这是我的数据:

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) 

This is my code:这是我的代码:

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

This is my plot.这是我的阴谋。 如何给图例加分?

If you want to get a legend you have to map on an aesthetic, eg you could map on the shape aes to get a legend for your points too:如果你想得到一个图例,你必须在美学上进行映射,例如,你可以在shape aes 上进行映射,以获得你的点的图例:

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

If somebody is interested to move the legend to free space on the plot there is an obvious way to do so:如果有人有兴趣将图例移动到情节上的空闲空间,则有一种明显的方法:

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 = "") +
theme(legend.position = c(.4, .7))

But the legend appears stacked:但传说似乎堆积如山: 在此处输入图像描述 Adding + guides(color = guide_legend(nrow = 1)) does not work:添加+ guides(color = guide_legend(nrow = 1))不起作用: 在此处输入图像描述

My colleague have proposed to add legend.box = "horizontal" .我的同事建议添加legend.box = "horizontal" This code works:此代码有效:

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 = "") +
theme(legend.position = c(.4, .7), legend.box = "horizontal") +
guides(color = guide_legend(nrow = 1))

The plot looks like this:情节如下所示: 在此处输入图像描述

Still, I wonder why the legend appears in different boxes and how to put it together?不过,我想知道为什么图例出现在不同的框中,以及如何将它们放在一起?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM