gpt4 book ai didi

r - 拼凑中色标的水平对齐

转载 作者:行者123 更新时间:2023-12-04 16:25:10 25 4
gpt4 key购买 nike

我正在使用包 patchwork 来垂直组合多个 ggplot2 绘图。无论比例名称的长度如何,我都希望每个地 block 的比例直接在另一个上方。目前,这些比例没有相互对齐。

我愿意使用 ggpubrfacet_grid() 如果它们可以做到的话,但我已经看到了这些方面 doesn't allow multiple scales ,我还没有找到任何使用 ggpubr

的解决方案
library(ggplot2)
library(patchwork)

set.seed(0)
testdata <- data.frame(x=1:10, y=1:10, col=runif(10))

g1 <- ggplot(testdata, aes(x=x,y=y,col=col)) + geom_point() +
scale_color_gradient(name="Short")
g2 <- ggplot(testdata, aes(x=x,y=y,col=col)) + geom_point() +
scale_color_gradient(name="A rather longer name")
g1/g2

ggsave("testfile.tiff", units = "mm", device="tiff",
width=100, height=100, dpi = 100)

Example plot with scales not aligned

理想输出:

Example plot with scales aligned

最佳答案

使用 plot_layout 您可以“收集”图例。这用作默认的 theme(legend.position = 'right')。您可以使用 & theme(legend.position = 'right') 在 plot_layout 之后添加它,如果要更改图例的位置,请调整位置。

g1/g2 + plot_layout(guides = 'collect') # & theme(legend.position = 'right') <- adjust position here!

ggsave("testfile.tiff", units = "mm", device="tiff",
width=100, height=100, dpi = 100)

enter image description here

关于r - 拼凑中色标的水平对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66174318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com