x-axis labels overlap - want to rotate labels 45º

Greetings,
I'm using the code from this post

with my own data. However, my variable names are longer than those in mtcars. Using that code, how can I rotate the labels on the x-axis 45º to keep them from overlapping?

Advice and suggestions are welcome.

Cheers,
Jason the #rstatsnewbie

You can use the theme() function of ggplot.

DF <- data.frame(L = c("LongLabelAAA", "LongLabelBBB", "LongLabelCCC"),
                 Y = 1:3)
library(ggplot2)
ggplot(DF, aes(L, Y)) + geom_point() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Created on 2020-04-29 by the reprex package (v0.3.0)

1 Like

Another option as of ggplot2 version 3.3.0 is to stagger the labels:

library(ggplot2)
library(vcd)  # For Baseball data frame

ggplot(Baseball, aes(team86, error86)) + 
  geom_point() +
  scale_x_discrete(guide=guide_axis(n.dodge=2))

Rplot39

2 Likes

Both of these are excellent!
I really like the second option!
Thank you.
By any chance do you know what "theme_classic()" is and/or does?

EDIT
Also, would you (or anyone) know how to highlight cells with significant p-values in this code?

Cheers,
Jason the #rstatsnewbie

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

You can add + theme_classic() to your ggplot to see what it does. ggplot has several built-in "themes" that customize the overall look of the plot in various ways. The default theme is theme_grey(). You can change this for all future plots by running, for example:

theme_set(theme_classic())

To see available themes in ggplot, see the help for any theme. For example, run ?theme_classic(). The help page that opens will list all of the available themes. The ggthemes package has more, and you can of course create your own.

To tweak any theme, add a theme() statement to a ggplot after setting the overall theme. For example:

ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  theme_classic() +
  theme(axis.text.x=element_text(size=rel(1.1)),
        panel.border=element_rect(fill=NA))

See here and here for web versions of theme help files.

Here are some examples of how ggplots look with various themes.

1 Like

Thank you.
This helps a lot!
I will definitely follow up on your suggestions.

I am sure more questions will come soon enough. :smile::rofl:

~ Jason the #rstatsnewbie