gpt4 book ai didi

r - ggplot - 多个图例排列

转载 作者:行者123 更新时间:2023-12-04 12:39:09 27 4
gpt4 key购买 nike

我想在多行多列的 ggplot 中排列多个图例。但是目前,从文档中我只能决定方向或操作 1 个图例中的行/列。我忽略了什么吗?感谢您提供解决方案的任何引用点。
这是示例代码以及我所做的和预期的结果。

data <- seq(1000, 4000, by=1000)
colorScales <- c("#c43b3b", "#80c43b", "#3bc4c4", "#7f3bc4")
names(colorScales) <- data
ggplot() +
geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
shape=21) +
scale_color_manual(name="Legend 1",
values=colorScales) +
scale_fill_gradientn(name="Legend 2",
labels=comma, limits=c(0, max(data)),
colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
values=c(0, 0.5, 1)) +
scale_size_continuous(name="Legend 3") +
theme(legend.direction = "vertical", legend.box = "vertical")

输出垂直图例:
enter image description here
ggplot() +
geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
shape=21) +
scale_color_manual(name="Legend 1",
values=colorScales) +
scale_fill_gradientn(name="Legend 2",
labels=comma, limits=c(0, max(data)),
colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
values=c(0, 0.5, 1)) +
scale_size_continuous(name="Legend 3") +
theme(legend.direction = "vertical", legend.box = "horizontal")

输出水平图例
enter image description here
ggplot() +
geom_point(aes(x=data, y=data, color=as.character(data), fill=data, size=data),
shape=21) +
scale_color_manual(name="Legend 1",
values=colorScales) +
scale_fill_gradientn(name="Legend 2",
labels=comma, limits=c(0, max(data)),
colours=rev(c("#000000", "#FFFFFF", "#BA0000")),
values=c(0, 0.5, 1)) +
guides(colour = guide_legend(nrow = 2, byrow = T, override.aes=list(size=4))) +
guides(size = guide_legend(nrow = 2, byrow = T)) +
scale_size_continuous(name="Legend 3") +
theme(legend.direction = "vertical", legend.box = "vertical")

输出具有垂直布局的图例,每个图例中有 2 列:
enter image description here

我想要的是这个:

enter image description here

最佳答案

这个想法是单独创建每个图( colorfillsize )然后提取它们的图例并以所需的方式将它们与主图组合在一起。

查看更多关于 cowplot包裹herepatchwork包裹here

library(ggplot2)
library(cowplot) # get_legend() & plot_grid() functions
library(patchwork) # blank plot: plot_spacer()

data <- seq(1000, 4000, by = 1000)
colorScales <- c("#c43b3b", "#80c43b", "#3bc4c4", "#7f3bc4")
names(colorScales) <- data

# Original plot without legend
p0 <- ggplot() +
geom_point(aes(x = data, y = data,
color = as.character(data), fill = data, size = data),
shape = 21
) +
scale_color_manual(
name = "Legend 1",
values = colorScales
) +
scale_fill_gradientn(
name = "Legend 2",
limits = c(0, max(data)),
colours = rev(c("#000000", "#FFFFFF", "#BA0000")),
values = c(0, 0.5, 1)
) +
scale_size_continuous(name = "Legend 3") +
theme(legend.direction = "vertical", legend.box = "horizontal") +
theme(legend.position = "none")

# color only
p1 <- ggplot() +
geom_point(aes(x = data, y = data, color = as.character(data)),
shape = 21
) +
scale_color_manual(
name = "Legend 1",
values = colorScales
) +
theme(legend.direction = "vertical", legend.box = "vertical")

# fill only
p2 <- ggplot() +
geom_point(aes(x = data, y = data, fill = data),
shape = 21
) +
scale_fill_gradientn(
name = "Legend 2",
limits = c(0, max(data)),
colours = rev(c("#000000", "#FFFFFF", "#BA0000")),
values = c(0, 0.5, 1)
) +
theme(legend.direction = "vertical", legend.box = "vertical")

# size only
p3 <- ggplot() +
geom_point(aes(x = data, y = data, size = data),
shape = 21
) +
scale_size_continuous(name = "Legend 3") +
theme(legend.direction = "vertical", legend.box = "vertical")

获取所有传奇

leg1 <- get_legend(p1)
leg2 <- get_legend(p2)
leg3 <- get_legend(p3)

# create a blank plot for legend alignment
blank_p <- plot_spacer() + theme_void()

结合传奇

# combine legend 1 & 2
leg12 <- plot_grid(leg1, leg2,
blank_p,
nrow = 3
)

# combine legend 3 & blank plot
leg30 <- plot_grid(leg3, blank_p,
blank_p,
nrow = 3
)

# combine all legends
leg123 <- plot_grid(leg12, leg30,
ncol = 2
)

把所有东西放在一起

final_p <- plot_grid(p0,
leg123,
nrow = 1,
align = "h",
axis = "t",
rel_widths = c(1, 0.3)
)

print(final_p)



创建于 2018-08-28 由 reprex package (v0.2.0.9000)。

关于r - ggplot - 多个图例排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52060601/

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