function for horizontal stack bar with ggplot

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

function for horizontal stack bar with ggplot

问题

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

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

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))
graph_data <- proba %>% select(-Total)
graph_data %>%
  pivot_longer(-PartnerName) %>%
  ggplot(aes(x=PartnerName, y=value, fill=name))+
  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.

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))
graph_data &lt;- proba %&gt;% select(-Total)
graph_data %&gt;%  
  pivot_longer(-PartnerName)  %&gt;% 
  ggplot(aes(x=PartnerName, y=value, fill=name))+
  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.
library(dplyr)
library(readr) # parse_number()
library(ggplot2)
library(tidyr) # pivot_longer()
proba %&gt;%  
  mutate(label_x = paste0(PartnerName, &quot; N=&quot;, Total),
         across(c(Food, Machinery), parse_number)) %&gt;% 
  arrange(Machinery) %&gt;% 
  pivot_longer(c(Food, Machinery)) %&gt;% 
  ggplot(aes(x=value, y=label_x, fill=name))+
  geom_col(width = 0.5, position = position_stack())+
  geom_text(aes(label = paste0(value, &quot;%&quot;)), position = position_stack(0.5)) +
  scale_fill_manual(values = c(&quot;orange&quot;, &quot;steelblue&quot;))+
  labs(x=&quot;&quot;, fill=&quot;&quot;, y=&quot;&quot;)+
  theme_classic()+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        legend.position = &quot;bottom&quot;)

data:

structure(list(PartnerName = c(&quot;China&quot;, &quot;Brazil&quot;, &quot;Argentina&quot;, 
&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;, 
&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, 
-5L))

First answer:
Something like this:

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

proba %&gt;%  
  mutate(label_x = paste0(PartnerName, &quot; N=&quot;, Total),
         across(c(Food, Machinery), parse_number)) %&gt;% 
  arrange(Machinery) %&gt;% 
  pivot_longer(c(Food, Machinery)) %&gt;% 
  ggplot(aes(x=label_x, y=value, fill=name))+
  geom_col(width = 0.5, position = position_stack())+
  geom_text(aes(label = paste0(value, &quot;%&quot;)), position = position_stack(0.5)) +
  scale_fill_manual(values = c(&quot;orange&quot;, &quot;steelblue&quot;))+
  labs(x=&quot;&quot;, fill=&quot;&quot;, y=&quot;&quot;)+
  coord_flip()+
  theme_classic()+
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        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-2.html
匿名

发表评论

匿名网友

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

确定