[R] Histogram plots in Lattice with spatialgrid dataframe data

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon Jul 10 04:16:23 CEST 2017

Glad you found an answer, though it looks more self-educational than efficient (see suggestions below). In the future, follow the recommendations of the Posting Guide: use plain text, and provide a reproducible example. Some elaborations on what "reproducible" means are [1][2][3]. One issue here was that you did not include sample data to work with (I have assumed below that ann_bias has no other columns than the biasNNNN columns, which is not the usual case).

There are a number of ways to achieve the reshaping of your ann_bias data frame that are less painful than your approach. For example, the base R "stack" function:

bias2 <- stack( ann_bias )
names( bias2 ) <- c( "bias", "year )
levels( bias2$year ) <- sub( "bias", "", levels( bias2$year ) )

Or... if you are willing to venture into the tidyverse...

bias3  <- (   ann_bias
         %>% gather( year, bias )
         %>% mutate( year = factor( sub( "bias", "", year ) ) )

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

[2] http://adv-r.had.co.nz/Reproducibility.html

[3] https://cran.r-project.org/web/packages/reprex/index.html

Sent from my phone. Please excuse my brevity.

On July 9, 2017 12:32:32 PM PDT, Thomas Adams <tea3rd at gmail.com> wrote:
>Hello all,
>After more digging I was able to find out how to do this. The answer
>from an example here:
>histogram(~ bias | year, data=bias)
>On Sun, Jul 9, 2017 at 11:21 AM, Thomas Adams <tea3rd at gmail.com> wrote:
>> Hi all,
>> I can not seem to get what I want using the Lattice package to
>generate an
>> array of histograms of
>> spatialgrid dataframe data.
>> I can use the sp package and spplot to generate an array of maps that
>> display an array of spatialgrid dataframe data -- that's good. I
>> spplot(ann_bias,xlim=c(1423987.5,2614612.5),ylim=c(-
>> OHRFC Stage-3/MPE Precipitation Estimate Bias with respect to
>PRISM\n1997 -
>> 2016")
>> Which works... I can also do histogram(ann_bias$bias1997), which
>> too. I have also created a 'time-series' of boxplots successfully
>> these data as well...
>> But if I try:
>> year<-c('1997','1998','1999','2000','2001','2002','2003','
>> 2004','2005','2006','2007','2008','2009','2010','2011','
>> 2012','2013','2014','2015','2016')
>> dat<-c(ann_bias$bias1997,ann_bias$bias1998,ann_bias$
>> bias1999,ann_bias$bias2000,ann_bias$bias2001,ann_bias$
>> bias2002,ann_bias$bias2003,ann_bias$bias2004,ann_bias$
>> bias2005,ann_bias$bias2006,ann_bias$bias2007,ann_bias$
>> bias2008,ann_bias$bias2009,ann_bias$bias2010,ann_bias$
>> bias2011,ann_bias$bias2012,ann_bias$bias2013,ann_bias$
>> bias2014,ann_bias$bias2015,ann_bias$bias2016)
>> > data<-data.frame(year=c(year),bias=c(dat))
>> > histogram(~ bias | year, data=data)
>> I get a lattice plot of histograms, where the years vary, but all the
>> histograms are identical, which I know they should not be. It seem
>that all
>> the data from the combined spatialgrid dataframes are being used and
>> repeated.
>> Obviously, I'm not constructing the data correctly. Can someone tell
>> what I doing wrong. I've poured over this for a solid day, now...
>> Regards,
>> Tom
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list