gpt4 book ai didi

r - 箭头在 ggplot2 中用作尺寸美学

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

我有包含矢量坐标及其特征的数据 - success_rateprop :

ex_data <- structure(list(group = c("group A", "group A", "group A", "group A", 
"group A", "group A", "group C", "group C", "group C", "group C",
"group C", "group W", "group W", "group W", "group W", "group W"
), category = c(5, 4, 3, 2, 1, 6, 5, 1, 2, 3, 4, 1, 4, 5, 2,
3), success_rate = c(0.816037735849057, 0.938775510204082, 0.653061224489796,
0.985915492957746, 0.934306569343066, 1, 0.979166666666667, 0.887323943661972,
0.319587628865979, 0.721590909090909, 0.941176470588235, 0.689320388349515,
0.338028169014085, 0.396551724137931, 0.7375, 0.763948497854077
), x0 = c(24.5, 24.5, 23.7, 22.6, 28.6, 27.6, 27.2, 27.4, 15.7,
25.5, 24.2, 21.4, 17.5, 9.9, 23.6, 29), y0 = c(21.2, 27.9, 96.6,
43.9, 65.1, 71.5, 34.3, 71.2, 47.9, 88, 36, 86.9, 49.4, 41.3,
85.6, 16.7), x1 = c(35.2, 36.5, 34, 32.9, 39.4, 40.2, 35.9, 31.6,
63.1, 29.5, 35.9, 35.4, 61.4, 54, 37.3, 37.2), y1 = c(5.8, 33.6,
96.8, 70, 64.8, 96.6, 7.3, 64.9, 63.1, 89.7, 38.5, 95, 59.3,
32.2, 77.9, 12), prop = c(0.124926340601061, 0.0866234531526223,
0.0288744843842074, 0.041838538597525, 0.0807307012374779, 0.0271066588096641,
0.0365853658536585, 0.0541158536585366, 0.0739329268292683, 0.134146341463415,
0.0259146341463415, 0.0787461773700306, 0.108562691131498, 0.0443425076452599,
0.0611620795107034, 0.178134556574924)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -16L))

我不想为每个 group 制作矢量图形将使用 prop作为尺寸美学。不幸的是,我不认为在箭头大小美学的情况下效果不佳,因为它调整了整个箭头的大小,这通常看起来很糟糕:
ggplot(ex_data) +
geom_segment(aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate, size = prop),
arrow = arrow(length = unit(0.2, "cm"), ends = 'last', type = 'closed')) +
facet_wrap(~group) +
scale_color_viridis_c()

enter image description here

所以我只想用 prop 控制箭头的大小多变的。不幸的是,箭头不是 geom_segment美学和控制它们大小的唯一方法是为它们提供单独的向量。它适用于单个组:
ex_data %>% 
filter(group == 'group C') %>%
ggplot() +
geom_segment(aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate),
arrow = arrow(length = unit(filter(ex_data, group == 'group C')$prop * 3, "cm"), ends = 'last', type = 'closed')) +
facet_wrap(~group) +
scale_color_viridis_c()

enter image description here

但不是 facet_wrap :
ggplot(ex_data) +
geom_segment(aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate),
arrow = arrow(length = unit(ex_data$prop * 3, "cm"), ends = 'last', type = 'closed')) +
facet_wrap(~group) +
scale_color_viridis_c()

enter image description here

您可以注意到 group C 的箭头大小不再有效(例如,上面的箭头应该有最大的箭头,而它有最小的)。我的原始数据有更多的组,创建单独的图会非常乏味。有什么解决方法吗?

最佳答案

通常在这种情况下(例如, 123 ),我们可以为每个方面使用子数据框。例如,

ggplot(ex_data) +
lapply(split(ex_data, ex_data$group), function(df)
geom_segment(data = df, aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate),
arrow = arrow(length = unit(df$prop * 3, "cm"), ends = 'last', type = 'closed'))) +
facet_wrap(~group) + scale_color_viridis_c()

enter image description here

关于r - 箭头在 ggplot2 中用作尺寸美学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54346541/

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