Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a Time Series in ggplot, with lines grouped by Year

I am trying to plot a time series with years each on different lines. Take this example:

library(ggplot2)
library(lubridate)
library(dplyr)

df = data.frame(dt = seq(as.Date("2015/01/01"), by = "day", length.out = 1000),
     num = c(sample(1:333), sample(333:665), sample(665:998)))

With this sample data, I then try to plot:

ggplot(df, aes(x= format(dt, format="%m-%d"), y=num, group = as.factor(year(dt)))) 
    + geom_line()

This returns a warning that geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?. Replacing color with group gives me something similar to what I want, but the x-axis has too many labels since its type is string.

TimeSeriesPlot

How would you make it so that the x-axis only displays the 1st day of each month here? Or is there a better way to plot a time series like this?

like image 400
Matsutake Avatar asked Nov 27 '25 19:11

Matsutake


1 Answers

I think it would be easiest if you just converted all the dates to the same year and use that for plotting the x-axis. Then you can customize the scale to get the labels you want. For example

same_year <- function(x) {
  year(x) <- 2000
  x
}

ggplot(df, aes(x=same_year(dt), y=num, color = as.factor(year(dt)))) + 
  geom_line()  + 
  scale_x_date(date_breaks = "1 month", date_labels="%b")

Which returns

enter image description here

like image 131
MrFlick Avatar answered Nov 30 '25 10:11

MrFlick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!