我有这个数据库:

我试着做了一个geom_mosaic图。这是我的代码:
ggplot(data) +
geom_mosaic(aes(x = product(substanceabuse,probation),fill=substanceabuse))这就是结果:

如何添加从mosaicplot函数获得的“Yes”和“No”标签:mosaicplot graph
感谢大家的支持!
发布于 2020-09-09 20:02:39
看起来geom_mosaic()有一些bug。我建议使用这种风格的方法。也许对你有用:
library(ggplot2)
library(dplyr)
#Data
data <- data.frame(ClientId=1:6,
substanceabuse=rep(c('Yes','No'),each=3),
probation=c('No',rep('Yes',3),rep('No',2)),stringsAsFactors = F)
#Plot
data %>% group_by(substanceabuse,probation) %>%
summarise(count = n()) %>%
mutate(cut.count = sum(count),
prop = count/sum(count)) %>%
ungroup() %>%
ggplot(aes(x = substanceabuse, y = prop, width = cut.count, fill = probation)) +
geom_bar(stat = "identity", position = "fill", colour = "black") +
geom_text(aes(label = scales::percent(prop)), position = position_stack(vjust = 0.5)) + # if labels are desired
facet_grid(~substanceabuse, scales = "free_x", space = "free_x")+
theme(strip.background = element_blank(),
strip.text = element_blank())输出:

从某种意义上说,这与你想要的很接近。
https://stackoverflow.com/questions/63809436
复制相似问题