ggplot2 中的旋转和间距轴标签
我有一个图,其中 x 轴是一个标签很长的因子。虽然可能不是理想的可视化,但现在我想简单地将这些标签旋转为垂直。我已经用下面的代码解决了这部分问题,但正如您所看到的,标签并不完全可见。
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))
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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
将最后一行更改为
默认情况下,即使旋转,轴也会在文本中心对齐。当您旋转 +/- 90 度时,您通常希望它在边缘对齐:
上图来自此博客发布。
Change the last line to
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.
ggplot 3.3.0
通过提供guide_axis(angle = 90)
(作为scale_..
的guide
参数或作为x
来修复此问题guides
的参数):来自
angle
参数的文档:或者,它还提供
guide_axis(n.dodge = 2)
(作为scale_..
的guide
参数或作为x 参数到
guides
),通过垂直避开标签来克服过度绘制问题。在这种情况下效果很好:ggplot 3.3.0
fixes this by providingguide_axis(angle = 90)
(asguide
argument toscale_..
or asx
argument toguides
):From the documentation of the
angle
argument:Alternatively, it also provides
guide_axis(n.dodge = 2)
(asguide
argument toscale_..
or asx
argument toguides
) to overcome the over-plotting problem by dodging the labels vertically. It works quite well in this case:使用
coord_flip()
添加
str_wrap()
在 R for Data Science,Wickham 和 Grolemund 发言对于这个确切的问题:
Use
coord_flip()
Add
str_wrap()
In Ch 3.9 of R for Data Science, Wickham and Grolemund speak to this exact question:
要使刻度标签上的文本完全可见并以与 y 轴标签相同的方向读取,请将最后一行更改为
To make the text on the tick labels fully visible and read in the same direction as the y-axis label, change the last line to
我想提供一个替代解决方案,最新版本的 ggtern,自从引入画布旋转功能以来。
基本上,您需要使用三角学确定相对位置,方法是构建一个返回
element_text
对象的函数,给定角度(即度数)和定位(即 x、y、顶部或右侧之一)信息。坦率地说,在我看来,我认为在指定时,应该为
hjust
和vjust
参数在 ggplot2 中提供“auto”选项无论如何,让我们从角度来演示上面的工作原理。生成以下内容:
I'd like to provide an alternate solution, a robust solution similar to what I am about to propose was required in the latest version of ggtern, since introducing the canvas rotation feature.
Basically, you need to determine the relative positions using trigonometry, by building a function which returns an
element_text
object, given angle (ie degrees) and positioning (ie one of x,y,top or right) information.Frankly, in my opinion, I think that an 'auto' option should be made available in
ggplot2
for thehjust
andvjust
arguments, when specifying the angle, anyway, lets demonstrate how the above works.Which produces the following:
过时 - 请参阅此答案了解更简单的方法
要获得无需额外依赖项的可读 x 刻度标签,您需要使用:
已 刻度标签逆时针旋转 90°,并在其末端垂直对齐 (
hjust = 1
),并将其中心与相应的刻度线水平对齐 (vjust = 0.5
)。完整示例:
请注意,垂直/水平对齐参数
vjust
/hjustelement_text
的 code> 是相对于文本的。因此,vjust
负责水平对齐。如果没有
vjust = 0.5
,它看起来像这样:没有
hjust = 1
code> 它看起来像这样:如果由于某种(有线)原因您想将刻度标签顺时针旋转 90°(以便可以从左侧读取它们)您需要使用:
q + theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = -1))
。所有这些都已经在这个答案的评论中讨论过,但我经常回到这个问题,我想我可以直接复制而不阅读评论的答案。
OUTDATED - see this answer for a simpler approach
To obtain readable x tick labels without additional dependencies, you want to use:
This rotates the tick labels 90° counterclockwise and aligns them vertically at their end (
hjust = 1
) and their centers horizontally with the corresponding tick mark (vjust = 0.5
).Full example:
Note, that vertical/horizontal justification parameters
vjust
/hjust
ofelement_text
are relative to the text. Therefore,vjust
is responsible for the horizontal alignment.Without
vjust = 0.5
it would look like this:Without
hjust = 1
it would look like this:If for some (wired) reason you wanted to rotate the tick labels 90° clockwise (such that they can be read from the left) you would need to use:
q + theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = -1))
.All of this has already been discussed in the comments of this answer but I come back to this question so often, that I want an answer from which I can just copy without reading the comments.
ggpubr 包提供了一个默认情况下执行正确操作的快捷方式(右对齐文本,中间对齐文本框以勾选):
由 reprex 包(v0.2.1)于 2018 年 11 月 6 日创建)
通过 GitHub 搜索相关参数名称找到:https://github.com/search?l=R&q=element_text+angle+90+vjust+org%3Acran&type=Code
The ggpubr package offers a shortcut that does the right thing by default (right align text, middle align text box to tick):
Created on 2018-11-06 by the reprex package (v0.2.1)
Found with a GitHub search for the relevant argument names: https://github.com/search?l=R&q=element_text+angle+90+vjust+org%3Acran&type=Code
coord_flip() 的替代方法是使用 ggstance 包。
优点是它可以更轻松地将图形与其他图形类型组合起来,也许更重要的是,您可以为坐标系设置固定比例比例。
由 reprex 包 (v0.3.0)
An alternative to
coord_flip()
is to use theggstance
package.The advantage is that it makes it easier to combine the graphs with other graph types and you can, maybe more importantly, set fixed scale ratios for your coordinate system.
Created on 2020-03-11 by the reprex package (v0.3.0)