Reputation: 3
I am creating bar plots in qplot, (ggplot2
function in R) and have two problems.
When I rotate the x-axis scale values by 90 degrees they no longer centre under the columns and tick marks. This doesn't seem like much of an issue with just a few columns, but I'm creating multiple plots with 12 columns using facet-wrap and then the offset becomes very noticeable (the scale values then sit between the columns).
The x-axis values are dates as factors. How can I order the columns by date rather than by alphabetical order. If I convert the dates to date format they will order correctly but will space out on a continuous date scale which I dont want. This could be a problem with any type of x-axis data of course.
Here's a dummy dataset and R code to illustrate the problems.
date <- c("Oct 2006","Feb 2007","Nov 2007","Feb 2008","Oct 2008","Feb 2009","Nov 2009")
count <- c(23,54,12,78,23,34,12)
df <- data.frame(date,count)
qplot(x=date, y=count, data=df, geom="bar", stat="identity") + theme (axis.text.x = element_text(angle = 90,))
Upvotes: 0
Views: 255
Reputation: 20811
To make things easy, convert your date strings to a proper date format and then you don't have to worry about ordering them yoruself
by default, the vertical alignment is 1 which puts the tick labels to the right of ticks, 0 to the left, and .5 for centered.
I would also suggest switching to ggplot
instead of qplot
date <- c("Oct 2006","Feb 2007","Nov 2007","Feb 2008","Oct 2008","Feb 2009","Nov 2009")
df <- data.frame(
count = c(23,54,12,78,23,34,12),
date = factor(d <- as.Date(paste0('01 ', date), '%d %b %Y'),
labels = format(d, '%b %Y'))
)
library(ggplot2)
qplot(x=date, y=count, data=df, geom="bar", stat="identity") +
theme (axis.text.x = element_text(angle = 90, vjust = .5))
Upvotes: 1