创建一个多列的饼图

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

Create a pie chart of multiple columns

问题

我想创建一个包含多列数据的饼图。这种略显不同寻常的组合是为了评估一项大型调查的结果。答案一直是是/否,而且有多列数据重叠,因此简单地合并它们会改变陈述。最终,需要每列数据中的“是”答案的绝对数量。

#示例表格:

  1. x <- read.csv2(file = textConnection("a;b;c;d
  2. yes;no;yes;no
  3. no;no;yes;no
  4. no;no;yes;no
  5. no;yes;no;no
  6. no;yes;no;no
  7. no;no;no;yes
  8. no;no;no;yes
  9. no;no;yes;yes
  10. no;no;yes;yes
  11. no;yes;yes;no
  12. yes;yes;no;no
  13. yes;yes;no;no
  14. yes;yes;no;no
  15. yes;yes;yes;no
  16. yes;no;yes;no
  17. yes;no;yes;yes
  18. yes;no;yes;yes
  19. no;no;no;yes
  20. no;no;no;no
  21. no;no;no;no
  22. no;no;yes;no
  23. no;no;yes;no
  24. yes;no;no;no
  25. yes;no;no;no"))

最终,应该有一个饼图,以百分比显示a列中的“是”答案的数量(x%),b列中的“是”答案的数量(y%)...以此类推。

感谢您提前的帮助。

英文:

I want to create a pie chart of multiple columns. This somewhat unusual constellation is necessary for the evaluation of a large survey. The answers were always yes/no, with several columns overlapping with yes/no, so simply merging them would change the statement. In the end, an absolute number of yes answers would be needed per associated column in total.

#example table:

  1. x &lt;- read.csv2(file = textConnection(&quot;a;b;c;d
  2. yes;no;yes;no
  3. no;no;yes;no
  4. no;no;yes;no
  5. no;yes;no;no
  6. no;yes;no;no
  7. no;no;no;yes
  8. no;no;no;yes
  9. no;no;yes;yes
  10. no;no;yes;yes
  11. no;yes;yes;no
  12. yes;yes;no;no
  13. yes;yes;no;no
  14. yes;yes;no;no
  15. yes;yes;yes;no
  16. yes;no;yes;no
  17. yes;no;yes;yes
  18. yes;no;yes;yes
  19. no;no;no;yes
  20. no;no;no;no
  21. no;no;no;no
  22. no;no;yes;no
  23. no;no;yes;no
  24. yes;no;no;no
  25. yes;no;no;no&quot;))

In the end, there should be a pie chart showing in percent the a's amount of yes (x%), b's amount of yes (y%)... and so on.

Thanks in advance for your help.

答案1

得分: 1

这显示了每列的“yes”绝对数量。

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. x %>%
  5. pivot_longer(cols = everything()) %>%
  6. summarise(res = sum(value == "yes"), .by = name) %>%
  7. ggplot(aes(x="", y=res, fill=name)) +
  8. geom_bar(stat="identity", width=1) +
  9. coord_polar("y", start=0) +
  10. geom_text(aes(label = res), position = position_stack(vjust = 0.5),
  11. color = "white", size=6) +
  12. scale_fill_brewer(palette="Set1") +
  13. theme_void()

创建一个多列的饼图

英文:

This shows the absolute number of "yes" for each column.

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. x %&gt;%
  5. pivot_longer(cols = everything()) %&gt;%
  6. summarise(res = sum(value == &quot;yes&quot;), .by = name) %&gt;%
  7. ggplot(aes(x=&quot;&quot;, y=res, fill=name)) +
  8. geom_bar(stat=&quot;identity&quot;, width=1) +
  9. coord_polar(&quot;y&quot;, start=0) +
  10. geom_text(aes(label = res), position = position_stack(vjust = 0.5),
  11. color = &quot;white&quot;, size=6) +
  12. scale_fill_brewer(palette=&quot;Set1&quot;) +
  13. theme_void()

创建一个多列的饼图

答案2

得分: 1

Reshape from wide-to-long, keep only "yes" rows, then get counts and plot:

  1. d <- stack(x)
  2. tbl <- table(d[d$values == "yes", "ind"])
  3. pie(tbl, labels = paste(names(tbl), "-", round(prop.table(tbl) * 100), "%"))

创建一个多列的饼图

英文:

Reshape from wide-to-long, keep only "yes" rows, then get counts and plot:

  1. d &lt;- stack(x)
  2. tbl &lt;- table(d[ d$values == &quot;yes&quot;, &quot;ind&quot; ])
  3. pie(tbl, labels = paste(names(tbl), &quot;-&quot;,
  4. round(prop.table(tbl) * 100), &quot;%&quot;))

创建一个多列的饼图

huangapple
  • 本文由 发表于 2023年3月31日 17:28:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896861.html
匿名

发表评论

匿名网友

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

确定