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

25 views Asked by At

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?

1

There are 1 answers

0
joran On BEST ANSWER

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.