rggplot2geom-col

Why wouldn't my code stack bars on R ggplot geom_col?


I thought this was a simple syntax, but my code would not stack bars.

data.frame(x = c('a', 'b', 'c'),
           y = c(.549, .051, .4)) %>%
  ggplot(aes(x = x, y = y)) + 
  geom_col(position = 'stack')

I also tried position = position_stack(), but it still shows position dodge.

What am I doing wrong?


Solution

  • Try this instead:

    data.frame(x = c(1,1,1),
               grp = c('a', 'b', 'c'),
               y = c(.549, .051, .4)) %>%
      ggplot(aes(x = x, y = y,fill = grp)) + 
      geom_col(position = 'stack')
    

    Stacking requires a third variable beyond x & y.