function for horizontal stack bar with ggplot

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

function for horizontal stack bar with ggplot

问题

我尝试创建一个水平堆叠条形图,但无法实现,还想使条形的对齐方式变得动态,可以是水平或垂直。

还想显示N和下面图片中显示的标签。

  1. proba <- data.frame(PartnerName = c("China", "Brazil", "Argentina", "UE", "US"), Food = c("12%", "22%", "36%", "40%", "51"), Machinery = c("91%", "78%", "64%", "60%", "49%"), Total = c(26, 28, 43, 34, 25))
  2. graph_data <- proba %>% select(-Total)
  3. graph_data %>%
  4. pivot_longer(-PartnerName) %>%
  5. ggplot(aes(x=PartnerName, y=value, fill=name))+
  6. geom_bar(width = 0.9, stat="identity", position = "stack")
英文:

I am trying to create a horizontal stack bar but unable to do that also want to make alignment of bars dynamic like (horizontal,vertical).

also want to display N with labels like showed in pic below.

  1. proba &lt;- data.frame(PartnerName = c(&quot;China&quot;, &quot;Brazil&quot;, &quot;Argentina&quot;, &quot;UE&quot;, &quot;US&quot;), Food = c(&quot;12%&quot;,&quot;22%&quot;,&quot;36%&quot;,&quot;40%&quot;,&quot;51&quot;), Machinery = c(&quot;91%&quot;,&quot;78%&quot;,&quot;64%&quot;,&quot;60%&quot;,&quot;49%&quot;), Total = c(26,28,43,34,25))
  2. graph_data &lt;- proba %&gt;% select(-Total)
  3. graph_data %&gt;%
  4. pivot_longer(-PartnerName) %&gt;%
  5. ggplot(aes(x=PartnerName, y=value, fill=name))+
  6. geom_bar(width = 0.9,stat=&quot;identity&quot;,position = &quot;stack&quot;)

答案1

得分: 2

以下是代码的翻译部分:

  1. 使用mutatepaste0为标签创建label_x
  2. 使用parse_number()(我最喜欢的函数之一)从百分比中提取数字。
  3. 使用pivot_longer()将数据转换为长格式(ggplot2首选的格式)。
  4. 使用geom_text将百分比数字添加到条形图上。
  5. 一些微调:将图例放在底部,移除x和y轴标签,以及空白背景。

请注意,这些翻译是关于代码部分的内容,不包括问题的回答。

英文:

Update: Code with some explanation (removed coord_flip) as suggested by @tjebo:

  1. We create a label_x for our labels with mutate and paste0
  2. Using parse_number() (my favorite function) we extract the number from xx%.
  3. Bring data in long format with pivot_longer() (long format is the preferred format for ggplot2)
  4. with geom_text we add the percent numbers to the bars.
  5. Some tweaking: legend to bottom, remove labels x and y axis, and blank background.
  1. library(dplyr)
  2. library(readr) # parse_number()
  3. library(ggplot2)
  4. library(tidyr) # pivot_longer()
  5. proba %&gt;%
  6. mutate(label_x = paste0(PartnerName, &quot; N=&quot;, Total),
  7. across(c(Food, Machinery), parse_number)) %&gt;%
  8. arrange(Machinery) %&gt;%
  9. pivot_longer(c(Food, Machinery)) %&gt;%
  10. ggplot(aes(x=value, y=label_x, fill=name))+
  11. geom_col(width = 0.5, position = position_stack())+
  12. geom_text(aes(label = paste0(value, &quot;%&quot;)), position = position_stack(0.5)) +
  13. scale_fill_manual(values = c(&quot;orange&quot;, &quot;steelblue&quot;))+
  14. labs(x=&quot;&quot;, fill=&quot;&quot;, y=&quot;&quot;)+
  15. theme_classic()+
  16. theme(axis.title.x=element_blank(),
  17. axis.text.x=element_blank(),
  18. axis.ticks.x=element_blank(),
  19. legend.position = &quot;bottom&quot;)

data:

  1. structure(list(PartnerName = c(&quot;China&quot;, &quot;Brazil&quot;, &quot;Argentina&quot;,
  2. &quot;UE&quot;, &quot;US&quot;), Food = c(&quot;9%&quot;, &quot;22%&quot;, &quot;36%&quot;, &quot;40%&quot;, &quot;51&quot;), Machinery = c(&quot;91%&quot;,
  3. &quot;78%&quot;, &quot;64%&quot;, &quot;60%&quot;, &quot;49%&quot;), Total = c(26, 28, 43, 34, 25)), class = &quot;data.frame&quot;, row.names = c(NA,
  4. -5L))

First answer:
Something like this:

Be careful China 91% and 12% are not 100%

  1. proba %&gt;%
  2. mutate(label_x = paste0(PartnerName, &quot; N=&quot;, Total),
  3. across(c(Food, Machinery), parse_number)) %&gt;%
  4. arrange(Machinery) %&gt;%
  5. pivot_longer(c(Food, Machinery)) %&gt;%
  6. ggplot(aes(x=label_x, y=value, fill=name))+
  7. geom_col(width = 0.5, position = position_stack())+
  8. geom_text(aes(label = paste0(value, &quot;%&quot;)), position = position_stack(0.5)) +
  9. scale_fill_manual(values = c(&quot;orange&quot;, &quot;steelblue&quot;))+
  10. labs(x=&quot;&quot;, fill=&quot;&quot;, y=&quot;&quot;)+
  11. coord_flip()+
  12. theme_classic()+
  13. theme(axis.title.x=element_blank(),
  14. axis.text.x=element_blank(),
  15. axis.ticks.x=element_blank(),
  16. legend.position = &quot;bottom&quot;)

function for horizontal stack bar with ggplot

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

发表评论

匿名网友

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

确定