3

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?

user2554330
  • 37,248
  • 4
  • 43
  • 90
Matsutake
  • 87
  • 6
  • If I remove the `x=format(dt,format="%m-%d")` and just use `x=dt` I get the correct formatting. For further options - http://ggplot2.tidyverse.org/reference/scale_date.html – Jason Jan 16 '18 at 19:33

1 Answers1

2

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295