0

I have this data set:

x <- sample(80)
matrix <- matrix(x, ncol=5, nrow=16)
matrix <- cbind(matrix, c(rep("group1",4), rep("group2",4), rep("group3",4), rep("group4",4)))
colnames(matrix) <- c(letters[1:5], "groups")
df <- as.data.frame(matrix)

I would like to be able to plot five boxplots, one for each column from a to e, using a for loop. I have tried this, but I cannot figure out how to make it work:

for(i in df[,1:5]) {
  p <- ggplot(df, aes(x=groups, y=as.numeric(as.character(i))))
  p +  geom_boxplot()
}
Sergio.pv
  • 1,380
  • 4
  • 14
  • 23

1 Answers1

3

I'm guessing here:

library(reshape)
ggplot(melt(df, id.vars="groups")) + geom_boxplot(aes(x=groups, y=as.numeric(value))) + facet_wrap(~variable)

gives

enter image description here

and

library(reshape)
ggplot(melt(df, id.vars="groups")) + geom_boxplot(aes(x=groups, y=as.numeric(value)), color=variable)

gives

enter image description here

Is any of that close to what you want?

martin
  • 3,149
  • 1
  • 24
  • 35