ggplot time series plotting: group by dates

前端 未结 1 388
日久生厌
日久生厌 2021-01-20 06:01

I would like to plot several time series on the same panel graph, instead of in separate panels. I took the below R code from another stackoverflow post.

Please not

相关标签:
1条回答
  • 2021-01-20 06:42

    Since the data file has different days for each group's time, one way to get all the groups onto the same plot is to just create a new variable, giving all groups the same "dummy" date but using the actual times collected.

    experiment <- data.frame(Time, Value, Group)  #creates a data frame
    experiment$hms <- as.POSIXct(paste("2015-01-01", substr(experiment$Time, 12, 19)))  # pastes dummy date 2015-01-01 onto the HMS of Time
    

    Now that you have the times with all the same date, you then can plot them easily.

    experiment$Grouping <- as.factor(experiment$Group)  # gglot needed Group to be a factor, to give the lines color according to Group
    ggplot(experiment, aes(x=hms, y=Value, color=Grouping)) + geom_line(size=2)
    

    Below is the resulting image (you can change/modify the basic plot as you see fit):

    0 讨论(0)
提交回复
热议问题