【问题标题】:Why is my legend not appearing next to the graph?为什么我的图例没有出现在图表旁边?
【发布时间】:2021-07-02 17:39:32
【问题描述】:

我在一个散点图中有两个不同的数据集,图例中的 prediction2020_bounds 需要为蓝色,图例中的 median2020 需要为红色。 这是我的代码:

  ggplot() + 
      
    # Points without corona
    geom_line(data=prediction2020_bounds, aes(x = Date, y = lwr), colour = "blue", linetype = "solid") +
    geom_line(data=prediction2020_bounds, aes(x = Date, y = upr), colour = "blue", linetype = "solid") +
    geom_ribbon(data=prediction2020_bounds, aes(x = Date, ymin = lwr, ymax = upr), alpha=.4) +
    labs(x = "Date", y = "Median Daily Price", title = "Daily Median Price", colour = "Legend Title\n") +
    scale_x_date(date_breaks="1 month", date_labels="%Y %m") +
    scale_colour_manual(labels = c("prediction2020_bounds", "median2020"), values = c(prediction2020_bounds, median2020)) +
    theme_bw() +
    geom_point(data=prediction2020_bounds, aes(x=Date, y=Price),fill="blue",
          colour="darkblue", size=1) +
    
    
    #Points with corona 2020 year
    geom_point(data=median2020, aes(x=Date, y=Price), fill="red",
            colour="red", size=1) 

【问题讨论】:

  • 这是因为您没有在图层的aes() 部分指定颜色。如果你使用aes(..., colour = "my legend item"),它会将颜色映射到一个比例,从而生成一个图例。

标签: r ggplot2 legend scatter-plot


【解决方案1】:

下次请考虑包含一些可重复的示例,以便其他人更容易支持您找到一个好的答案;)

要在ggplot2 中显示图例,您需要将mapping 参数内的colourfill 映射为aes(),而不是您在问题中所做的事情。下面的代码是实现你想要做的事情的一种方式。

我还根据个人喜好重新格式化代码,其中绘图的配置scaleslabelstheme 都在底部,而 geom 在顶部 @987654332 @ 命令。这只是个人喜好。

希望这能帮助你更好地理解ggplot2中的传说。

color_names <- c("prediction 2020 bounds", "median 2020")
color_values <- c("blue", "red")
names(color_values) <- color_names
ggplot() + 
  
  # Points without corona
  geom_line(data=prediction2020_bounds, aes(x = Date, y = lwr,
    # assign the colour to the same name as blue value of color_values
    colour = "prediction 2020 bounds"),  linetype = "solid") +
  geom_line(data=prediction2020_bounds, aes(x = Date, y = upr,
    # assign the colour to the same name as blue value of color_values
    colour = "prediction 2020 bounds"), linetype = "solid") +
  geom_ribbon(data=prediction2020_bounds, aes(x = Date, ymin = lwr, ymax = upr),
    alpha=.4) +
  geom_point(data=prediction2020_bounds, aes(x=Date, y=Price,
    # assign the fill to the same name as blue value of color_values
    fill="prediction 2020 bounds"), 
    colour="darkblue", size=1) +

  #Points with corona 2020 year
  geom_point(data=median2020, aes(x=Date, y=Price, 
    # assign the colour to the same name as red value of color_values
    fill="median 2020"),
    colour="median 2020", size=1) +
  
  # plot configuration scales, theme, etc...
  labs(x = "Date", y = "Median Daily Price",
    title = "Daily Median Price", colour = "Legend Title\n") +
  scale_x_date(date_breaks="1 month", date_labels="%Y %m") +
  scale_colour_manual(values = color_values) +
  scale_fill_manual(values = color_values) +
  theme_bw()

除此之外,您还可以在哪里使用theme(legend.position = [position] [position]可以

  • none: 不显示图例
  • left/right/botom/top:对应位置与主地块区域相关

我发现这篇文章内容丰富,解释透彻 https://aosmith.rbind.io/2018/07/19/manual-legends-ggplot2/

【讨论】:

  • 谢谢你的回答,它非常接近我想要的。但是,图例中的点都是红色而不是蓝色和红色。
  • 如果没有可重现的示例来确保我们在相同的东西上运行,真的很难做到。如果您可以提供一个最小的可重现示例作为下面的链接,那么其他人支持您会容易得多。 stackoverflow.com/questions/5963269/…
  • 是的,谢谢您的评论,我将通过可重现的示例分享一个新问题。
  • 请检查我的新问题,这个问题分解为我遇到的问题
猜你喜欢
  • 1970-01-01
  • 2022-08-18
  • 2011-09-08
  • 1970-01-01
  • 2015-08-04
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多