我正在 ggplot 中绘制一个图,其中 x 轴标签包含不同蛋白质的名称,但我遇到了麻烦,因为其中一些名称太长,并且标签变得太大,以至于很难看到图。
有什么方法可以减少 x 轴标签中的字符数,而不是“打印”更大的图表?
这是一个显示我的问题的示例:
library(ggplot2)
dat <- mtcars
# Make the x-axis labels very long for this example
dat$car <- paste0(rownames(mtcars),rownames(mtcars),rownames(mtcars),rownames(mtcars))
ggplot(dat, aes (x=car,y=hp)) +
geom_bar(stat ="identity", fill="#009E73",colour="black") +
theme_bw() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
![enter image description here](https://i.stack.imgur.com/Kxfle.png)
我想将标签从这样的东西转换:
Thisisaveryveryveryloooooongprotein
To this
Thisisavery[...]
这样我的情节就可以始终可见
Try the abbreviate
功能:
qplot(Species, Sepal.Length, data=iris, geom="boxplot") +
scale_x_discrete(label=abbreviate)
![example of label abbreviation](https://i.stack.imgur.com/WQJEn.png)
如果默认值不适用于您的情况,您可以定义自己的函数:
qplot(Species, Sepal.Length, data=iris, geom="boxplot") +
scale_x_discrete(label=function(x) abbreviate(x, minlength=7))
您还可以尝试旋转标签。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)