how to create box plot of precipitation data from raster stack

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

how to create box plot of precipitation data from raster stack

karsten-2
Hi All,
 
I am a beginner with R and spatial and working on plotting precipitation
data (from CHIRPS) using raster stacks for the purpose of agricultural site
characterization. I succeeded to generate plots for many research locations
using a csv file with x-y coordinates and generating a bar graph as a
'climate diagram' for a selected year of a raster stack (containing several
hundred *.tif files) with my r script.
Now I would like to add a box plot to the output of each diagram showing the
current years monthly rain data against the median, max mix of the entire 35
years time series as a backdrop (what I would like to add should look like
this http://www.terragis.net/docs/other/timeseries.png ). I am really
struggling on how to prepare or extract data from the tifs in order to
summarize them by month of the 35 years.
Currently I have one folder with 420+ tif flies with names in this format
es_af.1981.01.tif
es_af.1981.02.tif
es_af.1981.03.tif
es_af.1981.04.tif
es_af.1981.07.tif
es_af.1981.08.tif
es_af.1981.09.tif
es_af.1981.10.tif
es_af.1981.12.tif
es_af.1982.01.tif
and so on for 35 years
How would I approach (write my r script) in order to extract a data frame /
raster stack in order to output box plot diagrams as in the link above ?
In my existing script I was using these steps to get a list of all file
names and then creating individual stacks for each month , but what are my
next steps ?
 
# Use pattern arg to return a wildcard to get a list of all tifs in dir
alltiffs = list.files(getwd(), pattern="*\\.tif$", full.names=TRUE)
 
# get main data - e.g year 2016 to plot
climategrids =  alltiffs[grepl("*2016.*\\.tif$", alltiffs)]
 
## create list of all data files for all 35 years for each month
## then create stack for each month
month_1_climategrids =  alltiffs[grepl("*\\.01\\.tif$", alltiffs)]
month_1 <- stack(month_1_climategrids, quick=TRUE)
month_2_climategrids =  alltiffs[grepl("*\\.02\\.tif$", alltiffs)]
month_2 <- stack(month_2_climategrids, quick=TRUE)
 
and so on
 
Thanks
Karsten Vennemann
 <http://www.terragis.net> www.terragis.net
 

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
Reply | Threaded
Open this post in threaded view
|

Re: how to create box plot of precipitation data from raster stack

Michael Sumner
My short advice is to first treat all the files more abstractly, build a
data frame of the file name, and the full path to the file, and the date
associated with each (by string parse on the name).

Then you can check the set for sensibility, proper date, correct order,
sensible step between each. The full column of file paths can be given to
raster stack, and you can use setZ to apply the time layer reference in a
consistent way.

At another level up you might write functions that refer to you data frame,
and return layer/s or summaries as a function of date/season etc.

It helps to tidy up all the file stuff first, so it's a load of issues to
put behind you, and it's robust to future data coming in.

Cheers, Mike

On Sat, Mar 4, 2017, 14:26 karsten <[hidden email]> wrote:

> Hi All,
>
> I am a beginner with R and spatial and working on plotting precipitation
> data (from CHIRPS) using raster stacks for the purpose of agricultural site
> characterization. I succeeded to generate plots for many research locations
> using a csv file with x-y coordinates and generating a bar graph as a
> 'climate diagram' for a selected year of a raster stack (containing several
> hundred *.tif files) with my r script.
> Now I would like to add a box plot to the output of each diagram showing
> the
> current years monthly rain data against the median, max mix of the entire
> 35
> years time series as a backdrop (what I would like to add should look like
> this http://www.terragis.net/docs/other/timeseries.png ). I am really
> struggling on how to prepare or extract data from the tifs in order to
> summarize them by month of the 35 years.
> Currently I have one folder with 420+ tif flies with names in this format
> es_af.1981.01.tif
> es_af.1981.02.tif
> es_af.1981.03.tif
> es_af.1981.04.tif
> es_af.1981.07.tif
> es_af.1981.08.tif
> es_af.1981.09.tif
> es_af.1981.10.tif
> es_af.1981.12.tif
> es_af.1982.01.tif
> and so on for 35 years
> How would I approach (write my r script) in order to extract a data frame /
> raster stack in order to output box plot diagrams as in the link above ?
> In my existing script I was using these steps to get a list of all file
> names and then creating individual stacks for each month , but what are my
> next steps ?
>
> # Use pattern arg to return a wildcard to get a list of all tifs in dir
> alltiffs = list.files(getwd(), pattern="*\\.tif$", full.names=TRUE)
>
> # get main data - e.g year 2016 to plot
> climategrids =  alltiffs[grepl("*2016.*\\.tif$", alltiffs)]
>
> ## create list of all data files for all 35 years for each month
> ## then create stack for each month
> month_1_climategrids =  alltiffs[grepl("*\\.01\\.tif$", alltiffs)]
> month_1 <- stack(month_1_climategrids, quick=TRUE)
> month_2_climategrids =  alltiffs[grepl("*\\.02\\.tif$", alltiffs)]
> month_2 <- stack(month_2_climategrids, quick=TRUE)
>
> and so on
>
> Thanks
> Karsten Vennemann
>  <http://www.terragis.net> www.terragis.net
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>
--
Dr. Michael Sumner
Software and Database Engineer
Australian Antarctic Division
203 Channel Highway
Kingston Tasmania 7050 Australia

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology