英文:
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 <- 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")
答案1
得分: 2
以下是代码的翻译部分:
- 使用
mutate
和paste0
为标签创建label_x
。 - 使用
parse_number()
(我最喜欢的函数之一)从百分比中提取数字。 - 使用
pivot_longer()
将数据转换为长格式(ggplot2首选的格式)。 - 使用
geom_text
将百分比数字添加到条形图上。 - 一些微调:将图例放在底部,移除x和y轴标签,以及空白背景。
请注意,这些翻译是关于代码部分的内容,不包括问题的回答。
英文:
Update: Code with some explanation (removed coord_flip) as suggested by @tjebo:
- We create a
label_x
for our labels withmutate
andpaste0
- Using
parse_number()
(my favorite function) we extract the number from xx%. - Bring data in long format with
pivot_longer()
(long format is the preferred format for ggplot2) - with geom_text we add the percent numbers to the bars.
- 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 %>%
mutate(label_x = paste0(PartnerName, " N=", Total),
across(c(Food, Machinery), parse_number)) %>%
arrange(Machinery) %>%
pivot_longer(c(Food, Machinery)) %>%
ggplot(aes(x=value, y=label_x, fill=name))+
geom_col(width = 0.5, position = position_stack())+
geom_text(aes(label = paste0(value, "%")), position = position_stack(0.5)) +
scale_fill_manual(values = c("orange", "steelblue"))+
labs(x="", fill="", y="")+
theme_classic()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom")
data:
structure(list(PartnerName = c("China", "Brazil", "Argentina",
"UE", "US"), Food = c("9%", "22%", "36%", "40%", "51"), Machinery = c("91%",
"78%", "64%", "60%", "49%"), Total = c(26, 28, 43, 34, 25)), class = "data.frame", row.names = c(NA,
-5L))
First answer:
Something like this:
Be careful China 91% and 12% are not 100%
proba %>%
mutate(label_x = paste0(PartnerName, " N=", Total),
across(c(Food, Machinery), parse_number)) %>%
arrange(Machinery) %>%
pivot_longer(c(Food, Machinery)) %>%
ggplot(aes(x=label_x, y=value, fill=name))+
geom_col(width = 0.5, position = position_stack())+
geom_text(aes(label = paste0(value, "%")), position = position_stack(0.5)) +
scale_fill_manual(values = c("orange", "steelblue"))+
labs(x="", fill="", y="")+
coord_flip()+
theme_classic()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
legend.position = "bottom")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论