简体   繁体   English

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

[英]Multiple lines with ggplot in R - legend disappearing

I am relatively new to R and ggplot, and I am trying to create a simple line graph with multiple lines from multiple variables over time. 我对R和ggplot相对较新,我试图创建一个简单的折线图,其中包含多个随时间变化的多条线。 My data frame looks like this: 我的数据框如下所示:

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)

I am trying to make a line graph with the three lines (y.articles, y.police, and y.protester) over time (x). 我正在尝试随着时间(x)用三条线(y.articles,y.police和y.protester)制作线图。 I also have two horizontal lines marking events. 我还有两条水平线标记事件。 Here is the code I am using: 这是我正在使用的代码:

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

I have a few issues. 我有几个问题。 (1) the legend is not showing up. (1)图例未显示。 (2) I would like to use shapes for the line graphs (eg like a line with circles for one and a line with squares for another). (2)我想对折线图使用形状(例如,一个带圆圈的线一个,另一个带正方形的线)。 I am not trying to make a scatter plot, just want to change the shape of the line so that the lines don't look too similar to one another. 我不是要绘制散点图,而只是想更改线条的形状,以使线条看起来不太相似。 (3) I have suppressed the x-axis tick mark labels but I would like to have a few dates show up (ie 2013, 2014, 2015, and 2016 if possible). (3)我取消了x轴刻度标记的标签,但希望显示一些日期(如果可能的话,即2013、2014、2015和2016)。

This is a lot of stuff but any guidance at all will be greatly appreciated! 这是很多东西,但是任何指导都将不胜感激!

If I understood you correctly, the tidyverse approach could do the trick. 如果我对您的理解正确,那么tidyverse方法就可以解决问题。 I'm using your data. 我正在使用您的数据。

require(tidyverse)

 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: 情节:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM