2

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. 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). 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. (2) I would like to use shapes for the line graphs (e.g. like a line with circles for one and a line with squares for another). 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 (i.e. 2013, 2014, 2015, and 2016 if possible).

This is a lot of stuff but any guidance at all will be greatly appreciated!

maneesh
  • 23
  • 3
  • For the question of a legend, see [this question](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot). – aosmith May 29 '18 at 22:10
  • What are the dates where you need the `geom_vline`s? Those `xintercept` values you have here won't show up once you've converted the `x` values to proper dates. – camille May 29 '18 at 23:03
  • I want vertical lines at July, 2013 and August, 2014. I was just playing around with this but I'm not sure how to get the lines to show up. – maneesh May 30 '18 at 21:16

1 Answers1

0

If I understood you correctly, the tidyverse approach could do the trick. 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:

enter image description here

DJV
  • 4,743
  • 3
  • 19
  • 34
  • This worked great, thank you for the help! I've been meaning to learn how to use tidyverse and this gives me some more incentive to do so. – maneesh May 30 '18 at 00:32