需要添加图例并固定ggplot中的轴 [英] Need to add legend and fix axis in ggplot

查看:50
本文介绍了需要添加图例并固定ggplot中的轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ggplot的新手,我试图弄清楚如何在图形中添加图例并重新标记x轴.我已经附上了绘图代码和结果图.我想添加一个图例,该图例说明了蓝线以及绿点和红点.我还希望x轴上的年份显示为2018、2019,...,2020,而不是2017.5、2010.0,...,2020.0.我在在线文档中找不到解决方案.感谢您的帮助.

I'm new to ggplot and I'm trying to figure out how to add a legend to a graph and re-label the x-axis. I've enclosed the plotting code and resulting graph . I would like to add a legend that explains what the blue line and the green and red dots are. I would also like the years on the x-axis to appear as 2018, 2019, ... , 2020 instead of 2017.5, 2010.0, ..., 2020.0. I can't find a solution in the online documentation. Thanks for your help.

  ggplot(data = annual_rate_preds) + 
    geom_point(mapping = aes(x = year, y = predicted), color = 'green') +
    geom_line(mapping = aes(x = year, y = observed), color = 'blue') + 
    geom_point(data = backfit_rate_preds, mapping = aes(x = target_year, y = rate_pred), 
               shape = 18, color = 'red', size = 2) +
    theme(plot.title = element_text(size = 10))

推荐答案

使用一些随机示例数据,可以像这样实现:

Using some random example data this could be achieved like so:

  1. 使用 scale_x_continuous(breaks = scales :: pretty_breaks())可以得到漂亮的x轴中断和标签
  2. 要获得传奇,您必须映射美学,即在aes()中移动 color .然后可以通过 scale_color_manual
  3. 设置颜色值
  4. 可以通过 labs()
  5. 设置轴,图例等的标签
  6. 最棘手的部分是正确设置图例.为此,我利用 guides guide_legend 调整图例,以便对于 observed 仅显示实线,而对于其他类别仅出现点(形状16).
  1. Using scale_x_continuous(breaks = scales::pretty_breaks()) gives pretty x-axis breaks and labels
  2. To get a legend you have to map on aesthetics, i.e. move color inside aes(). The color values can then be set via scale_color_manual
  3. Labels for the axes, legend, ... can be set via labs()
  4. Most tricky part is to get the legend right. To this end I make use of guides and guide_legend to adjust the legend such that for observed only a solid line is shown while for the other categories only points (shape 16) show up.

library(ggplot2)

set.seed(42)
annual_rate_preds <- data.frame(
  predicted = runif(13, -.1, .1),
  observed = runif(13, -.1, .1),
  year = 2008:2020
)

backfit_rate_preds<- data.frame(
  rate_pred = runif(13, -.1, .1),
  target_year = 2008:2020
)

ggplot(data = annual_rate_preds) + 
  geom_point(mapping = aes(x = year, y = predicted, color = 'predicted')) +
  geom_line(mapping = aes(x = year, y = observed, color = 'observed')) + 
  geom_point(data = backfit_rate_preds, mapping = aes(x = target_year, y = rate_pred, color = 'rate_pred'), 
             shape = 18, size = 2) +
  scale_x_continuous(breaks = scales::pretty_breaks()) +
  scale_color_manual(values = c(predicted = "green", observed = "blue", rate_pred = "red")) +
  theme(plot.title = element_text(size = 10)) +
  guides(color = guide_legend(override.aes = list(linetype = c("solid", "blank", "blank"), shape = c(NA, 16, 16)))) +
  labs(x = "Year", y = NULL, color = NULL)

这篇关于需要添加图例并固定ggplot中的轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆