ggplot2产生两个图例,而不是一个

时间:2019-05-11 05:50:38

标签: r ggplot2 viridis

我正在尝试做一个简单的日志ggplot,显示随着时间(站点年龄)树木和灌木密度的变化。这些树种被分为本地树种/外来树种。

我还下载了viridis软件包,以便为图例+线+点+置信区间填充添加一种颜色。

问题是,当我使用viridis代码进行绘图时,我得到了两个不需要的图例。我不知道如何保留绿色传说,并删除其他传说。

我想提供输出的图片-但无法弄清楚如何将其添加到此问题模板中...

这是我使用的代码:

attach(data.df4)
base <- ggplot(data.df4, aes(age, total_trees))

base + 
  theme_classic(base_size = 10, base_family = "times") + 
  scale_y_log10() +
  geom_point(aes(color = status)) +
  geom_smooth(aes(color = status, fill = status), method = "lm", se = TRUE) +
  scale_colour_viridis(discrete = TRUE, option = "D")+
  scale_fill_viridis(discrete = TRUE, option = "D") +
  labs(title = "changes in planted canopy and subcanopy tree and shrub density over time", 
       x = "planting age", 
       y = "density (plot-level)") 

1 个答案:

答案 0 :(得分:0)

在没有看到数据或屏幕截图的情况下,很难知道需要更改什么。您可以通过2种不同的方式删除不需要的图例

  • 关闭填充图例ggplot() + guides(fill = FALSE)
  • 指定不要在geom_smooth(..., show.legend = FALSE)层内创建图例

本文可以向您展示如何发布一些示例数据: https://reprex.tidyverse.org/articles/articles/datapasta-reprex.html