Skip to content

ggplot, qplot in loop

3 messages · Albin Blaschka, Gabor Grothendieck, Hadley Wickham

#
Dear List,

yes, me again trying to work with qplot ;-)

I would like to make several single plots within a loop, like this 
(simplified and so on...):

trials <- c("A","B","C")
mycolours <- ("wheat","darkolivegreen","lightgreen",
	"khaki","darkseagreen","orange","chocolate4","gray75")


for (i in 1:length(trials)) {

     sql <- paste("select trial, cover_of, dateofsurvey, cover
		from mytable where trial = '",trials[i],"'
     mydata <- (sqlQuery(channel, sql))
     mytitle <- paste("Development of Cover, ", trials[i],",sep = "")
     myplot <- qplot(dateofsurvey, cover, data=mydata, geom="area",
  		fill=cover_of, main = mytitle,
		ylab="projective Cover[%]", xlab = "Year")
     myplot <- myplot + scale_fill_manual(values = mycolours)
}

If I have no loop, everything works, with the loop, no graphics device 
is openend, nothing happens, but the variables are filled with data 
(checked on the commandline), the SQL is executed, the result from the 
database is available, but nothing gets produced.

If I execute all variants by hand (all different sql-statements), the 
respective graphic is produced, but put together in the loop, it does not...

OS: WinXP, R version 2.7.0 (2008-04-22)

What do I have missed?

Thank you in advance,
Albin
#
Its a FAQ:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

although the FAQ should probably be reworded to mention that it applies
to all ggplot2 as well as lattice.

On Thu, Sep 25, 2008 at 7:17 AM, Albin Blaschka
<albin.blaschka at gmail.com> wrote:
#
On Thu, Sep 25, 2008 at 6:17 AM, Albin Blaschka
<albin.blaschka at gmail.com> wrote:
Inside loops you need to print(myplot)

Hadley