ggplot2: side by side barplot with one bar stacked and the other not

后端 未结 2 1486
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 19:09

I am trying to create a barplot where for each category, two bars are plotted (side by side): one is for the \"total\", the other is stacked by subgroups. For example, in th

2条回答
  •  旧时难觅i
    2020-12-19 19:52

    You can also increase the width of the bars to better fit the figure. Try this:

     p = ggplot(df, aes(x=factor(names)), ) + 
      geom_bar(width=0.75,data=subset(df,num=="total"), aes(y=values), stat="identity",width=.5) +
      geom_bar( width=0.75, data=subset(df,num!="total"), aes(y=-values,fill=factor(num)), stat="identity",width=.5) 
    print(p)
    

    New plot

    EDIT:

    I think I misunderstood your question. Do you want the bars within one name to be side by side?

提交回复
热议问题