I want to have both month
and day
in the x-axis of the time series plot when using facet
for years in ggplot2
. My MWE is below:
set.seed(12345)
Date <- seq(as.Date("2010/1/1"), as.Date("2014/1/1"), "week")
Y <- rnorm(n=length(Date), mean=100, sd=1)
df <- data.frame(Date, Y)
df$Year <- format(df$Date, "%Y")
df$Month <- format(df$Date, "%b")
df$Day <- format(df$Date, "%d")
df$MonthDay <- format(df$Date, "%d-%b")
p <- ggplot(data=df, mapping=aes(x=MonthDay, y=Y, shape=Year, color=Year)) + geom_point() +geom_line(aes(group = 1))
p <- p + facet_grid(facets = Year ~ ., margins = FALSE) + theme_bw()
print(p)
I tried to control the x-axis labels with the following command
p + scale_y_continuous() + scale_x_date(labels = date_format("%d-%b"))
But it throws the following error message.
Error: Invalid input: date_trans works with objects of class Date only