添加数值到堆叠条形图中

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

add values to a stacked barplot

问题

可以有人帮我将百分比添加到以下的条形图中吗?

library(ggplot2)
library(dplyr)
n <- 10000

test <- data.frame(value = sample(1:3, size = n, replace = TRUE),
                   grp = sample(c("A", "B", "C"), size = n, replace = TRUE),
                   item = sample(c("Item1", "Item2", "Item3", "Item4", "Item5", "Item6"), size = n, replace = TRUE)) %>%
  mutate(value = as.factor(value))

test %>%
  ggplot(aes(x = grp, fill = value, group = value)) + 
    geom_bar(position = 'fill') +
    facet_wrap(item ~., ncol = 2) +
    coord_flip() +
    scale_fill_manual(values = c("green", "yellow", "red"))
英文:

Can someone please help me with adding percentages to the following barplot?

library(ggplot2)
library(dplyr)
n &lt;- 10000

test &lt;- data.frame(value = sample(1:3, size = n, replace = TRUE),
                   grp = sample(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;), size = n, replace = TRUE),
                   item = sample(c(&quot;Item1&quot;, &quot;Item2&quot;, &quot;Item3&quot;, &quot;Item4&quot;, &quot;Item5&quot;, &quot;Item6&quot;), size = n, replace = TRUE)) %&gt;%
  mutate(value = as.factor(value))
                   
test %&gt;%
  ggplot(aes(x = grp, fill = value, group = value)) + 
    geom_bar(position = &#39;fill&#39;) +
    facet_wrap(item ~., ncol = 2) +
    coord_flip() +
    scale_fill_manual(values = c(&quot;green&quot;, &quot;yellow&quot;, &quot;red&quot;))

答案1

得分: 1

以下是您要翻译的内容:

"The linked answer is similar but only viable for 2 groups. Following the rabbit hole here is a solution for more than 2 from https://rstudio-pubs-static.s3.amazonaws.com/329677_8f579b9e46284caeb9d3a72b7fdb7ac3.html"

"Code from linked site."

英文:

The linked answer is similar but only viable for 2 groups. Following the rabbit hole here is a solution for more than 2 from https://rstudio-pubs-static.s3.amazonaws.com/329677_8f579b9e46284caeb9d3a72b7fdb7ac3.html

test %&gt;%
  ggplot(aes(x = grp, fill = value, group = value)) + 
  geom_bar(position = &#39;fill&#39;) +
  facet_wrap(item ~., ncol = 2) +
  coord_flip() +
  scale_fill_manual(values = c(&quot;green&quot;, &quot;yellow&quot;, &quot;red&quot;)) +
  geom_text(aes(label=scales::percent(after_stat(count)/sum(after_stat(count)))),
            stat=&#39;count&#39;,position=position_fill(vjust=0.5))

添加数值到堆叠条形图中

Update per comment to add to 100:

# create new data
percentData &lt;- test %&gt;% group_by(grp) %&gt;% count(value) %&gt;%
  mutate(ratio=scales::percent(n/sum(n)))
percentData


test %&gt;%
  ggplot(aes(x = grp, fill = value, group = value)) + 
  geom_bar(position = &#39;fill&#39;) +
  facet_wrap(item ~., ncol = 2) +
  coord_flip() +
  scale_fill_manual(values = c(&quot;green&quot;, &quot;yellow&quot;, &quot;red&quot;)) +
  geom_text(data=percentData, aes(y=n,label=ratio),
            position=position_fill(vjust=0.5))

Code from linked site.
添加数值到堆叠条形图中

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

发表评论

匿名网友

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

确定