从具有多个组变量的 summarySE 结果 table 创建条形图

Creating a bar plot from summarySE result table with multiple group variables

我从 summarySE 函数得到一个结果 table:

    a        b N               variable   sd    se   ci
 1234      foo 264                  2.0 0.87 0.053 0.11
 1234      bar 111                  3.6 1.35 0.128 0.25
 5678      foo 169                  1.8 1.02 0.079 0.16
 5678      bar 118                  1.6 1.13 0.104 0.21
91011      foo   9                  1.3 1.35 0.450 1.04
91011      bar 384                  1.0 1.12 0.057 0.11

我想创建一个条形图,其中每一行对应一个条,其高度为 variable – 所以我需要 stat="identity"。现在,通常我这样做没有问题:

column = "varaible"
ggplot(data, aes_string(y = column)) + geom_bar(stat = "identity")

但它失败了:

Error during wrapup: argument "env" is missing, with no default

当然可以,因为有多个列定义了 x 是什么。如果我这样做

ggplot(data, aes_string(x = "a", y = column)) + geom_bar(stat = "identity")

它适用于 a 的唯一值,但不考虑 b。如何将 ab 的组合视为 x 值?

你可以使用另一种美学,比如填充

ggplot(data, aes_string(x = "a", fill = "b", y = column)) + 
  geom_bar(stat = "identity") 

或连接两列

ggplot(transform(data, t = paste(a, b)), 
       aes_string(x = "t", y = column)) + 
  geom_bar(stat = "identity")