标记图表上的特定点

时间:2018-06-09 04:52:42

标签: r graph line tidyverse

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

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())

这会产生以下情节:

Plot

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

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

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

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

1 个答案:

答案 0 :(得分: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的值。如果要对每个标签应用不同的值,它们也可以是向量。