How To Print x Label Vertical In Ggplot2

[This article was first published on R – Saturn Science, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I was working with some boxplots last month and I needed to plot twelve months of air quality data. The problem was that the twelve months over lapped each other and the plot didn’t look good. If I could only draw the x labels vertical.

For this example, I’ll show you how to plot the x labels vertical. It’s just a matter of using the theme() function.

Here is the R code using ggplot to plot the Iris data of Species and Sepal.Width

p <- ggplot(iris, aes(Species, Sepal.Width))
p + geom_boxplot()

Here is the graph of this plot.

X horizontal

To make the x label vertical, add the theme() function:

p <- ggplot(iris, aes(Species, Sepal.Width))
p + geom_boxplot() + theme(axis.text.x = element_text(angle = 60, hjust = 1))

The new plot will look like this:

X 60 degrees

You can change it to 90 degrees by adjusting the angle.

p <- ggplot(iris, aes(Species, Sepal.Width))
p + geom_boxplot() + theme(axis.text.x = element_text(angle = 90, hjust = 1))

 

The new 90 degrees plot now looks like this:

X 90 degrees

I am working on more plots using air quality data that plots month verses AQI2.5 and I’ll have them for my next post.

To leave a comment for the author, please follow the link and comment on their blog: R – Saturn Science.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)