英文:
Group color/ flow so that the bars create a bar chart of first
问题
以下是您要的翻译部分:
我有一个数据集,看起来像这样:
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")
这看起来很好,除了每个层次(stratum)之外,我希望垂直顺序按颜色(Type)组织,以便在每个测试的每个层次中,我可以看到每种类型的"No"和"Yes"的百分比。如何更改垂直排序以使每个层次(Test1和Test2)按颜色(Type)分组。目前第二层次(Test 1)看起来不错,但第三层次(Test 2)看起来不好。
[![enter image description here][1]][1]
<details>
<summary>英文:</summary>
I have a dataset that looks something like this:
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")
Then I create an alluvial plot with 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][1]][1]
This looks fine except for each stratum I want the vertical order to be organized by the color (Type), so that each stratum is a bar chart of sorts where I can see what percentage of each Type are No and Yes for each test. How would I change so the vertical ordering is grouped by color ($type) at each stratum (Test1 and Test2). At current the second stratum (Test 1) looks good but the the third does not (test 2)
[1]: https://i.stack.imgur.com/SCqCr.png
</details>
# 答案1
**得分**: 2
如果我理解正确,你唯一需要添加的是在 `geom_flow()` 中设置 `aes.bind = 'flow'`。
在 `geom_flow()` 函数中添加以下内容:
``` r
aes.bind = 'flow'
然后,你可以使用下面的代码进行更改以实现所需效果:
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")
如果你想在 strata 中添加颜色,可以使用 geom_alluvial()
并做如下更改:
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")
希望这些信息有助于你实现所需的效果。
英文:
If I understood it correctly, the only thing you have to add is aes.bind = 'flow'
in geom_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")
<sup>Created on 2023-03-07 by the reprex package (v2.0.1)</sup>
EDIT: I am not quite sure if it is possible to get the colors in the stratas with geom_flow()
, but you can do it with geom_alluvial()
. For this I changed the way the example data was generated, because in your example Freq
was not numeric and geom_alluvial()
threw an error. Now you can add the fill
-argument to geom_stratum
. If one stratum cannot filled by a single Type its color will be NA
. If you add scale_fill_discrete(na.value = NA)
these strata will become transparent and you can see the colors.
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")
<sup>Created on 2023-03-07 by the reprex package (v2.0.1)</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论