分组条形图中的堆积条形图

dmv*_*nna 15 r ggplot2

我有以下图表

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()
Run Code Online (Sandbox Code Playgroud)

测试 我需要将每个'猫'除以'cond'(真或假).我怎么做?

Sve*_*ein 21

你可以把cat在x轴和使用facet_gridyear:

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

在此输入图像描述