【问题标题】:Removing the border of legend symbol去除图例符号的边框
【发布时间】:2018-03-19 22:29:54
【问题描述】:

我试图绘制一些预测数据与实际数据,类似于以下内容:

# Some random data
x <- seq(1: 10)
y_pred <- runif(10, min = -10, max = 10)
y_obs <- y_pred + rnorm(10)
# Faking a CI
Lo.95 <- y_pred - 1.96
Hi.95 <- y_pred + 1.96
my_df <- data.frame(x, y_pred, y_obs, Lo.95, Hi.95)

ggplot(my_df, aes(x = x, y = y_pred)) +
  geom_line(aes(colour = "Forecasted Data"), size = 1.2) +
  geom_point(aes(x = x, y = y_obs, colour = "Actual Data"), size = 3) +
  geom_ribbon(aes(ymin=Lo.95, ymax=Hi.95, x=x, linetype = NA,  colour = "Confidence Interval"), alpha=0.2) +
  theme_grey() +
  scale_colour_manual(
    values = c("gray30", "blue", "red"),
    guide = guide_legend(override.aes = list(
      border=c(NA, NA, NA), 
      fill=c("gray30", "white", "white"),
      linetype = c("blank", "blank", "solid"),
      shape = c(NA, 19, NA)))) 

剧情如下:

我对这个图的唯一问题是线的图例项符号周围的红色边框(即预测数据)。有什么办法可以在不破坏其余情节的情况下删除它?

【问题讨论】:

  • 我建议这里的根本问题是您将数据映射到颜色的方式。例如,aes(color = "Actual Data") 并不是真正正确的用法。通常我们使用aes() 来映射变量(列名,不加引号)。我会收集数据以创建一个列来指示 y 是否被观察或预测,然后使用 geom_point()aes() 来为该列着色。

标签: r ggplot2


【解决方案1】:

我认为geom_ribbon 是问题所在。如果我们从aes 中取出colorfill,一切看起来都很好

library(ggplot2)

# Some random data
x <- seq(1: 10)
y_pred <- runif(10, min = -10, max = 10)
y_obs <- y_pred + rnorm(10)
# Faking a CI
Lo.95 <- y_pred - 1.96
Hi.95 <- y_pred + 1.96
my_df <- data.frame(x, y_pred, y_obs, Lo.95, Hi.95)

m1 <- ggplot(my_df, aes(x = x, y = y_pred)) +
  geom_point(aes(x = x, y = y_obs, colour = "Actual"), size = 3) +
  geom_line(aes(colour = "Forecasted"), size = 1.2) +
  geom_ribbon(aes(x = x, ymin = Lo.95, ymax = Hi.95), 
              fill = "grey30", alpha = 0.2) +
  scale_color_manual("Legend", 
                     values = c("blue", "red"),
                     labels = c("Actual", "Forecasted")) +
  guides( color = guide_legend(
    order = 1,
    override.aes = list(
                        color = c("blue", "red"),
                        fill  = c("white", "white"),
                        linetype = c("blank", "solid"),
                        shape = c(19, NA)))) +
  theme_bw() +
  # remove legend key border color & background
  theme(legend.key = element_rect(colour = NA, fill = NA),
    legend.box.background = element_blank())
m1

当我们离开Confidence Interval 离开aes 时,我们不再拥有它的传奇。一种解决方法是创建一个不可见点并使用一个未使用的geom 手动创建图例键。这里我们可以使用size/shape(感谢answer

m2 <- m1 +
  geom_point(aes(x = x, y = y_obs, size = "Confidence Interval", shape = NA)) +
  guides(size = guide_legend(NULL, 
                             order = 2,
                             override.aes = list(shape = 15, 
                                                 color = "lightgrey",
                                                 size = 6))) +
  # Move legends closer to each other
  theme(legend.title = element_blank(),
        legend.justification = "center",
        legend.spacing.y = unit(0.05, "cm"),
        legend.margin = margin(0, 0, 0, 0),
        legend.box.margin = margin(0, 0, 0, 0)) 
m2

reprex package (v0.2.0) 于 2018 年 3 月 19 日创建。

【讨论】:

    【解决方案2】:

    解决此问题的更好方法是在geom_ribbon() 中指定show.legend = F 选项。这将消除第二步为置信区间添加和合并图例键的需要。这是稍作修改的代码。

      ggplot(my_dff, aes(x = x, y = y_pred)) +
        geom_line(aes(colour = "Forecasted Data"), size = 1) +
        geom_point(aes(x = x, y = y_obs, colour = "Actual Data"), size = 1) +
        geom_ribbon(aes(ymin=Lo.95, ymax=Hi.95, x=x, linetype = NA,  colour = "Confidence Interval"), alpha=0.2, show.legend = F) +
        theme_grey() +
        scale_colour_manual(
          values = c("blue", "gray30", "red"))+
          guides(color = guide_legend(
            override.aes = list(linetype = c(1, 1, 0)), 
            shape = c(1, NA, NA),
            reverse = T))
    

    My plot

    归功于https://stackoverflow.com/users/4282026/marblo 为他们对类似问题的回答。

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 2013-06-25
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-09-07
      • 2016-10-17
      • 2017-01-02
      相关资源
      最近更新 更多