R 百分比叠加条形图

R 百分比叠加条形图,r,R,我想绘制一个带有质量百分比的堆叠条形图,以量化每种尺寸对我拥有的优质服装百分比的贡献。此处我的表格显示type=1,如果产品质量、尺寸和品牌仍然良好: sok <- structure(list(type = c(0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0 ), size = c("four", "four", "two", "three", "two", "two", "two", "three", "three", "four", "two", "tw

我想绘制一个带有质量百分比的堆叠条形图,以量化每种尺寸对我拥有的优质服装百分比的贡献。此处我的表格显示type=1,如果产品质量、尺寸和品牌仍然良好:

sok <- structure(list(type = c(0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0
), size = c("four", "four", "two", "three", "two", "two", "two", 
"three", "three", "four", "two", "two", "two"), brand = c("Armani", 
"Armani", "Armani", "Armani", "Armani", "Armani", "Armani", "Armani", 
"Armani", "Armani", "Armani", "Armani", "Armani")), row.names = c(NA, 
-13L), class = "data.frame")
具有以下特征:

这不是我想要的,因为我不想求和百分比,但我想画出我的优质阿玛尼服装的百分比(46%),用尺寸来填充它。我不是在寻找
position=position\u fill()
,因为它将以100%显示完整的条形图


有人能帮忙吗?

你可以使用
prop.table()

定义颜色,在本例中为三种

clr <- rainbow(nrow(armani.1), alpha=.7)
产量
这就是你要找的吗

library(tidyverse)
library(scales)

sok %>%
  group_by(brand,size) %>%
  summarise(size_per_brand = n(),
            quality = mean(type)) %>%
  group_by(brand) %>% 
  mutate(perc = quality / sum(quality)) %>% 
  ggplot(aes(x = brand, y = perc, fill = size)) +
  geom_col() +
  geom_text(aes(label = if_else(perc > 0, percent(perc, accuracy = 1), NA_character_)), position = position_stack(vjust = 0.5)) +
  scale_y_continuous(labels = function(x) percent(x, accuracy = 1))

您可以包括多个品牌吗?这可能有助于理解您的复杂性。如果你没有多个品牌,那么你就不需要按品牌进行分组。没错,复杂的是我与多个品牌合作,但我避免将它们包括在内,这是我能提出的最简单的问题。如果你不想将它们相加,你希望如何进行叠加?我不清楚这里期望的结果是什么。我想在吧台上展示46%的优质服装。堆叠应按尺寸显示优质衣服的百分比。一种分百分之四十六的感谢!但我只对prop.table(表(品牌,类型)的x=1感兴趣。我想精确地显示对应于大小(百分比)的子栏。很抱歉造成混淆
armani.1 <- as.matrix(armani[rownames(armani) == 1, ])
clr <- rainbow(nrow(armani.1), alpha=.7)
barplot(armani.1, ylim=c(0, 1), yaxt="n", col=clr,
        main="Good quality clothes by size")
axis(2, (0:10)/10, labels=FALSE)
mtext(paste0((0:5)*20, "%"), 2, 1, at=(0:5)*.2, las=2)
mtext("Armani", 1, 1, font=2)
legend("topright", rownames(Armani), title="Size", cex=.8, 
       pch=15, col=clr)
library(tidyverse)
library(scales)

sok %>%
  group_by(brand,size) %>%
  summarise(size_per_brand = n(),
            quality = mean(type)) %>%
  group_by(brand) %>% 
  mutate(perc = quality / sum(quality)) %>% 
  ggplot(aes(x = brand, y = perc, fill = size)) +
  geom_col() +
  geom_text(aes(label = if_else(perc > 0, percent(perc, accuracy = 1), NA_character_)), position = position_stack(vjust = 0.5)) +
  scale_y_continuous(labels = function(x) percent(x, accuracy = 1))