R: Multi-cell plots using columns from a data frame

I would like to build an individual graph for each unrelated column in the data frame. It seemed to me that I was on the right track with boxplot.matrix from the boxplot.matrix package, but it seems to do the same as boxplot(as.matrix(plotdata) , which should display everything in a common box with a common scale on the axis I want (say) 5 separate stories.

I could do it manually:

 par(mfrow=c(2,2)) boxplot(data$var1 boxplot(data$var2) boxplot(data$var3) boxplot(data$var4) 

But should there be a way to use the columns of a data frame?

EDIT: I used iterations, see my answer.

+7
source share
3 answers

I used iteration for this. I think that maybe I was not a clear initial question. Thanks for the answers nonetheless.

 par(mfrow=c(2,5)) for (i in 1:length(plotdata)) { boxplot(plotdata[,i], main=names(plotdata[i]), type="l") } 
+2
source

You can use the reshape package to simplify things.

 data <- data.frame(v1=rnorm(100),v2=rnorm(100),v3=rnorm(100), v4=rnorm(100)) library(reshape) meltData <- melt(data) boxplot(data=meltData, value~variable) 

or even then use the ggplot2 package to make things better

 library(ggplot2) p <- ggplot(meltData, aes(factor(variable), value)) p + geom_boxplot() + facet_wrap(~variable, scale="free") 
+10
source

From ?boxplot we see that we have the ability to transfer several data vectors as elements of a list, and we will get several boxes, one for each vector in our list.

So, all we need to do is convert the columns of our matrix to a list:

 m <- matrix(1:25,5,5) boxplot(x = as.list(as.data.frame(m))) 

If you really need separate panels with one drawer (although, to be honest, I donโ€™t understand why you want to do this), I will go to ggplot and cut instead:

 m1 <- melt(as.data.frame(m)) library(ggplot2) ggplot(m1,aes(x = variable,y = value)) + facet_wrap(~variable) + geom_boxplot() 
+7
source

All Articles