Boundaries in a stacked histogram are overloaded with bars
I am trying to create a complex histogram where some of the bars get black board and others don't. For this I set the color depending on the selected variable. When true, the border is black, otherwise it is transparent.
Problem: when the first bar has a border, the right edge of the border is overlapped by the second band.
Here is my code and picture of the problem:
#Sample Data
Var1 <- rep(c("A1","A2"),4)
Var2 <- c("Q1","Q1","Q2","Q2","Q3","Q3","Q4","Q4")
Freq <- c(4,2,6,2,6,4,9,3)
choose <- c(F,F,T,F,F,T,F,T)
df <- as.data.frame(cbind(Var1,Var2, Freq,choose))
g<- ggplot(df, aes(x=factor(Var2), y=Freq))+
geom_bar(stat="identity", aes(fill = Var1, color = choose), size = 3) +
scale_color_manual(values = c('FALSE' = 'transparent', 'TRUE' = 'black'))+
coord_flip()
g
I tried to fix this by drawing the border after the bars with padding = NA, this does drawing over the bars but not in the correct position.
g<- ggplot(df, aes(x=factor(Var2), y=Freq))+
scale_color_manual(values = c('FALSE' = 'transparent', 'TRUE' = 'black'))+
geom_bar(stat="identity", aes(fill = Var1))+
geom_bar(stat="identity", aes(color = choose), fill = NA, size = 3)+
coord_flip()
g
Any ideas how to fix this?
+3
source to share
1 answer
Match Var1
with aesthetics group
to take things away in the second geom_bar
.
ggplot(df, aes(x=factor(Var2), y=Freq))+
scale_color_manual(values = c('FALSE' = 'transparent', 'TRUE' = 'black'))+
geom_bar(stat="identity", aes(fill = Var1))+
geom_bar(stat="identity", aes(color = choose, group = Var1), fill = NA, size = 3)+
coord_flip()
+2
source to share