使用ggplot2
和tidyverse
函数尝试这种方法。正如@r2evans提到的,下次请尝试使用数据创建一个可重现的示例。这里是代码。您需要计算标签的位置,然后绘制代码:
library(ggplot2)
library(dplyr)
library(tidyr)
#Code
df %>% pivot_longer(-Race) %>%
group_by(name) %>% mutate(Pos=value/sum(value)) %>%
ggplot(aes(x=name,y=value,fill=Race))+
geom_bar(stat = 'identity',position = 'fill')+
geom_text(aes(y=Pos,label=value),position = position_stack(0.5))+
scale_y_continuous(labels = scales::percent)
输出:
使用的一些数据:
#Data
df <- structure(list(Race = c("Caucasian/White", "African American",
"Asian", "Other"), `Clinic A` = c(374, 820, 31, 108), `Clinic B` = c(291,
311, 5, 15), `Clinic C` = c(330, 206, 6, 5), `Clinic D` = c(950,
341, 6, 13)), class = "data.frame", row.names = c(NA, -4L))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句