kay
kay

Reputation: 2011

Boxplot of table using ggplot2 - with specific colors

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

Upvotes: 0

Views: 217

Answers (1)

Yorgos
Yorgos

Reputation: 30485

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

Upvotes: 2

Related Questions