gpt4 book ai didi

R:将条形图与填充有匹配条形颜色的线条连接起来

转载 作者:行者123 更新时间:2023-12-02 02:22:12 26 4
gpt4 key购买 nike

我正在努力重新创建最近 Economist 中发现的情节的外观文章。有两个由线条连接的条形图,它们之间的空间具有相同的颜色,尽管有点不透明。

我看过这个问题asked但这些例子只有连接条形图的线。这是一些假数据:

set.seed(0)
data_bar <- data.frame(
stringsAsFactors = F,
Sample = rep(c("A", "B"), each = 10),
Percentage = runif(20),
Taxon = rep(1:10, by = 2)
)

我一直致力于重新设计链接的示例,但没有成功。有什么想法吗?

如果您看不到图表,这里有一个屏幕截图:

enter image description here

最佳答案

这需要一些调整条的宽度和区域的 x 位置,但本质上 geom_area(..., position = "fill") 可以带你走很远。或者,您也可以使用 position = "stack"

library(ggplot2)

set.seed(0)
data_bar <- data.frame(
stringsAsFactors = F,
Sample = rep(c("A", "B"), each = 10),
Percentage = runif(20),
Taxon = rep(1:10, by = 2)
)


ggplot(data_bar, aes(Sample, Percentage, fill = Taxon, group = Taxon)) +
geom_col(position = "fill", width = 0.5, colour = "black") +
geom_area(aes(x = c("A" = 1.25, "B" = 1.75)[Sample]),
position = "fill", colour = "black", alpha = 0.5,
outline.type = "both")

reprex package 创建于 2021-02-16 (v1.0.0)

堆叠的替代方案:

ggplot(data_bar, aes(Sample, Percentage, fill = Taxon, group = Taxon)) +
geom_col(position = "stack", width = 0.5, colour = "black") +
geom_area(aes(x = c("A" = 1.25, "B" = 1.75)[Sample]),
position = "stack", colour = "black", alpha = 0.5,
outline.type = "both")

enter image description here

关于R:将条形图与填充有匹配条形颜色的线条连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66229164/

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