gpt4 book ai didi

r - 如何在一个ggplot中有两个不同大小的图例?

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

想象一下我绘制了这个玩具数据:

lev <- c("A", "B", "C", "D")

nodes <- data.frame(ord=c(1,1,1,2,2,3,3,4), brand=
factor(c("A", "B", "C","B", "C","D", "B","D"), levels=lev),
thick=c(16,9,9,16,4,1,4,1))

edge <- data.frame(ord1=c(1,1,2,3), brand1=factor(c("C","A","B","B"),
levels=lev),ord2=c(2,2,3,4), brand2=c("C","B","B","D"),
N1=c(2,1,2,1), N2=c(5,5,2,1))

ggplot() +
geom_point(data = nodes,
aes(x = ord, y = brand, size = sqrt(thick)),
color = "black", shape = 16, show.legend = T) +
scale_x_continuous(limits=c(1, 4), breaks=seq(0,4,1),
minor_breaks = NULL) +
geom_segment(data = edge,
aes(x = ord1, y = brand1, xend = ord2, yend = brand2),
color = "blue", size = edge$N2/edge$N1) +
ylim(lev) +
theme_bw()

正如预期的那样,我得到了这个情节。
enter image description here

我想添加另一个与线段宽度和 N2/N1 相关的图例(在节点下方)。

PD:
按照你的一些建议...
ggplot() + 
geom_segment(data = edge,
aes(x = ord1, y = brand1, xend = ord2, yend = brand2, size = N2/N1),
color = "blue", show.legend = T) +
geom_point(data = nodes,
aes(x = ord, y = brand, size = thick),
color = "black", shape = 16, show.legend = T) +
scale_x_continuous(limits = c(1, 4), breaks = 0:4,
minor_breaks = NULL) +
scale_size_continuous(trans = "sqrt", breaks = c(1,4,9,16)) +
ylim(lev) + theme_bw()

enter image description here

我得到了传说,但它与另一个重叠。

我可以尝试使用颜色而不是宽度:
ggplot()+ geom_segment(data=edge, aes(x=ord1, y=brand1, xend=ord2, yend=brand2, alpha=N2/N1) , size=1 ,show.legend = T) +
geom_point(data=nodes,aes(x=ord, y=brand, size=thick), color="black", shape=16,show.legend = T) +
scale_x_continuous(limits=c(1, 4), breaks=seq(0,4,1), minor_breaks = NULL) + scale_size_continuous(trans = "sqrt", breaks=c(1,4,9,16)) +
ylim(lev) + theme_bw()

enter image description here

或不同的阿尔法
enter image description here

虽然我更喜欢原始的宽度方法,因为在我的真实情节中,我会有很多线交叉。

PD:任何带有格子的解决方案或任何可以导出为 svg 或矢量 pdf 的替代方案?

PD2:我发现了另一个问题,细点没有正确缩放,有时无法强制 ggplot 显示正确的图例:
How can I force ggplot to show more levels on the legend?

最佳答案

使用我放在一起的高度实验性的包:

library(ggplot2) # >= 2.3.0
library(dplyr)
library(relayer) # install.github("clauswilke/relayer")

# make aesthetics aware size scale, also use better scaling
scale_size_c <- function(name = waiver(), breaks = waiver(), labels = waiver(),
limits = NULL, range = c(1, 6), trans = "identity", guide = "legend", aesthetics = "size")
{
continuous_scale(aesthetics, "area", scales::rescale_pal(range), name = name,
breaks = breaks, labels = labels, limits = limits, trans = trans,
guide = guide)
}


lev <- c("A", "B", "C", "D")

nodes <- data.frame(
ord = c(1,1,1,2,2,3,3,4),
brand = factor(c("A", "B", "C", "B", "C", "D", "B", "D"), levels=lev),
thick = c(16, 9, 9, 16, 4, 1, 4, 1)
)

edge <- data.frame(
ord1 = c(1, 1, 2, 3),
brand1 = factor(c("C", "A", "B", "B"), levels = lev),
ord2 = c(2, 2, 3, 4),
brand2 = c("C", "B", "B", "D"),
N1 = c(2, 1, 2, 1),
N2 = c(5, 5, 2, 1)
)

ggplot() +
(geom_segment(
data = edge,
aes(x = ord1, y = brand1, xend = ord2, yend = brand2, edge_size = N2/N1),
color = "blue"
) %>% rename_geom_aes(new_aes = c("size" = "edge_size"))) +
(geom_point(
data = nodes,
aes(x = ord, y = brand, node_size = thick),
color = "black", shape = 16
) %>% rename_geom_aes(new_aes = c("size" = "node_size"))) +
scale_x_continuous(
limits = c(1, 4),
breaks = 0:4,
minor_breaks = NULL
) +
scale_size_c(
aesthetics = "edge_size",
breaks = 1:5,
name = "edge size",
guide = guide_legend(keywidth = grid::unit(1.2, "cm"))
) +
scale_size_c(
aesthetics = "node_size",
trans = "sqrt",
breaks = c(1, 4, 9, 16),
name = "node size"
) +
ylim(lev) + theme_bw()



创建于 2018-05-16 由 reprex package (v0.2.0)。

关于r - 如何在一个ggplot中有两个不同大小的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46695651/

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