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,188
  • 3
  • 19

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
  • 38,442
  • 13
  • 38
  • 79
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")