使用ggplot创建多个变量的组合条形图

I am trying to create a frequency (in % terms) bar plot using the following data:

>fulldata
Type Category
Sal         0
Sal         0
Sal         1
Sal         0
Sal         1
Sal         1
Self        1
Self        0
Self        1
Self        0
Self        0

So, I am trying to create a bar plot (using ggplot) which shows both the % of Sal and Self in the fulldata and % of Sal and Self in the Category==1 side by side (with labels of % values). I tried creating a separate data frame by filtering Category==1 from the fulldata but they are getting overlapping over each other. I tried the following:

> Category1 = fulldata[which(fulldata$Category==1),]

ggplot(fulldata, aes(x=Type,y = (..count..)/sum(..count..)))+
    geom_bar()+
    geom_label(stat = "count", aes(label=round(..count../sum(..count..),3)*100), 
               vjust=1.2,size=3, format_string='{:.1f}%')+
    scale_y_continuous(labels = scales::percent)+
    labs(x = "Type", y="Percentage")+
    geom_bar(data = Category1, position = "dodge", color = "red")

*原始数据大约有80000行。

评论