[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...

library(dplyr)
library(tidyr)
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
>came
>from an example here:
>
>https://stackoverflow.com/questions/3541713/how-to-plot-two-histograms-together-in-r
>
>
>yr_1997<-data.frame(bias=ann_bias$bias1997)
>yr_1998<-data.frame(bias=ann_bias$bias1998)
>yr_1999<-data.frame(bias=ann_bias$bias1999)
>yr_2000<-data.frame(bias=ann_bias$bias2000)
>yr_2001<-data.frame(bias=ann_bias$bias2001)
>yr_2002<-data.frame(bias=ann_bias$bias2002)
>yr_2003<-data.frame(bias=ann_bias$bias2003)
>yr_2004<-data.frame(bias=ann_bias$bias2004)
>yr_2005<-data.frame(bias=ann_bias$bias2005)
>yr_2006<-data.frame(bias=ann_bias$bias2006)
>yr_2007<-data.frame(bias=ann_bias$bias2007)
>yr_2008<-data.frame(bias=ann_bias$bias2008)
>yr_2009<-data.frame(bias=ann_bias$bias2009)
>yr_2010<-data.frame(bias=ann_bias$bias2010)
>yr_2011<-data.frame(bias=ann_bias$bias2011)
>yr_2012<-data.frame(bias=ann_bias$bias2012)
>yr_2013<-data.frame(bias=ann_bias$bias2013)
>yr_2014<-data.frame(bias=ann_bias$bias2014)
>yr_2015<-data.frame(bias=ann_bias$bias2015)
>yr_2016<-data.frame(bias=ann_bias$bias2016)
>
>
>yr_1997$year<-'1997'
>yr_1998$year<-'1998'
>yr_1999$year<-'1999'
>yr_2000$year<-'2000'
>yr_2001$year<-'2001'
>yr_2002$year<-'2002'
>yr_2003$year<-'2003'
>yr_2004$year<-'2004'
>yr_2005$year<-'2005'
>yr_2006$year<-'2006'
>yr_2007$year<-'2007'
>yr_2008$year<-'2008'
>yr_2009$year<-'2009'
>yr_2010$year<-'2010'
>yr_2011$year<-'2011'
>yr_2012$year<-'2012'
>yr_2013$year<-'2013'
>yr_2014$year<-'2014'
>yr_2015$year<-'2015'
>yr_2016$year<-'2016'
>
>
>bias<-rbind(yr_1997,yr_1998,yr_1999,yr_2000,yr_2001,yr_2002,yr_2003,yr_2004,yr_2005,yr_2006,yr_2007,yr_2008,yr_2009,yr_2010,yr_2011,yr_2012,yr_2013,yr_2014,yr_2015,yr_2016)
>histogram(~ bias | year, data=bias)
>
>Cheers!
>Tom
>
>
>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
>have:
>>
>> spplot(ann_bias,xlim=c(1423987.5,2614612.5),ylim=c(-
>>
>5862637.5,-4624387.5),at=brks,col.regions=colp(length(brks)-1),main="NOAA/NWS
>> OHRFC Stage-3/MPE Precipitation Estimate Bias with respect to
>PRISM\n1997 -
>> 2016")
>>
>> Which works... I can also do histogram(ann_bias$bias1997), which
>works
>> too. I have also created a 'time-series' of boxplots successfully
>with
>> 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
>me
>> 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
>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