Chr*_*ois 607 r labels 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))
Run Code Online (Sandbox Code Playgroud)
Jon*_*ang 1016
将最后一行更改为
q + theme(axis.text.x = element_text(angle = 90, hjust = 1))
Run Code Online (Sandbox Code Playgroud)
默认情况下,即使旋转,轴也会在文本的中心对齐.旋转+/- 90度时,通常希望它在边缘对齐:
上面的图片来自这篇博客文章.
e3b*_*3bo 84
要使刻度标签上的文本完全可见并以与y轴标签相同的方向读取,请将最后一行更改为
q + theme(axis.text.x=element_text(angle=90, hjust=1))
Run Code Online (Sandbox Code Playgroud)
Ric*_*loo 74
coord_flip()
在"R for Data Science"中,Wickham和Grolemund谈到了这个确切的问题.在第3.8章" 位置调整"中,他们写道:
coord_flip()
切换x和y轴.如果您想要水平箱图,这很有用(例如).它对于长标签也很有用:很难让它们适应而不会在x轴上重叠.
将此应用于您的情节,我们只需添加coord_flip()
到ggplot:
data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
qplot(cut,carat,data = diamonds, geom = "boxplot") +
coord_flip()
Run Code Online (Sandbox Code Playgroud)
现在,超长标题横向展开,非常容易阅读!
jan*_*glx 47
ggplot 3.3.0
通过提供guide_axis(angle = 90)
(作为guide
参数scale_..
或作为x
参数guides
)来解决这个问题:
library(ggplot2)
data(diamonds)
diamonds$cut <- paste("Super Dee-Duper", as.character(diamonds$cut))
ggplot(diamonds, aes(cut, carat)) +
geom_boxplot() +
scale_x_discrete(guide = guide_axis(angle = 90)) +
# ... or, equivalently:
# guides(x = guide_axis(angle = 90)) +
NULL
Run Code Online (Sandbox Code Playgroud)
与在 theme() / element_text() 中设置角度相比,这还使用了一些启发式方法来自动选择您可能想要的 hjust 和 vjust。
或者,它还提供 guide_axis(n.dodge = 2)
(作为 的guide
参数scale_..
或作为 的x
参数guides
)通过垂直躲避标签来克服过度绘图问题。在这种情况下它工作得很好:
library(ggplot2)
data(diamonds)
diamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))
ggplot(diamonds, aes(cut, carat)) +
geom_boxplot() +
scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
NULL
Run Code Online (Sandbox Code Playgroud)
Nic*_*ton 23
我想提供一个替代解决方案,因为引入了画布旋转功能,所以在最新版本的ggtern中需要类似于我要提出的强大解决方案.
基本上,您需要使用三角函数确定相对位置,方法是构建一个函数,该函数返回一个element_text
对象,给定角度(即度)和定位(即x,y,top或right之一)信息.
#Load Required Libraries
library(ggplot2)
library(gridExtra)
#Build Function to Return Element Text Object
rotatedAxisElementText = function(angle,position='x'){
angle = angle[1];
position = position[1]
positions = list(x=0,y=90,top=180,right=270)
if(!position %in% names(positions))
stop(sprintf("'position' must be one of [%s]",paste(names(positions),collapse=", ")),call.=FALSE)
if(!is.numeric(angle))
stop("'angle' must be numeric",call.=FALSE)
rads = (angle - positions[[ position ]])*pi/180
hjust = 0.5*(1 - sin(rads))
vjust = 0.5*(1 + cos(rads))
element_text(angle=angle,vjust=vjust,hjust=hjust)
}
Run Code Online (Sandbox Code Playgroud)
坦率地说,在我看来,我认为应该ggplot2
为hjust
和vjust
参数提供'自动'选项,当指定角度时,无论如何,让我们演示上面的工作原理.
#Demonstrate Usage for a Variety of Rotations
df = data.frame(x=0.5,y=0.5)
plots = lapply(seq(0,90,length.out=4),function(a){
ggplot(df,aes(x,y)) +
geom_point() +
theme(axis.text.x = rotatedAxisElementText(a,'x'),
axis.text.y = rotatedAxisElementText(a,'y')) +
labs(title = sprintf("Rotated %s",a))
})
grid.arrange(grobs=plots)
Run Code Online (Sandbox Code Playgroud)
产生以下内容:
jan*_*glx 14
要获得可读的 x 刻度标签而不需要额外的依赖项,您需要使用:
\n ... +\n theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +\n ...\n
Run Code Online (Sandbox Code Playgroud)\n这会将刻度标签逆时针旋转 90\xc2\xb0,并将它们在其末端 ( hjust = 1
) 垂直对齐,并将其中心与相应的刻度线 ( vjust = 0.5
) 水平对齐。
完整示例:
\nlibrary(ggplot2)\ndata(diamonds)\ndiamonds$cut <- paste("Super Dee-Duper",as.character(diamonds$cut))\nq <- qplot(cut,carat,data=diamonds,geom="boxplot")\nq + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))\n
Run Code Online (Sandbox Code Playgroud)\n请注意,垂直/水平对齐参数vjust
/是相对于文本的hjust
。element_text
因此,vjust
负责水平对齐。
如果没有vjust = 0.5
它,看起来会像这样:
q + theme(axis.text.x = element_text(angle = 90, hjust = 1))\n
Run Code Online (Sandbox Code Playgroud)\n如果没有hjust = 1
它,看起来会像这样:
q + theme(axis.text.x = element_text(angle = 90, vjust = 0.5))\n
Run Code Online (Sandbox Code Playgroud)\n如果由于某种(有线)原因您想要顺时针旋转刻度标签 90\xc2\xb0 (以便可以从左侧读取它们),您将需要使用:q + theme(axis.text.x = element_text(angle = -90, vjust = 0.5, hjust = -1))
。
所有这些都已经在这个答案的评论中讨论过,但我经常回到这个问题,我想要一个答案,我可以直接复制而不阅读评论。
\n该ggpubr包提供一个快捷方式的默认操作(右对齐文本,中间对齐文本框中打勾)正确的事情:
library(ggplot2)
diamonds$cut <- paste("Super Dee-Duper", as.character(diamonds$cut))
q <- qplot(cut, carat, data = diamonds, geom = "boxplot")
q + ggpubr::rotate_x_text()
Run Code Online (Sandbox Code Playgroud)
由reprex软件包(v0.2.1)创建于2018-11-06
在GitHub搜索中找到了相关的参数名称:https : //github.com/search?l=R&q= element_text+angle+90+vjust+org%3Acran&type =Code
归档时间: |
|
查看次数: |
667920 次 |
最近记录: |