ggplot2 中的旋转和间隔轴标签 [英] Rotating and spacing axis labels in ggplot2

查看:37
本文介绍了ggplot2 中的旋转和间隔轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图,其中 x 轴是一个标签很长的因子.虽然可能不是理想的可视化,但现在我想简单地将这些标签旋转为垂直.我已经用下面的代码弄清楚了这部分,但正如你所看到的,标签并不完全可见.

数据(钻石)Diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))q <- qplot(cut,carat,data=diamonds,geom=boxplot")q + opts(axis.text.x=theme_text(angle=-90))

解决方案

将最后一行改为

q + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

默认情况下,轴对齐在文本的中心,即使在旋转时也是如此.当您旋转 +/- 90 度时,您通常希望它在边缘对齐:

上图来自这篇博文.>

I have a plot where the x-axis is a factor whose labels are long. While probably not an ideal visualization, for now I'd like to simply rotate these labels to be vertical. I've figured this part out with the code below, but as you can see, the labels aren't totally visible.

data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
q <- qplot(cut,carat,data=diamonds,geom="boxplot")
q + opts(axis.text.x=theme_text(angle=-90))

解决方案

Change the last line to

q + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

By default, the axes are aligned at the center of the text, even when rotated. When you rotate +/- 90 degrees, you usually want it to be aligned at the edge instead:

The image above is from this blog post.

这篇关于ggplot2 中的旋转和间隔轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆