[R] ggplot, qplot in loop

hadley wickham h.wickham at gmail.com
Thu Sep 25 13:29:21 CEST 2008


On Thu, Sep 25, 2008 at 6: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?

Inside loops you need to print(myplot)

Hadley

-- 
http://had.co.nz/



More information about the R-help mailing list