Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot x-axis labels with all x-axis values

Tags:

r

ggplot2

axis

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!

like image 587
Lumos Avatar asked Dec 06 '17 06:12

Lumos


People also ask

How do I show all X-axis labels in R?

To display all the labels, we need to rotate the axis, and we do it using the las parameter. To rotate the label perpendicular to the axis we set the value of las as 2, and for horizontal rotation, we set the value as 1. Secondly, to increase the font size of the labels we use cex.

How do I change X label in ggplot2?

To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .

How do I change the X-axis tick marks in R?

Option 1. Set xaxt = "n" and yaxt = "n" to remove the tick labels of the plot and add the new labels with the axis function. Note that the at argument sets where to show the tick marks.

Which arguments can be used to add labels in Ggplot?

Method 1: Using geom_text() This method is used to add Text labels to data points in ggplot2 plots.


1 Answers

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

like image 104
brettljausn Avatar answered Sep 22 '22 09:09

brettljausn