简体   繁体   English

ggplot x R中标签之间的轴间距

[英]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. 无法弄清楚如何在x点之间间隔。 Everything I found talked about vjust. 我发现的一切都谈到了正义。

My graph: http://imgur.com/kcJ5MCv 我的图表: http//imgur.com/kcJ5MCv

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. 尝试过调整,调整,scale_x_continuous。

Thanks guys 多谢你们

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. 最好的解决方案是遵循William Cleveland的建议并翻转轴,将文本轴垂直放置。 Your ggplot call should then look like: 您的ggplot调用应如下所示:

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() . 通常,如果使用geom_point()而不是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: 另一种方法是手动保存图形,使用widthheight参数来获得纵横比和字体大小的正确组合:

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() . 但是,在x轴上具有类别总是容易出现此类问题,并且读取旋转文本所需的扭曲将始终使此类图形的可读性不及使用coord_flip()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM