[R] five multiple plots with ggplot

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Feb 7 20:09:33 CET 2016


Looks like the data frame needs to be reshaped before being given to 
ggplot.

"melt" function from the "reshape2" package, or "gather" function from 
"tidyr" package are good tools for this.

On Sun, 7 Feb 2016, Ulrik Stervbo wrote:

> Hi Stefano,
>
> you try to facet on 'variable' and 'scales' which aren't part of your
> data.frame as far as I can see. The error 'At least one layer must contain
> all variables used for facetting' also tells you this - that at least one
> of the variables you use for facetting are missing.
>
> Hope this helps,
> Ulrik
>
> On Sun, 7 Feb 2016 at 19:28 Stefano Sofia <stefano.sofia at regione.marche.it>
> wrote:
>
>> Dear list users,
>> I would like to have multiple plots in one page with ggplot, in particular
>> five graphs:
>> in the central plot the snow cover (hs) of two meteorological stations;
>> above wind direction and temperature of one of the two stations and below
>> wind direction and temperature of the other station.
>>
>> My data frame (df) is like
>>
>> date hs1 temp1 wind1 wind_dir1 pch_wind_dir1 color_wind_dir1 hs2 temp2
>> wind2 wind_dir2 pch_wind_dir2 color_wind_dir2
>> 2014-12-27 00:00:00 20 -4.2 6.8 NW 5 "blue" 40 -4.2 6.8 NW 5 "blue"
>> 2014-12-27 00:30:00 22 -4.3 4.3 NNE 5 "cyan" 42 -4.3 4.3 NNE 5 "cyan"
>> 2014-12-27 01:00:00 24 -4.1 5.7 NNW 10 "cyan" 44 -4.1 5.7 NNW 10 "cyan"
>> 2014-12-27 10:00:00 30 -2.6 7.2 NNW 10 "cyan" 44 -3.1 5.7 NNW 10 "cyan"
>> ...
>>
>> with 48 rows.
>>
>> The ylim of hs1 and hs2 should be fixed between 0 and 200cm, hs1 colored
>> with blue and the other with red;
>> wind1 and wind2 should take the pch and color described in each row.
>>
>> lapply(df, class) gives
>>
>> $date
>> [1] "POSIXct" "POSIXt"
>>
>> $hs1
>> [1] "numeric"
>>
>> $temp1
>> [1] "numeric"
>>
>> $wind1
>> [1] "numeric"
>>
>> and so on.
>>
>> I have been trying for a long time to do that starting from some examples
>> found in the web, with no success. The example code I started with is
>>
>> > ggplot(df, aes(date, hs1, ymin=0, ymax=200, colour = "blue")) +
>> scale_colour_identity() + xlim(trunc(df$date[1], "days"),
>> trunc(df$date[48], "days")) + facet_grid(variable ~ ., scales = "free",
>> as.table = FALSE) + theme_bw()
>> > f1 <- f + geom_step(subset = .(variable =="HS"))
>>
>> but I always get the following error:
>> Error in layout_base(data, rows, drop = drop) :
>> At least one layer must contain all variables used for facetting.
>>
>> Is the problem due to how date has been specified? I tried both with
>> > df$date <- strptime(df$olddata, format = "%Y-%m-%d-%H-%M")
>> and
>> > df$date <- as.POSIXct(df$olddata, format = "%Y-%m-%d-%H-%M", tz = "")
>> but the result is the same.
>>
>> Could somebody help me in that?
>>
>> Thank you for your attention and your help
>> Stefano
>>
>>
>> ________________________________
>>
>> AVVISO IMPORTANTE: Questo messaggio di posta elettronica pu? contenere
>> informazioni confidenziali, pertanto ? destinato solo a persone autorizzate
>> alla ricezione. I messaggi di posta elettronica per i client di Regione
>> Marche possono contenere informazioni confidenziali e con privilegi legali.
>> Se non si ? il destinatario specificato, non leggere, copiare, inoltrare o
>> archiviare questo messaggio. Se si ? ricevuto questo messaggio per errore,
>> inoltrarlo al mittente ed eliminarlo completamente dal sistema del proprio
>> computer. Ai sensi dell?art. 6 della DGR n. 1394/2008 si segnala che, in
>> caso di necessit? ed urgenza, la risposta al presente messaggio di posta
>> elettronica pu? essere visionata da persone estranee al destinatario.
>> IMPORTANT NOTICE: This e-mail message is intended to be received only by
>> persons entitled to receive the confidential information it may contain.
>> E-mail messages to clients of Regione Marche may contain information that
>> is confidential and legally privileged. Please do not read, copy, forward,
>> or store this message unless you are an intended recipient of it. If you
>> have received this message in error, please forward it to the sender and
>> delete it completely from your computer system.
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list