Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

r - ggplot x-axis labels with all x-axis values

I'm plotting ggplot with geom_point. The x-axis will be individuals' ID, and y-axis is variable A. How can I ggplot all and individual ID values on the x-axis without overlapping labels? ID may not be continuous.

df sample (actual rows are much longer)

> df
ID     A
1      4
2      12
3      45
5      1

Code for the plot:

ggplot(df, aes(x = ID, y = A)) + geom_point()

Above code has x-axis in intervals, but not presenting individual ID.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is this what you're looking for?

ID <- 1:50
A <- runif(50,1,100)

df <- data.frame(ID,A)

ggplot(df, aes(x = ID, y = A)) + 
  geom_point() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  scale_x_continuous("ID", labels = as.character(ID), breaks = ID)

This will produce this image:

enter image description here

So you'll get a label for every ID-value. If you'd like to remove the gridlines (There are too much for my taste) you can remove them by adding theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

EDIT: The easier way would be to just use ID as a factor for the plot. like this:

ggplot(df, aes(x = factor(ID), y = A)) + 
  geom_point() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  xlab("ID")

enter image description here

The advantage of this method is that you don't get empty spaces from missing IDs

EDIT2: Concerning your Problem with overlapping labels: I'm guessing it comes from a large number of IDs to be plotted. There are several ways we can deal with this. So lets say your plot looks like this:

enter image description here

One idea would be to hide every 3rd label from the x-axis by modifying the break argument of the axis:

ggplot(df, aes(x = factor(ID), y = A)) + 
  geom_point() + 
  scale_x_discrete(breaks = ID[c(T,F,F)]) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  xlab("ID")

which leads to this:

enter image description here

If hiding labels is not an option, you could split your plot into subplots.

df$group <- as.numeric(cut(df$ID, 4))

ggplot(df, aes(x = factor(ID), y = A)) + 
  geom_point() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
  xlab("ID") +
  facet_wrap(~group, ncol = 1, scales = "free_x")

which leads to this:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...