21

I would like to plot an INDIVIDUAL box plot for each unrelated column in a data frame. I thought I was on the right track with boxplot.matrix from the sfsmsic package, but it seems to do the same as boxplot(as.matrix(plotdata) which is to plot everything in a shared boxplot with a shared scale on the axis. I want (say) 5 individual plots.

I could do this by hand like:

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

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

EDIT: I used iterations, see my answer.

3 Answers 3

26

You could 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 ggplot2 package to make things nicer

library(ggplot2)
p <- ggplot(meltData, aes(factor(variable), value)) 
p + geom_boxplot() + facet_wrap(~variable, scale="free")
Sign up to request clarification or add additional context in comments.

1 Comment

what is the difference between melt and stack ?
18

From ?boxplot we see that we have the option to pass multiple vectors of data as elements of a list, and we will get multiple boxplots, 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 want separate panels each with a single boxplot (although, frankly, I don't see why you would want to do that), I would instead turn to ggplot and faceting:

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

3 Comments

Using this still returns a single plot with multiple x categories. I'm trying to make seperate plots for each x category found in a matrix. par(mfrow=c(2,5)) boxplot(x = as.list(as.data.frame(plotdata))) is the code I used, where plotdata is a ...x9 double matrix.
I want separate plots because they are separate unrelated variables with varying scales. Plotted all together, one dominates the others and you get 8 squashed illegible plots and one big one. I want 9 separate plots which could easily be achieved given the code in my original question. However, I was hoping to speed things up by automating this process by getting R to do a box plot of each variable (column) in a data frame.
@gisol In that case, if you decide to use ggplot you'll probably want to use scales = "free_y" when faceting.
6

I used iteration to do this. I think perhaps I wasn't clear in the original question. Thanks for the responses none the less.

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.