R | geom_bar with both stacked and unstacked columns

136 Views Asked by At

I want to plot a stacked colum next to an unstacked column in a barplot (geom_bar). Using the following code both columns overlap:

Var1_1=c("B","B","B","B","B","C","C","C","C","C")
Var2_1=c("1","2","3","4","5","1","2","3","4","5")
Var3_1=c(-1,-2.5,-0.4,4,1,5,7,9,10,11)
DATA1<-data.frame(Var1_1,Var2_1,Var3_1)


Var1_2=c("A","A","A","A","A")
Var2_2=c("1","2","3","4","5")
Var3_2=c(9.9279354,10.3156576,6.5565236,15.6320412,11.4934097)
DATA2 <- data.frame(Var1_2,Var2_2,Var3_2)
    
    ggplot() + 
    geom_bar(aes(factor(Var2_1), Var3_1, fill =Var1_1),stat="identity", position = "stack",data=DATA1) + geom_bar(aes(factor(Var2_2),Var3_2),stat="identity",position="dodge",data=DATA2)+
        scale_fill_brewer(palette = "Pastel1")

Notice the first geom_bar is used for plotting the stacked columns and the second one for the unstacked columns.

How can I correct the code?

0

There are 0 best solutions below