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?
Make sure that it is formatted as General or as Number. On the Data tab of the ribbon, click Text to Columns. Select Delimited, then click Finish.
To format data labels, select your chart, and then in the Chart Design tab, click Add Chart Element > Data Labels > More Data Label Options. Click Label Options and under Label Contains, pick the options you want. To make data labels easier to read, you can move them inside the data points or even outside of the chart.
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)
scale_x_discrete
should do it:
x <- sample(1:5, 20, T) y <- rnorm(20) + x df <- data.frame(x = ordered(x), y = y) ggplot(df,aes(x,y)) + geom_point() + scale_x_discrete(breaks = 1:5, labels = letters[1:5])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With