1

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?

David E.S.
  • 87
  • 1
  • 7
  • 2
    I don't know how to solve your issue, but I do know that you don't want quotes around numbers like `"6.5565236"`--that will lead to them being categorical strings rather than numbers. `as.data.frame(cbind())` can also cause that issue, you'd be better off directly using `data.frame` directly, e.g., `DATA2 <- data.frame(Var1_2,Var2_2,Var3_2)`. – Gregor Thomas May 28 '21 at 19:17
  • Thanks for your appreciation! I'll implement it in the code :-) – David E.S. May 28 '21 at 19:21

0 Answers0