在分组条形图中叠加的条形图

时间:2022-11-16 23:40:27

I have the following graph

我有下图

test <- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'year' = 2001:2005)
test$value <- floor((rnorm(nrow(test)))*100)
test$value[test$value < 0] <- 0

ggplot() +
  geom_bar(data=test, aes(y = value, x = year, fill = cat), stat="identity",position='dodge') +
  theme_bw()

在分组条形图中叠加的条形图 and I need to divide each 'cat' by 'cond'(true or false). How do I do that?

我需要把每只“猫”除以“cond”(真或假)。我该怎么做呢?

1 个解决方案

#1


18  

You can put cat on the x-axis and use facet_grid with year:

可以将cat放在x轴上,使用facet_grid with year:

ggplot() +
  geom_bar(data=test, aes(y = value, x = cat, fill = cond), stat="identity",
           position='stack') +
  theme_bw() + 
  facet_grid( ~ year)

在分组条形图中叠加的条形图

#1


18  

You can put cat on the x-axis and use facet_grid with year:

可以将cat放在x轴上,使用facet_grid with year:

ggplot() +
  geom_bar(data=test, aes(y = value, x = cat, fill = cond), stat="identity",
           position='stack') +
  theme_bw() + 
  facet_grid( ~ year)

在分组条形图中叠加的条形图