If I have a dataframe similar to that below
a=data.frame(year=paste('FY',2001:2012,sep='.'),values=rnorm(12))
library(ggplot2)
The following graph works
ggplot(a,aes(x=year,y=values,group=1))+geom_line()
but the following one does not.
ggplot(a,aes(x=year,y=values,group=1))+geom_line() +xlim(0,13)
How can I extend the limits of a ggplot of data that has a category axis rather than a numeric one?
You can add a new factor with NA to extend the x-range. It is a little bit tricky but it does the job. I hope someone else get better solution.
You can use
scale_x_discrete
and thelimits
parameter to add extra levels byc
ombining these with your original levels: