r - rotating axis labels in date format -
does know how rotate axis ticks in date format ggplot2? want labels "date-month" (ex. "1985-5") 45° angle on x axis.
data sample:
station date ptot 1980-02 16 1980-03 19 1980-04 40 1980-05 48 1980-06 na 1980-07 18.5 1980-08 24.6 b 1980-07 50.8 b 1980-08 28.9 b 1980-09 32.9 b 1980-10 47.9 b 1980-11 16.3
i tried this:
library(ggplot2) library(scales) plot <- ggplot(data=na.omit(data), aes(x=date, y=ptot, group=station))+ geom_line()+ facet_grid(station~.)+ scale_x_date(breaks = "year", labels=date_format("%y-%m"))+ xlab("year")+ ylab("prec (mm)")+ labs(colour = "station")+ theme(axis.text.x = element_text(angle = 45, hjust = 1))+ theme_bw() plot
but doesn't work.
thanks!
first, should make column date
date. not have day provided, should add example 01 each date , convert them.
data$date<-as.date(paste(data$date,"-01",sep=""),format="%y-%m-%d")
to correct placement of labels under x axis should set not angle=
hjust=1
ensure end of label placed under tick mark. theme_bw()
should placed before theme specification of axis texts.
ggplot(data=na.omit(data), aes(x=date, y=ptot, group=station))+ geom_line()+ facet_grid(station~.)+ scale_x_date(breaks = "month", labels=date_format("%y-%m"))+ xlab("year")+ ylab("prec (mm)")+theme_bw()+ theme(axis.text.x = element_text(angle = 45, hjust = 1))
Comments
Post a Comment