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

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

Group color/ flow so that the bars create a bar chart of first

问题

以下是您要的翻译部分:

  1. 我有一个数据集,看起来像这样:
  2. results <- as.data.frame(cbind(c("Violence", "Violence", "Violence", "Violence", "Economic", "Economic","Economic","Economic","Institutional","Institutional","Institutional","Institutional"),
  3. c("No", "No", "Yes", "Yes","No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes"),
  4. c("Yes", "No", "Yes", "No","Yes", "No", "Yes", "No", "Yes", "No", "Yes", "No"),
  5. c(3,3,1,3,4,5,8,7,6,5,4,3)))
  6. colnames(results) <- c("Type", "Test1", "Test2", "Freq")

然后,我使用ggalluvial创建了一个连接河流图:

  1. library(ggplot2)
  2. library(tidyverse)
  3. library(ggalluvial)
  4. ggplot(data = results,
  5. aes(axis1 = Type, axis2 = Test1, axis3 = Test2,
  6. y = Freq)) +
  7. scale_x_discrete(limits = c("Article", "False 0s Removed", "New Flow Measure"), expand = c(.2, .05)) +
  8. xlab("Results") +
  9. geom_flow(aes(fill = Type)) +
  10. geom_stratum() +
  11. geom_text(stat = "stratum", aes(label = after_stat(stratum))) +
  12. theme_minimal() +
  13. ggtitle("Replication Summary")

这看起来很好,除了每个层次(stratum)之外,我希望垂直顺序按颜色(Type)组织,以便在每个测试的每个层次中,我可以看到每种类型的"No"和"Yes"的百分比。如何更改垂直排序以使每个层次(Test1和Test2)按颜色(Type)分组。目前第二层次(Test 1)看起来不错,但第三层次(Test 2)看起来不好。

[![enter image description here][1]][1]

  1. <details>
  2. <summary>英文:</summary>
  3. 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")

  1. 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")

  1. [![enter image description here][1]][1]
  2. 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)
  3. [1]: https://i.stack.imgur.com/SCqCr.png
  4. </details>
  5. # 答案1
  6. **得分**: 2
  7. 如果我理解正确,你唯一需要添加的是在 `geom_flow()` 中设置 `aes.bind = 'flow'`
  8. `geom_flow()` 函数中添加以下内容:
  9. ``` r
  10. aes.bind = 'flow'

然后,你可以使用下面的代码进行更改以实现所需效果:

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

如果你想在 strata 中添加颜色,可以使用 geom_alluvial() 并做如下更改:

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

希望这些信息有助于你实现所需的效果。

英文:

If I understood it correctly, the only thing you have to add is aes.bind = &#39;flow&#39; in geom_flow().

  1. 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;),
  2. 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;),
  3. 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;),
  4. Freq = c(3,3,1,3,4,5,8,7,6,5,4,3)
  5. )
  6. library(ggplot2)
  7. library(tidyverse)
  8. library(ggalluvial)
  9. ggplot(data = results,
  10. aes(axis1 = Type, axis2 = Test1, axis3 = Test2,
  11. y = Freq)) +
  12. scale_x_discrete(limits = c(&quot;Article&quot;, &quot;False 0s Removed&quot;, &quot;New Flow Measure&quot;), expand = c(.2, .05)) +
  13. xlab(&quot;Results&quot;) +
  14. geom_flow(aes(fill = Type), aes.bind = &#39;flow&#39;) +
  15. geom_stratum() +
  16. geom_text(stat = &quot;stratum&quot;, aes(label = after_stat(stratum))) +
  17. theme_minimal() +
  18. 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.

  1. ggplot(data = results,
  2. aes(axis1 = Type, axis2 = Test1, axis3 = Test2,
  3. y = Freq)) +
  4. scale_x_discrete(limits = c(&quot;Article&quot;, &quot;False 0s Removed&quot;, &quot;New Flow Measure&quot;), expand = c(.2, .05)) +
  5. xlab(&quot;Results&quot;) +
  6. geom_alluvium(aes(fill = Type), aes.bind = &quot;alluvia&quot;) +
  7. geom_stratum(aes(fill = Type)) +
  8. scale_fill_discrete(na.value = NA) +
  9. geom_text(stat = &quot;stratum&quot;, aes(label = after_stat(stratum))) +
  10. theme_minimal() +
  11. 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:

确定