3

this may relevant to this question: format-internal-lines-of-a-stacked-geom-bar-ggplot, but I still can not solve it since I am still beginner. I have many stacked bar for this plot, here is just sample data. I just want to keep external border and erase internal border in the stacked bar. Please help me:

data.frame(var1=c(rep("A1",4), rep("A2",4), rep("A3", 4), rep("A4", 4), rep("A5", 4), rep("A6", 4)), 
          var2=c(rep("A2",4), rep("A3", 4), rep("A4", 4), rep("A5", 4), rep("A6", 4), rep("A7", 4)), 
          varb=c(rep(c("S", "T", "U", "N"), 6)), value=c(80, 0, 20, 0, 
          20,30,30,20,0,60,40,0,100,0,0,0,0,60,0,40,0,60,0,40))->test

ggplot(test, aes(var2, var1, fill = varb))+ 
  geom_tile(aes(x=var1, y=value, width = 0.9, height=0.7), position = "stack", 
  colour="green")+facet_grid(var2~., space="free",scales="free")+
  scale_fill_manual(values=c("#99d594", "#fc8d59", "#ffffbf", "grey60")) 

enter image description here

mjberlin15
  • 147
  • 1
  • 10

1 Answers1

2

Basically you need to add two shapes separately, one geom_tile without color and another geom_col with transparent fill.

And I believe there are some errors in your plot, e.g.: y axis should be value instead of var1.

ggplot(test, aes(x = var2, fill = varb))+ 
    geom_tile(aes(y = value, width = 0.9, height=0.7), position = "stack") +
    geom_col(aes(y = max(value)),position = "identity",alpha = 0,color = "black") +
    facet_grid(var2~., space="free",scales="free") +
    scale_fill_manual(values=c("#99d594", "#fc8d59", "#ffffbf", "grey60")) 

enter image description here

yusuzech
  • 5,896
  • 1
  • 18
  • 33