force column width for geom_col even if data are lacking for some dates

Hi everyone,
I'm not succeeding having the same column width for groups A and B in my plot.
OK, there's no year 2001 in group 'B'.
But I don't understand why the column width for this group are automatically increased.
How can I correct this ?


df1 <- data.frame(
  year = factor(2001:2004),
  group = rep("A", 4),
  value = c(50, 60, 100, 40)

df2 <- data.frame(
  year = factor(2002:2004),
  group = rep("B", 3),
  value = c(90, NA, 150)

rbind(df1, df2) |> 
  ggplot(aes(x = group, y = value, fill = year)) +
  geom_col(width = 0.8, position = "dodge2") +

Thanks for help.

rbind(df1, df2) |> 
  ggplot(aes(x = group, y = value, fill = year)) +
  geom_col(position = position_dodge2(preserve = "single",width = .8,padding=.1)) +

This lets you preserve by "single" rather than the default "total"

preserve Should dodging preserve the "total" width of all elements at a position, or the width of a "single" element?

Hi nirgrahamuk,

This is exactly what I was looking for.
Thanks a lot for your help.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.