0

I have a follow up question to this question regarding plotting a boxplot for every column of a table.

I have a similar table like in the example shown, and I have plotted a box plot for every column of my matrix. On top of this, I have group labels assigned for each column eg:

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

I want to color my box plots based on these groups (instead of coloring based on variable). Could someone show me how to do that ?

Thanks K

Community
  • 1
  • 1
kay
  • 1,851
  • 3
  • 13
  • 14

1 Answers1

2

Assuming that your initial dataframe is 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))

enter image description here

gd047
  • 29,749
  • 18
  • 107
  • 146