0

How is it possible to receive a time line plot with multiple time lines in the same plot like this:

df3 <- structure(list(id = c(6L, 6L, 2L, 3L, 6L, 7L, 3L, 5L, 7L, 
                            4L, 6L, 7L, 7L, 3L, 6L, 6L, 6L, 7L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 
                            7L, 6L, 6L, 7L, 6L, 7L, 6L, 3L, 5L, 6L), month = c("2011-01", 
                                                                                       "2011-02", "2011-04", "2011-04", "2011-04", "2011-04", "2011-05", 
                                                                                       "2011-05", "2011-05", "2011-06", "2011-06", "2011-06", "2011-07", 
                                                                                       "2011-08", "2011-08", "2011-09", "2011-10", "2011-11", "2012-03", 
                                                                                       "2012-04", "2012-05", "2012-05", "2012-08", "2012-09", "2012-09", 
                                                                                       "2012-09", "2012-10", "2012-11", "2012-11", "2012-12", "2012-12", 
                                                                                       "2013-01", "2013-02", "2013-02", "2013-02"), frq = c(1, 
                                                                                                                                                  1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 
                                                                                                                                                  1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 2)), row.names = c(NA, 35L
                                                                                                                                                  ), class = "data.frame")

Additionally how is it possible to add in the x legend the year evenif the plot frequency is per month? Also, in the group variable how is is possible two show exactly which is the variable and the color of the respective line?

What I tried is this

library(ggplot2)
ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()
Nathalie
  • 1,228
  • 7
  • 20
  • Does https://stackoverflow.com/q/10576095/3358272 resolve your question? – r2evans Jul 06 '20 at 16:12
  • ... and you probably want to set to a Date format: `df3$date <- as.Date(paste(df3$month, "01", sep="-"))` and use that as the x-variable. And to show discrete colours your `id` variable should be a `factor` – user20650 Jul 06 '20 at 16:15

2 Answers2

1

Do you mean something like this:

library(ggplot2)
df3$id <- as.factor(df3$id)
ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84
0

Function to read first (n) characters:

firstfew <- function(x, n){
x=as.character(x)
substr(x, 1, n)}

years=firstfew(df3$month,4)
df3$years=years

ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()+ scale_x_discrete(labels= years)

Facets view:

ggplot(df3,aes(x=month,y=frq,colour=id,group=id)) + geom_line()+theme(legend.position="top",axis.ticks.x=element_blank(),axis.title.x=element_blank(),axis.text.x=element_blank(),panel.spacing.x=unit(0, "lines"),panel.grid.major = element_blank())+facet_wrap(~years,strip.position = "bottom",scales = "free_x")