Anjan Anjan - 1 year ago 139
R Question

Changing font size and direction of axes text in ggplot2

I am plotting a graph with a categorical variable on the x axis and a numerical variable on the y axis.

For the x axis, given that there are many data points, the default text formatting causes the label for each tick mark to overlap with other labels. How do I (a) change the font size for my axis text and (b) change the orientation of the text so that the text is perpendicular to the axis?

Answer Source

Use theme():

d <- data.frame(x=gl(10, 1, 10, labels=paste("long text label ", letters[1:10])), y=rnorm(10))
ggplot(d, aes(x=x, y=y)) + geom_point() +
    theme(text = element_text(size=20),
        axis.text.x = element_text(angle=90, vjust=1)) 
#vjust adjust the vertical justification of the labels, which is often useful

enter image description here

There's lots of good information about how to format your ggplots here. You can see a full list of parameters you can modify (basically, all of them) using ?theme.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download