Chris Robles
Chris Robles

Reputation: 203

ggplot x axis spacing between labels in R

Been looking everywhere but couldnt find an answer.

Cant figure out how to space BETWEEN x points. Everything I found talked about vjust.

My graph: https://i.sstatic.net/F7F0F.jpg

code:

df.307b<-read.csv('307B.csv')
colnames(df.307b)<-c('Chr', 'Reads')
p<-ggplot(data=df.307b, aes(Chr, Reads)) + geom_bar(stat="identity") 
require(scales)
p + theme(axis.text.x = element_text(angle = 90, hjust = 1)) + scale_y_continuous(labels = comma) + ggtitle("307B_WES_Read_distribution") 

I have been looking for an hour and a half. Tried hjust, vjust, scale_x_continuous.

Thanks guys

Upvotes: 1

Views: 2536

Answers (1)

Tom
Tom

Reputation: 714

Your problem is the combination of font size and horizontal sizing of the graph. Basically, the font is too big, or the graph is too small.

The best solution is to follow the advice of William Cleveland and flip your axes, putting the text axis on the vertical. Your ggplot call should then look like:

p<-ggplot(data=df.307b, aes(Chr, Reads)) + geom_bar(stat="identity") + coord_flip()

Normally the graph would be even easier to read if you use geom_point() instead of geom_bar().

Another approach would be to save your graph manually, playing with the width and height parameters to get the right combination of aspect ratio and font size:

ggsave("plot.png", p, height = 3, width = 6, units = "in", dpi = 300)

However, having the categories on the x-axis is always going to be prone to such problems, and the contortions needed to read rotated text will always make such graphs less readable than using coord_flip().

Upvotes: 1

Related Questions