r - 对颜色/流程进行分组,以便条形图创建第一个条形图

标签 r ggplot2 ggalluvial

我有一个看起来像这样的数据集:

results <-  as.data.frame(cbind(c("Violence", "Violence", "Violence", "Violence", "Economic", "Economic","Economic","Economic","Institutional","Institutional","Institutional","Institutional"), 
                                c("No", "No", "Yes", "Yes","No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes"),
                                c("Yes", "No", "Yes", "No","Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No"), 
                                c(3,3,1,3,4,5,8,7,6,5,4,3)))
colnames(results) <- c("Type", "Test1", "Test2", "Freq")

然后我用 ggalluvial 创建一个冲积图

 library(ggplot2)
  library(tidyverse)
  library(ggalluvial)

ggplot(data = results,
       aes(axis1 = Type, axis2 = Test1, axis3 = Test2,
           y = Freq)) +
  scale_x_discrete(limits = c("Article", "False 0s Removed", "New Flow Measure"), expand = c(.2, .05)) +
  xlab("Results") +
  geom_flow(aes(fill = Type)) +
  geom_stratum() +
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  theme_minimal() +
  ggtitle("Replication Summary")

enter image description here

这看起来不错,除了每个层我希望垂直顺序按颜色(类型)组织,这样每个层都是一个条形图,我可以在其中看到每种类型的“否”和"is"的百分比测试。我将如何更改以便垂直排序按每个层(Test1 和 Test2)的颜色($type)分组。目前,第二层(测试 1)看起来不错,但第三层则不然(测试 2)

最佳答案

如果我理解正确的话,您唯一需要添加的是 geom_flow() 中的 aes.bind = 'flow'

results <-  data.frame(Type = c("Violence", "Violence", "Violence", "Violence", "Economic", "Economic","Economic","Economic","Institutional","Institutional","Institutional","Institutional"), 
                       Test1 = c("No", "No", "Yes", "Yes","No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes"),
                       Test2 = c("Yes", "No", "Yes", "No","Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No"), 
                       Freq = c(3,3,1,3,4,5,8,7,6,5,4,3)
                       )


library(ggplot2)
library(tidyverse)
library(ggalluvial)

ggplot(data = results,
       aes(axis1 = Type, axis2 = Test1, axis3 = Test2,
           y = Freq)) +
  scale_x_discrete(limits = c("Article", "False 0s Removed", "New Flow Measure"), expand = c(.2, .05)) +
  xlab("Results") +
  geom_flow(aes(fill = Type), aes.bind = 'flow') +
  geom_stratum() +
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  theme_minimal() +
  ggtitle("Replication Summary")

reprex package于2023年3月7日创建(v2.0.1)

编辑:我不太确定是否可以使用geom_flow()获取地层中的颜色,但您可以使用geom_alluvial()来实现。为此,我更改了示例数据的生成方式,因为在您的示例中 Freq 不是数字,并且 geom_alluvial() 引发了错误。现在,您可以将 fill 参数添加到 geom_stratum。如果一个层无法由单一类型填充,则其颜色将为NA。如果添加 scale_fill_discrete(na.value = NA),这些层将变得透明,您可以看到颜色。

ggplot(data = results,
       aes(axis1 = Type, axis2 = Test1, axis3 = Test2,
           y = Freq)) +
  scale_x_discrete(limits = c("Article", "False 0s Removed", "New Flow Measure"), expand = c(.2, .05)) +
  xlab("Results") +
  geom_alluvium(aes(fill = Type), aes.bind = "alluvia") +
  geom_stratum(aes(fill = Type)) +
  scale_fill_discrete(na.value = NA) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  theme_minimal() +
  ggtitle("Replication Summary")

reprex package于2023年3月7日创建(v2.0.1)

关于r - 对颜色/流程进行分组,以便条形图创建第一个条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75661918/

相关文章:

r - 如何根据变量大小而不是层大小计算 geom_flow() 中的百分比

正则表达式获取 data.table 中包含值的所有行?

r - 在 R 中合并数据帧的问题

r - 如何创建显示比值比和 95% CI 的图

r - 在ggplot中设置注释文本的宽度

r - ggplot2 - boxplot 多个 data.frames 同时保持秩序

R:如何将字符列换行以在 ggplot 中打印为 2 行,但它们不是标题?

r - 更改冲积图中字符的字体大小

r - 在 ubuntu 中停止或重启 R CMD Rserve

r - 用ggplot避开点和误差线