【问题标题】:Labeling specific points on the graph在图表上标记特定点
【发布时间】:2018-06-09 04:52:42
【问题描述】:

我有一个按月显示数据的折线图。我的数据框如下所示:

x <- c("2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06", "2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12",
       "2014-01", "2014-02", "2014-03", "2014-04", "2014-05", "2014-06", "2014-07", "2014-08", "2014-09", "2014-10", "2014-11", "2014-12",
       "2015-01", "2015-02", "2015-03", "2015-04", "2015-05", "2015-06", "2015-07", "2015-08", "2015-09", "2015-10", "2015-11", "2015-12",
       "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11", "2016-12")

Articles <- c(0, 1, 1, 2, 0, 0, 9, 6, 1, 0, 0, 1,
                3, 0, 0, 0, 4, 1, 7, 106, 19, 16, 57, 115,
                26, 20, 33, 52, 45, 36, 32, 23, 21, 38, 17, 18,   
                6, 10, 14, 6, 17, 5, 34, 6, 11, 11, 2, 2)

PoliceFrame <- c(0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0,
              0, 0, 0, 0, 0, 0, 1, 29, 9, 2, 17, 46,
              13, 9, 14, 10, 13, 18, 6, 8, 7, 12, 1, 6,
              1, 2, 3, 1, 2, 2, 22, 2, 2, 7, 2, 1)

ProtesterFrame <- c(0, 0, 0, 2, 0, 0, 1, 2, 1, 0, 0, 1,
                 3, 0, 0, 0, 1, 0, 6, 51, 6, 11, 18, 38,
                 8, 9, 11, 32, 22, 10, 15, 10, 10, 19, 9, 5,
                 3, 6, 6, 2, 8, 3, 8, 3, 8, 4, 0, 1)

我使用以下代码创建图表:

data %>% 
  gather(subject, value, -x) %>% 
  mutate(x = as.Date(paste(x,"-01",sep="")))  %>% 
  ggplot(aes(x=x, y=value,
             group=subject, color = subject, shape = subject)) +
  geom_line() +
  geom_point() +
  labs(title="Media Attention",x="", y = "Media Articles")+
  geom_vline(xintercept=19, linetype = "longdash") +
  geom_vline(xintercept=6, linetype = "longdash") +
  scale_linetype_manual(values = c(1,2,3)) +
  theme(plot.title = element_text(hjust = 0.5, face="bold"), 
        legend.position = "bottom", legend.title = element_blank())

这会产生以下情节:

我想在这个情节中添加两件事:

(1) 我想在 2013 年 7 月和 2014 年 8 月添加两条垂直虚线。

(2) 我只想将标签添加到红色(文章)峰值所在的位置。例如,2014 年 8 月有一个大的峰值。我想在 x 轴或绘图本身(红点所在的位置)上标记它。所以基本上我会在 2014 年 8 月贴标签; 2014 年 12 月; 2015 年 4 月; 2015 年 10 月;和 2016 年 7 月。

任何关于如何完成这些任务的指导将不胜感激!

【问题讨论】:

  • 检查geom_text,如heregeom_vline所示的垂直线here

标签: r graph line tidyverse


【解决方案1】:

第一个是geom_vline,第二个是geom_text

# separate the data manipulation from the plots so we can reuse it
data <- data %>%
  gather(subject, value, -x) %>% 
  mutate(x = as.Date(paste(x,"-01",sep="")))  

# your original plot call
p <- data %>% 
  ggplot(aes(x=x, y=value,
    group=subject, color = subject, shape = subject)) +
  geom_line() +
  geom_point() +
  labs(title="Media Attention",x="", y = "Media Articles")+
  geom_vline(xintercept=19, linetype = "longdash") +
  geom_vline(xintercept=6, linetype = "longdash") +
  scale_linetype_manual(values = c(1,2,3)) +
  theme(plot.title = element_text(hjust = 0.5, face="bold"), 
    legend.position = "bottom", legend.title = element_blank())

# the new stuff:
p + 
  geom_vline(xintercept = as.Date(c("2013-07-01", "2014-08-01")), linetype = "dashed") + 
  geom_text(data = data[data$subject == 'Articles' & data$value > 60,], mapping = aes(label = x), hjust = 1, nudge_x = -10)

随意使用hjust(0 = 左对齐,1 = 右对齐,0.5 = 居中)和nudge_x 的值。如果您想对每个标签应用不同的值,它们也可以是向量。

【讨论】:

  • 谢谢,梅丽莎!文本标签很好。我有两个问题我仍然遇到。垂直线不显示 - 我收到以下错误:“Ops.Date((x - from[1]), diff(from)) 中的错误:/未为“日期”对象定义”。另外,我也希望标记更多的尖峰。所有的峰值都大于 30 篇文章。问题是我不想标记其他超过 30 的点。有没有办法只标注几个特定的​​点?
  • 关于第一个问题 - 确保您使用的是最新版本的 ggplot2。如果这没有帮助,您可能需要从 github 安装该版本。其次,根据需要设置过滤器。我使用了data$subject == 'Articles' &amp; data$value &gt; 60,但你可以使用任何你喜欢的标准。
猜你喜欢
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多