Drawing a series of ggplots in multiple windows

1,409 views
Skip to first unread message

MichaelH

unread,
Nov 16, 2009, 10:37:25 PM11/16/09
to ggplot2
Hi all,

I am using ggplot to examine variable distributions per subject in a
dataset containing ~70 people. Using geom_boxplot with flipped
coordinates and 70 folks makes for very tiny boxplots, so my thought
was to break things down into graphs of 10 people per graph, creating
a new graphics window for each plot. But when I call dev.new and
ggplot within a loop, nothing is graphed on R 2.10.0 on Windows 7.
Here is a minimally reproducible example:

exampleData <- data.frame(id=rep(1:30, each=24), congruence=rep(rep
(1:3, each=8), 30), reactionTime=rnorm(720, mean=10, sd=2))
numplots <- ceiling(length(unique(exampleData$id))/10)
idList <- unique(exampleData$id)
for (i in 1:numplots) {
workingIds <- idList[((i-1)*10+1):min((i*10), length(idList))]
workingSubset <- exampleData[exampleData$id %in% workingIds,]
dev.new()
ggplot(workingSubset, aes(x=factor(id), y=reactionTime)) +
geom_boxplot() + coord_flip() + facet_grid(. ~ congruence)
# plot(workingSubset$id, workingSubset$reactionTime)
}

Note that the ggplot command works perfectly when executed outside of
the loop. And when I use a base graphics function, such as the plot
command commented out above, I also receive one plot per window, as
expected. So my hunch is that ggplot is failing to link with the
graphics device I've created, but I'm not sure how to fix this
problem.

Thanks for your help!

Michael

Raivo Kolde

unread,
Nov 17, 2009, 3:58:52 AM11/17/09
to MichaelH, ggplot2
You should apply print to your ggplot. For example this works:

exampleData <- data.frame(id=rep(1:30, each=24), congruence=rep(rep
(1:3, each=8), 30), reactionTime=rnorm(720, mean=10, sd=2))
numplots <- ceiling(length(unique(exampleData$id))/10)
idList <- unique(exampleData$id)
for (i in 1:numplots) {
workingIds <- idList[((i-1)*10+1):min((i*10), length(idList))]
workingSubset <- exampleData[exampleData$id %in% workingIds,]
dev.new()
print(ggplot(workingSubset, aes(x=factor(id), y=reactionTime)) +
geom_boxplot() + coord_flip() + facet_grid(. ~ congruence))
}

2009/11/17 MichaelH <michael....@gmail.com>:
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> To post to this group, send email to ggp...@googlegroups.com
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/ggplot2

MichaelH

unread,
Nov 17, 2009, 9:24:28 AM11/17/09
to ggplot2
Raivo,

Thanks for that tip! I knew there must be a simple way to accomplish
this... It works quite well now. FYI, if others are interested in
accomplishing a similar task (boxplots per person), I've included the
function below.

Michael

-----
iBoxplots <- function(dataset, dv, idvar, numPerPlot=10, facetby=NULL,
pdfFile=NULL, useLattice=FALSE) {
require(lattice)
require(ggplot2)
noMissing <- na.omit(dataset)
#plot numPerPlot at a time
numplots <- ceiling(length(unique(noMissing[[idvar]]))/numPerPlot)
idList <- unique(noMissing[[idvar]])

#iterate through each plot
if (!missing(pdfFile) && is.character(pdfFile)) pdf(file=pdfFile)
for (i in 1:numplots) {
workingIds <- idList[((i-1)*numPerPlot+1):min((i*numPerPlot),
length(idList))]
workingSubset <- noMissing[noMissing[[idvar]] %in% workingIds,]
#ensure that id is coded as a factor (trying to do this on the
whole dataset once doesn't make sense because factor levels change per
subset)
workingSubset[[idvar]] <- factor(workingSubset[[idvar]])

#only open a new window if we aren't printing to file
if (missing(pdfFile)) dev.new()

if (missing(facetby)) {
if (useLattice) {
latFormula <- as.formula(paste(idvar, "~", dv))
print(bwplot(latFormula, workingSubset))
}
else {
print(ggplot(workingSubset, aes_string(x=idvar, y=dv)) +
geom_boxplot() + coord_flip())
}
}
else {
#determine the number of levels of the facetby var, then plot
across columns (could get ugly)
dataset[[facetby]] <- factor(dataset[[facetby]])
numlevels <- length(levels(dataset[[facetby]]))
if (useLattice) {
latFormula <- as.formula(paste(idvar, "~", dv, "|", facetby))
print(bwplot(latFormula, workingSubset, layout=c(numlevels,
1)))
}
else {
facetformula <- as.formula(paste(". ~ ", facetby))
print(ggplot(workingSubset, aes_string(x=idvar, y=dv)) +
geom_boxplot() + coord_flip() + facet_grid(facetformula))
}
}
}
if (!missing(pdfFile) && is.character(pdfFile)) dev.off()
}
#example call: iBoxplots(exampleData, "reactionTime", "id",
numPerPlot=20, facetby="congruence", pdfFile="indivBoxplots.pdf")
Reply all
Reply to author
Forward
0 new messages