将颜色/流分组,使条形创建第一个条形图。

huangapple go评论54阅读模式
英文:

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 = &#39;flow&#39; in geom_flow().

results &lt;-  data.frame(Type = c(&quot;Violence&quot;, &quot;Violence&quot;, &quot;Violence&quot;, &quot;Violence&quot;, &quot;Economic&quot;, &quot;Economic&quot;,&quot;Economic&quot;,&quot;Economic&quot;,&quot;Institutional&quot;,&quot;Institutional&quot;,&quot;Institutional&quot;,&quot;Institutional&quot;), 
                       Test1 = c(&quot;No&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;Yes&quot;,&quot;No&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;Yes&quot;),
                       Test2 = c(&quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;,&quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;), 
                       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(&quot;Article&quot;, &quot;False 0s Removed&quot;, &quot;New Flow Measure&quot;), expand = c(.2, .05)) +
  xlab(&quot;Results&quot;) +
  geom_flow(aes(fill = Type), aes.bind = &#39;flow&#39;) +
  geom_stratum() +
  geom_text(stat = &quot;stratum&quot;, aes(label = after_stat(stratum))) +
  theme_minimal() +
  ggtitle(&quot;Replication Summary&quot;)

将颜色/流分组,使条形创建第一个条形图。

<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(&quot;Article&quot;, &quot;False 0s Removed&quot;, &quot;New Flow Measure&quot;), expand = c(.2, .05)) +
  xlab(&quot;Results&quot;) +
  geom_alluvium(aes(fill = Type), aes.bind = &quot;alluvia&quot;) +
  geom_stratum(aes(fill = Type)) +
  scale_fill_discrete(na.value = NA) +
  geom_text(stat = &quot;stratum&quot;, aes(label = after_stat(stratum))) +
  theme_minimal() +
  ggtitle(&quot;Replication Summary&quot;)

将颜色/流分组,使条形创建第一个条形图。

<sup>Created on 2023-03-07 by the reprex package (v2.0.1)</sup>

huangapple
  • 本文由 发表于 2023年3月7日 20:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定