[R] ggplot, qplot in loop

Gabor Grothendieck ggrothendieck at gmail.com
Thu Sep 25 13:26:19 CEST 2008


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:
>
> 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
>
>
> --
> | Albin Blaschka, Mag.rer.nat.
> | Etrichstrasse 26, A-5020 Salzburg
> | * www.albinblaschka.info * www.thinkanimal.info *
> | - It's hard to live in the mountains, hard but not hopeless!
>
> ______________________________________________
> 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