[R] Help! I couldn't put multiple qplot on the same page...
William Dunlap
wdunlap at tibco.com
Thu Dec 8 01:03:14 CET 2011
I've lost track of the changes you've done to your
code and your original code was not runnable. However,
I noticed it used the line
myplotlist[nCount]=tt
where myplotlist was a list. Try changing that to
myplotlist[[nCount]] <- tt
(the '=' is acceptable but the the '[' is wrong).
I made that change to the code you posted at "Wed Dec 7 23:32:08 CET 2011"
(in the R-help archives), along with some extra lines to generate data,
and it seemed to work. When using the original
myplotlist[nCount] <- tt I got one warning per plot
Warning messages:
1: In myplotlist[nCount] = tt :
number of items to replace is not a multiple of replacement length
2: In myplotlist[nCount] = tt :
number of items to replace is not a multiple of replacement length
3: In myplotlist[nCount] = tt :
number of items to replace is not a multiple of replacement length
4: In myplotlist[nCount] = tt :
number of items to replace is not a multiple of replacement length
5: In myplotlist[nCount] = tt :
number of items to replace is not a multiple of replacement length
6: In myplotlist[nCount] = tt :
number of items to replace is not a multiple of replacement length
and no plots were displayed.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
--- start of modified R script ---
my.multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots <- length(plots)
# Make the panel
plotCols <- cols # Number of columns of plots
plotRows <- ceiling(numPlots/plotCols) # Number of rows needed
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y) {
viewport(layout.pos.row = x, layout.pos.col = y)
}
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow <- ceiling(i/plotCols)
curCol <- (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
myyears <- 1996:2001
myplotlist <- vector('list', length(myyears))
nCount <- 0;
for (j in myyears)
{
print(j)
nCount <- nCount+1
#...
# prepare datasub and factorsub for different j...
datasub <- j + ((1:10)%%nCount)/12 # different shape of curve each year
factorsub <- paste(month.name[1:10], j)
#...
tmp <- data.frame(datasub,factorsub=factor(factorsub, levels=unique(factorsub)))
tt <- qplot(factorsub, datasub, data=tmp, geom = "boxplot", ylab='Chg', xlab='Hours', main=j)
myplotlist[[nCount]] <- tt
}
my.multiplot(plotlist=myplotlist,cols=3)
--- end of modified R script
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Michael
> Sent: Wednesday, December 07, 2011 3:30 PM
> To: Tengfei Yin
> Cc: r-help
> Subject: Re: [R] Help! I couldn't put multiple qplot on the same page...
>
> Still the same problem - in the final aggregated plots, all the titles
> are diffrent(which is good), but the curve shapes are the same...
>
> How could this happen?
>
> Is this a bug?
>
> On 12/7/11, Tengfei Yin <yintengfei at gmail.com> wrote:
> > library(gridExtra)
> > ?grid.arrange
> >
> > I found this function convenient to me, it could arrange multiple ggplot
> > object on the same view window
> > p1 <- qplot(...)
> > p2 <- qplot(...)
> > ....
> > grid.arrange(p1, p2, ..., nrow = 2)
> >
> > different from your design, but for the same purpose I guess.
> >
> > cheers
> >
> > Tengfei
> >
> > On Wed, Dec 7, 2011 at 5:08 PM, Michael <comtech.usa at gmail.com> wrote:
> >
> >> Thanks a lot!
> >>
> >> Now I can see the individual plot, but still not the
> >> originally-desired multi-plot-on-one-page?
> >>
> >> Any thoughts? Thanks again!
> >>
> >> On 12/7/11, Yihui Xie <xie at yihui.name> wrote:
> >> > This is probably one of the most frequently asked questions. The
> >> > answer is to print() the objects. See R FAQ 7.22.
> >> >
> >> >
> >> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f
> >> >
> >> > Regards,
> >> > Yihui
> >> > --
> >> > Yihui Xie <xieyihui at gmail.com>
> >> > Phone: 515-294-2465 Web: http://yihui.name
> >> > Department of Statistics, Iowa State University
> >> > 2215 Snedecor Hall, Ames, IA
> >> >
> >> >
> >> >
> >> > On Wed, Dec 7, 2011 at 4:38 PM, Michael <comtech.usa at gmail.com> wrote:
> >> >> I found that if I run each "qplot" manually it would plot out
> >> something...
> >> >>
> >> >> but if I put it into the loop,
> >> >>
> >> >> it just doesn't work at all - sometimes it refused to plot anything...
> >> >>
> >> >
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >
> >
> > --
> > Tengfei Yin
> > MCDB PhD student
> > 1620 Howe Hall, 2274,
> > Iowa State University
> > Ames, IA,50011-2274
> > Homepage: www.tengfei.name
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list