Skip to content
Fix Code Error

Rotating and spacing axis labels in ggplot2

March 13, 2021 by Code Error
Posted By: Anonymous

I have a plot where the x-axis is a factor whose labels are long. While probably not an ideal visualization, for now I’d like to simply rotate these labels to be vertical. I’ve figured this part out with the code below, but as you can see, the labels aren’t totally visible.

data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
q <- qplot(cut,carat,data=diamonds,geom="boxplot")
q + opts(axis.text.x=theme_text(angle=-90))

enter image description here

Solution

Change the last line to

q + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

By default, the axes are aligned at the center of the text, even when rotated. When you rotate +/- 90 degrees, you usually want it to be aligned at the edge instead:

alt text

The image above is from this blog post.

Answered By: Anonymous

Related Articles

  • What do hjust and vjust do when making a plot using ggplot?
  • How to change line width in ggplot?
  • Pandas pivot_table: filter on aggregate function
  • including regression coefficient and pvalue in the ggplot2
  • What to change in circular barplot in R?
  • ggplot geom_text font size control
  • Construct a manual legend for a complicated plot
  • Remove grid, background color, and top and right borders…
  • How to calculate the angle between a line and the horizontal…
  • plot different color for different categorical levels using…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Converting double to string

Next Post:

How to detect pressing Enter on keyboard using jQuery?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error