Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize axis labels

Tags:

r

ggplot2

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?

like image 726
gappy Avatar asked Feb 23 '11 20:02

gappy


People also ask

Why can'ti edit horizontal axis labels in Excel?

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.

How do I customize Data labels in Excel?

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.


2 Answers

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) 
like image 151
Chase Avatar answered Sep 19 '22 17:09

Chase


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]) 
like image 29
Prasad Chalasani Avatar answered Sep 20 '22 17:09

Prasad Chalasani