Using mtcars
, I am creating a bar graph for cyl
and fill gear
. There is no car with cyl==8 & gear==4. When plotted, I used position_dodge(preserve = "single")
to preserve the width of bars. Then, using geom_text
I am adding labels to bars.
library(ggplot)
library(dplyr)
df <- count(mtcars, cyl, gear)
ggplot(df, aes(x = factor(cyl), y = n, fill = factor(gear))) +
geom_col(position = position_dodge(preserve = "single")) +
geom_text(aes(label = n, y = n + .07), size = 5, vjust = 0,
position = position_dodge(width=.9))
Two problems come out:
- The text labels are off for the last group, that is cyl==8
- In the last group, there should be a gap between red and blue bars for the green color bar. Instead you see red and blue bars are side by side.