With the code below the labelling in facet bB is not correctly positioned.
The problem seems to originate from the fact that there is no position_dodge(preserve="single")
for geom_text
(correct?). I am aware that I could 'manually' add an empty dummy cell (filling y=0 in facet bB), but I was wondering whether there is any way to correct for it in ggplot directly?
v1 <- LETTERS[1:2]
v2 <- letters[1:2]
v3 <- c("x","y")
g <- expand.grid(v1,v2,v3)
val=c(sample(10,8))
df<- data.frame(g,val)
df<- df[-8,]
df %>% ggplot() +
geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3),
stat="identity",
position=position_dodge(preserve="single"))+
geom_text(aes(x=Var2, y=val+1, label=val, group=Var3),
position=position_dodge(width=1))+
facet_grid(Var1~Var2, scale="free_x")
Update/answer: using position_dodge2 alignes the bar with the labelling (however, the bar is the then centered and not aligned with the bars in the other facets).
df %>% ggplot() +
geom_bar(aes(x=Var2, y=val, fill=Var3, group=Var3), stat="identity",
position=position_dodge2(preserve="single"))+
geom_text(aes(x=Var2, y=val+1, label=val, group=Var3),
position=position_dodge2(width=1))+
facet_grid(Var1~Var2, scale="free_x")