gappy gappy - 9 days ago 6
R Question

Customize axis labels

I have a simple

geom_point
plot, in which the
x
variable is ordinal, taking 5 values (coded 1:5).

In the plot I would like to replace it with 5 corresponding text labels. Is it possible to do it in ggplot?

Answer

You should be able to do this with scale_x_discrete.

library(ggplot2)
df <- data.frame(x = 1:5, y = sample(1:10, 5, TRUE))

qplot(factor(x),y, data = df) + 
scale_x_discrete(breaks = 1:5, labels=c("foo","bar","baz","phi","fum")) +
xlab(NULL)
Comments