显示百分比和值的堆栈条

Stack bars with percentages and values shown

这是我的数据框 - data_long1

 data.frame(
       value = c(88, 22, 100, 12, 55, 17, 10, 2, 2),
     Subtype = as.factor(c("lung","prostate",
                           "oesophagus","lung","prostate","oesophagus","lung",
                           "prostate","oesophagus")),
    variable = as.factor(c("alive","alive",
                           "alive","dead","dead","dead","uncertain","uncertain",
                           "uncertain"))
)

下面的代码给出了我想要的漂亮图表,显示了所有值,但 none 百分比。

ggplot(data_long1, aes(x = Subtype, y = value, fill = variable)) + geom_bar(stat = "identity") + 
geom_text(aes(label= value), size = 3, hjust = 0.1, vjust = 2, position = "stack")

我要找的是一个堆叠条形图,Y 轴上显示的是实际值而不是百分比(如上图),但实际条形图的每个子部分也显示百分比数字。我尝试了这段代码,得到了一个毫无意义的图表,每个堆栈都是 33.3%。

data_long1 %>% count(Subtype, variable) %>% group_by(Subtype) %>% mutate(pct= prop.table(n) * 100) %>% ggplot() + aes(x = Subtype, y = variable, fill=variable) + 
geom_bar(stat="identity") + ylab("Number of Patients") + 
geom_text(aes(label=paste0(sprintf("%1.1f", pct),"%")), position=position_stack(vjust=0.5)) + ggtitle("My Tumour Sites") + theme_bw()  

我似乎找不到使用 mutate 函数解决此问题的方法。请帮忙。

我会 pre-compute 你想要的摘要。这是每个子类型中的比例:

data_long2 <- data_long1 %>% 
  group_by(Subtype) %>% 
  mutate(proportion = value / sum(value))

ggplot(data_long2, aes(x = Subtype, y = value, fill = variable)) + 
  geom_bar(stat = "identity") + 
  geom_text(aes(label= sprintf('%0.0f%%', proportion * 100)), size = 3, hjust = 0.1, vjust = 2, position = "stack")

您还可以简单地通过删除 group_by 语句来获得所有组和类型的比例:

data_long2 <- data_long1 %>% 
  mutate(proportion = value / sum(value))

ggplot(data_long2, aes(x = Subtype, y = value, fill = variable)) + 
  geom_bar(stat = "identity") + 
  geom_text(aes(label= sprintf('%0.0f%%', proportion * 100)), size = 3, hjust = 0.1, vjust = 2, position = "stack")