簡體   English   中英

如何在 ggplot2 中的堆積條形圖類別之間引入分區?

[英]How can I introduce a partition between the categories of a stacked bar chart in ggplot2?

當前的:

在此處輸入圖像描述

需要:紅色和藍色類別之間的空白

在此處輸入圖像描述

數據:

在此處輸入圖像描述

除了 @AllanCameron 和 @eipi10 的精彩評論之外,這里是您想要的代碼:

#Data
df <- data.frame(class=c('1st class','1st class','2nd class','2nd class','3rd class','3rd class'),
                 sex=rep(c('male','female'),3),
                 n=c(179,143,172,107,499,212))
#Code
ggplot(df,aes(x=class,y=n,fill=sex,label=n))+
  geom_bar(stat='identity',color='white',size=1)+
geom_text(color='white',position=position_stack(vjust=0.5),fontface='bold')+
  scale_fill_manual(values=c("#cb5e5f", "#0072b1")) +
  theme_classic() + theme(legend.position = "b")

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM