R - 图例中带有ggplot的多行消失了

时间:2018-05-29 22:05:46

标签: r ggplot2 graph

我对R和ggplot相对较新,我试图创建一个简单的折线图,其中包含多个变量的多行。我的数据框如下所示:

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

y.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)

y.police <- 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)

y.protester <- 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 <- data.frame(x, y.articles, y.police, y.protester)

我试图用随时间(x)的三条线(y.articles,y.police和y.protester)制作折线图。我还有两条水平线标记事件。这是我正在使用的代码:

temp = ggplot(data=data, aes(x=x, y=y.articles, group=1)) +
  geom_line(aes(y = y.articles)) + 
  geom_line(aes(y = y.protester)) +
  geom_line(aes(y = y.police)) + 
  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(axis.text.x = element_blank())

temp + theme(plot.title = element_text(hjust = 0.5, face="bold"), legend.position = "bottom", legend.title = element_blank())

我有几个问题。 (1)传说没有出现。 (2)我想使用线形图的形状(例如像一条圆圈的线条和另一条圆形线条的线条)。我不是想制作一个散点图,只是想改变线条的形状,使线条看起来不太相似。 (3)我已经压制了x轴刻度标记,但我希望显示一些日期(如果可能的话,2013年,2014年,2015年和2016年)。

这是很多东西,但任何指导都将非常感谢!

1 个答案:

答案 0 :(得分:0)

如果我理解正确,_source: lastmodifieddate: "2016-12-07T18:34:48.000+0000", .. .. fileType: "PDF" ... 方法可以解决问题。 我正在使用您的数据。

tidyverse

情节:

enter image description here