I have a dataset where the x-axis is a date, but it is only mm-dd (no year). I am using year as a group variable as I am trying to show a YOY change on the same plot. The x-axis labeling is too crowded and I'd like to limit the tick mark labels so that not every date is shown. This could be every other day, every third day, one day a week -- any of these would work.
I have tried a few solutions but cannot get them to work, I'm assuming because my x-axis is not a Date, but a character. (Previous to arriving at this mm-dd solution for the x-axis, I tried plotting the x-axis with a yyyy-mm-dd Date format, but was unsuccessful in figuring out how to get ggplot2 to ignore the "yyyy" part.)
An example:
myDF <- data.frame(
myDate = format(seq(as.Date("2014-02-01"),
length=28, by="1 day"), "%m-%d"),
myVar = sample(100,28),
myGroup = sample(2,28,TRUE)
)
head(myDF)
myDate myVar myGroup
02-01 87 1
02-02 34 1
02-03 48 2
02-04 59 1
02-05 98 1
02-06 18 2
ggplot(myDF, aes(myDate, myVar, group=myGroup, color=as.factor(myGroup))) +
geom_line()
I have tried:
ggplot(myDF, aes(myDate, myVar, group=myGroup, color=as.factor(myGroup))) +
geom_line() + scale_x_discrete(breaks = c(1,10,20))
This appears to confuse ggplot since the labels disappear completely. (Same result with a seq() attempt.)
I have also tried:
ggplot(myDF, aes(myDate, myVar, group=myGroup, color=as.factor(myGroup))) +
geom_line() + scale_x_date(breaks = "1 week")
This throws an error re: myDate not being a Date.
I've already switched the format of the tick labels to be vertical, but it is still too crowded on the plot.
Any tips would be very much appreciated. Thanks!
as.data.frame(cbind(.))
error yet again.cbind
coerces everything to character. And if you want Dates, then don't coerce to character withformat
. – IRTFM?scale_x_date
, a function which you used, then you would have found several examples on how to "control the format of the labels, and the frequency of [...] tickmarks". – Henrik