将右侧的轴标签与ggplot2对齐 [英] Align axis label on the right with ggplot2
问题描述
考虑下面的内容:
$ $ $ $ $ $ $ $ $ d = data.frame(y = rnorm(120),
x = rep c(bar,long category name,foo),each = 40))
ggplot(d,aes(x = x,y = y))+
geom_boxplot()+
theme(axis.text.x = element_text(size = 15,angle = 90))
x轴标签由标签的中心对齐。是否有可能在右侧自动对齐,以便每个标签都可以在图的下方结束? 解决方案
如何: 您可以使用 Consider the following The x-axis labels are aligned by the center of the label. Is it possible to automatically align on the right so that every label would end right below the graph? Edit: A slightly nicer looking plot. How about: You can use the 这篇关于将右侧的轴标签与ggplot2对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
ggplot(d,aes(x = x,y = y))+ geom_boxplot()+
theme(axis.text.x = element_text(size = 15,angle = 90,hjust = 0.95,vjust = 0.2))
hjust
和 vjust
参数将标签移动到所需的位置。此外,如果您想将轴标签对准x轴,您可以更改 hjust = 1
。d = data.frame(y=rnorm(120),
x=rep(c("bar", "long category name", "foo"), each=40))
ggplot(d,aes(x=x,y=y)) +
geom_boxplot() +
theme(axis.text.x=element_text(size=15, angle=90))
ggplot(d,aes(x=x,y=y)) + geom_boxplot() +
theme(axis.text.x=element_text(size=15, angle=90,hjust=0.95,vjust=0.2))
hjust
and vjust
parameters to move the labels around to where you want. Also if you wanted the axes labels right up against the x-axis, you can just change hjust = 1
.