R 将ggplot2中的图例标题和图例键居中对齐,以获得长图例标题
当图例标题较长时,我很难使图例中心的标题与图例键对齐。有一个问题是,它适用于短标题,但似乎不适用于长标题 例如,首先是一个简短的图例标题:R 将ggplot2中的图例标题和图例键居中对齐,以获得长图例标题,r,ggplot2,r-grid,gtable,R,Ggplot2,R Grid,Gtable,当图例标题较长时,我很难使图例中心的标题与图例键对齐。有一个问题是,它适用于短标题,但似乎不适用于长标题 例如,首先是一个简短的图例标题: library(ggplot2) ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) + scale_color_distiller(palette = "YlGn", type = "seq", direction = -1
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
name = "A") +
theme(legend.title.align = 0.5)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
name = "Long legend heading\nShould be centered") +
theme(legend.title.align = 0.5)
一切正常,图例标题位于图例键上方的中心
现在,与长图例标题相同:
library(ggplot2)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
name = "A") +
theme(legend.title.align = 0.5)
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) + geom_point(size = 3) +
scale_color_distiller(palette = "YlGn", type = "seq", direction = -1,
name = "Long legend heading\nShould be centered") +
theme(legend.title.align = 0.5)
我们可以看到,文本与自身中心对齐,但与图例键无关。我尝试过修改其他主题选项,例如legend.justification=“center”
,但似乎没有一个选项将关键点从图例框中最左侧的位置移动
有几点意见:
- 我正在运行几天前发布的ggplot2 v2.2.1.9000的开发版本
- 我特别需要一个连续色阶调色板的解决方案
- 您必须更改源代码。当前,在视口(GTTable)中显示它。这是硬编码的。2019年10月4日更新:
不久前,我写了一个相当通用的函数,它是基于两年前我在这里发布的原始想法。该函数位于github上,但它不是任何正式发布的包的一部分。其定义如下:
align_legend我对源代码进行了黑客攻击,类似于巴蒂斯特在上述评论中描述的方式:将颜色栏/标签/勾号格罗布放入子表,并将其定位为与标题具有相同的行/列跨度(取决于图例的方向)
这仍然是一种黑客行为,但我想把它看作是一种“整个会话只进行一次黑客行为”的方法,而不必为每个情节手动重复这些步骤
不同标题宽度/标题位置/图例方向的演示:
plot.demo <- function(title.width = 20,
title.position = "top",
legend.direction = "vertical"){
ggplot(iris,
aes(x=Sepal.Length, y=Sepal.Width, color=Petal.Width)) +
geom_point(size = 3) +
scale_color_distiller(palette = "YlGn",
name = stringr::str_wrap("Long legend heading should be centered",
width = title.width),
guide = guide_colourbar(title.position = title.position),
direction = -1) +
theme(legend.title.align = 0.5,
legend.direction = legend.direction)
}
cowplot::plot_grid(plot.demo(),
plot.demo(title.position = "left"),
plot.demo(title.position = "bottom"),
plot.demo(title.width = 10, title.position = "right"),
plot.demo(title.width = 50, legend.direction = "horizontal"),
plot.demo(title.width = 10, legend.direction = "horizontal"),
ncol = 2)
(旁注:legend.box.just=“center”
需要正确对齐两个图例。我担心了一会儿,因为目前只有“top”、“bottom”、“left”和“right”被列为可接受的参数值,但结果是“center”/“center”也被底层的网格::valid.所接受。只是
。我不确定为什么在?主题
帮助文件中没有明确提到这一点;尽管如此,它确实有效。)
要更改源代码,请运行:
trace(ggplot2:::guide_gengrob.colorbar, edit = TRUE)
并将代码的最后一部分更改为:
gt <- gtable(widths = unit(widths, "cm"), heights = unit(heights,
"cm"))
... # omitted
gt
}
使用的软件包版本:ggplot2 3.2.1。谢谢,这正是我所担心的。首先,我要打开一个问题。问题有自己的问题;如果你做得太过分了,可能会有。啊,我明白了,它需要附带一个拉动请求。也许以后的某一天,如果不添加另一个主题参数,我无法立即解决此问题。你能想出一种简单的方法来编辑legend grob以解决此ggplot2短命令问题吗?指南代码对我来说意义不大,据我所知,没有太多解释,但是使用GTTable处理对齐的一般策略是将其设置为子GTTable,并相应地将其放置在其父GTTable中(然后,查看输出指南grobs已经是嵌套GTTables的俄罗斯玩偶,因此添加一层可能不是一个好主意)。
untrace(ggplot2:::guide_gengrob.colorbar)