table 使用 ggplot2 的箱线图 - 具有特定颜色

Boxplot of table using ggplot2 - with specific colors

我有一个跟进问题 this question 关于为 table 的每一列绘制箱线图。

我有一个类似于所示示例的 table,并且我为矩阵的每一列绘制了一个箱线图。最重要的是,我为每一列分配了组标签 例如:

Paratio  = grp1
ShapeIdx   = grp2
FracD    = grp2
NNDis    = grp2
Core = grp1

我想根据这些组为我的箱形图着色(而不是根据变量着色)。有人可以告诉我该怎么做吗?

谢谢 K

假设您的初始数据框是 dd

library(reshape2)
library(ggplot2)

dd1 = melt(dd)

dd1$group <- apply(data,1, function(y)
  switch(y[1],
         Paratio  = "grp1",
         ShapeIdx   = "grp2",
         FracD    = "grp2",
         NNDis    = "grp2",
         Core = "grp1"
  )  )

ggplot(data = dd1, aes(x=variable, y=value)) + geom_boxplot(aes(fill=group))