[R-sig-eco] how to create box plot of precipitation data from raster stack

karsten karsten at terragis.net
Sat Mar 4 04:25:48 CET 2017


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]]



More information about the R-sig-ecology mailing list