To explore the cell values of raster layers several types of graphical summaries can be used, such as boxplots, and density plots. Scatter plots can be used to explore the relationships between the values of different raster layers.




EXAMPLES


Below examples of the generartion graphical summaries of the values and relationships between raster layers in R are presented. These examples are taken from the “Yasi Effects on Green Cover at Mission Beach” tutorial. Putting the code snippets in context, by looking at a broader section of the R script, could be benefitial. Boxes with grey background contain code snippets, and boxes with white background containt code (text) outputs.


We create boxplots and scatter plots (with loess) using the 'bwplot' and 'splom ' functions in the 'rasterVis' package. To create density plots we use two methods: (1) using the 'densityplot' function in the 'rasterVis' package, and (2) the 'ggplot' function in the 'ggplot2' package. The latter approach produce nicer graphs, but requires more elaborated coding. Finally, we use the 'grid.arrange' function in the 'gridExtra' package to plot multiple grobs (i.e. grid graphical objects) on a page (i.e. in a single graph).



Boxplots and scatter plots

The 'bwplot' and 'splom' functions require a RasterStack or RasterBrick object. In our example we already have a rasterBrick with raw (in %) values (i.e. for 2010-Winter and 2011-Winter). However, we need to create an analogous rasterBrick with standardised (i.e. in SDs) values.


#===================================================================================
# Visualise Change in SPGC between Winter 2010 and Winter 2011: Graph Raster Values
#===================================================================================
# We already have a rasterBrick with individuals raw (in %) values (i.e. for 2010-Winter and 2011-Winter).
# We need to create an analogous rasterBrick with standarised (i.e. in SDs) values.


# Create RasterBrick object with Standardised SPGC
# ------------------------------------------------
SPGC.StudyArea.Std2010q3_2011q3.rb = brick(normImage(SPGC.StudyArea.2010q3.rl), normImage(SPGC.StudyArea.2011q3.rl))
names(SPGC.StudyArea.Std2010q3_2011q3.rb) = c("SPGC.SA.Std2010q3", "SPGC.SA.Std2011q3")       
SPGC.StudyArea.Std2010q3_2011q3.rb


## class       : RasterBrick 
## dimensions  : 295, 206, 60770, 2  (nrow, ncol, ncell, nlayers)
## resolution  : 30, 30  (x, y)
## extent      : 1484025, 1490205, -1996985, -1988135  (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=aea +lat_1=-18 +lat_2=-36 +lat_0=0 +lon_0=132 +x_0=0 +y_0=0 +ellps=GRS80 +units=m +no_defs 
## data source : in memory
## names       : SPGC.SA.Std2010q3, SPGC.SA.Std2011q3 
## min values  :         -9.645614,        -10.521245 
## max values  :          3.048260,          2.860489



#----------------------------------------------------------------------------------
# Boxplots
#----------------------------------------------------------------------------------

# Change in 'Raw' SPGC
# --------------------
RawSPGC.IndvYrs.bwplot = bwplot(SPGC.StudyArea.2010q3_2011q3.rb, main="Raw SPGCs")

# Change in Standardised SPGC
# ---------------------------
StdSPGC.IndvYrs.bwplot = bwplot(SPGC.StudyArea.Std2010q3_2011q3.rb, main="Standardised SPGCs")


#----------------------------------------------------------------------------------
# Scatter plots with loess
#----------------------------------------------------------------------------------

# Change in 'Raw' SPGC
# --------------------
RawSPGC.IndvYrs.splot = splom(SPGC.StudyArea.2010q3_2011q3.rb, plot.loess=TRUE, xlab='')

# Change in Standardised SPGC
# ---------------------------
StdSPGC.IndvYrs.splot = splom(SPGC.StudyArea.Std2010q3_2011q3.rb, main="Standardised SPGCs", plot.loess=TRUE, xlab='')


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Combine the 4 Plots in a single Graph
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grid.arrange( RawSPGC.IndvYrs.bwplot, StdSPGC.IndvYrs.bwplot, 
              RawSPGC.IndvYrs.splot, StdSPGC.IndvYrs.splot,
              nrow=2, top="SPGC - Individuals Years" )




Density plots


Method 1: Using ‘densityplot’ from the ‘rasterVis’ package


#---------------------------------------------------------------------------------
# Density Plots
#---------------------------------------------------------------------------------

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Method 1: Using 'densityplot' from the package 'rasterVis`
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Change in 'Raw' SPGC
# --------------------
RawSPGC.IndvYrs.dp = densityplot(SPGC.StudyArea.2010q3_2011q3.rb, main="Individual Raw SPGCs")
RawSPGC.Diff.dp = densityplot(SPGC.StudyArea.Diffq3.rl, main="Change in Raw SPGCs (2010q3-2011q3)")

# Change in Standardised SPGC
# ---------------------------
StdSPGC.IndvYrs.dp = densityplot(SPGC.StudyArea.Std2010q3_2011q3.rb, main="Individual Standardised SPGCs")
StdSPGC.Diff.dp = densityplot(SPGC.StudyArea.StdDiffq3.rl, main="Change in Standardised SPGCs (2010q3-2011q3)")

# Combine 4 Plots in a Graph
# ~~~~~~~~~~~~~~~~~~~~~~~~~~
grid.arrange( RawSPGC.IndvYrs.dp, RawSPGC.Diff.dp, StdSPGC.IndvYrs.dp, StdSPGC.Diff.dp,
              nrow=2, top="SPGC: Individuals Years & Change" )



Method 2: Using ‘ggplot’ from the ‘ggplot2’ package

To create the the density plot using the 'ggplot' function, we first need to convert the raster cell values to a vector of values (i.e. a variable) using the 'values' function in the 'raster' package. Then we combine all variables into a data frame. Finally, convert the wide format data frame to long format (i.e. combining both year values in the same column with an additional column/variable containing the year-season), as this format is required in the arguments of the 'ggplot' function.


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Method 2: Using 'ggplot' from the package 'ggplot2`
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Create a DataFrame with the required values 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a DataFrame with the required values to pass to function 'ggplot' in the package 'ggplot2' 
# Required values: SPGCs for 2010q3, 2011q3, and their difference in both scales: Raw and Standardised.
RawSPGC.2010q3 = values(SPGC.StudyArea.2010q3.rl)
RawSPGC.2011q3 = values(SPGC.StudyArea.2011q3.rl)
RawSPGC.Diff = values(SPGC.StudyArea.Diffq3.rl)
StdSPGC.2010q3 = values(normImage(SPGC.StudyArea.2010q3.rl))
StdSPGC.2011q3 = values(normImage(SPGC.StudyArea.2011q3.rl))
StdSPGC.Diff = values(SPGC.StudyArea.StdDiffq3.rl)
SPGCs.df = data.frame( RawSPGC.2010q3, RawSPGC.2011q3, RawSPGC.Diff, 
                       StdSPGC.2010q3, StdSPGC.2011q3, StdSPGC.Diff )
#summary(SPGCs.df)
                               

# Change in 'Raw' SPGC
# --------------------

# Density plot of Raw SPGC values for Individual Years 
# ....................................................
# Subset Dataset and convert wide format to long format
RawSPGC.IndvYrs.dflf = melt(SPGCs.df[c("RawSPGC.2010q3","RawSPGC.2011q3")])
RawSPGC.IndvYrs.dp2 = ggplot(RawSPGC.IndvYrs.dflf, aes(x=value, fill=variable)) +
                      geom_density(alpha=0.25)
                   
# Density plot of the change in Raw SPGC values
# ....................................................
RawSPGC.Diff.dp2 = ggplot(SPGCs.df, aes(x=RawSPGC.Diff)) + 
                   geom_density(alpha=0.25,color="darkgreen", fill="lightgreen")
                   

# Change in Standardised SPGC
# ---------------------------

# Density plot of Raw SPGC values for Individual Years 
# ....................................................
# Subset Dataset and convert wide format to long format
StdSPGC.IndvYrs.dflf = melt(SPGCs.df[c("StdSPGC.2010q3","StdSPGC.2011q3")])
StdSPGC.IndvYrs.dp2 = ggplot(StdSPGC.IndvYrs.dflf, aes(x=value, fill=variable)) +
                       geom_density(alpha=0.25)
                   
# Density plot of the change in Raw SPGC values
# ....................................................
StdSPGC.Diff.dp2 = ggplot(SPGCs.df, aes(x=StdSPGC.Diff)) + 
                    geom_density(alpha=0.25,color="darkgreen", fill="lightgreen")

                    
# Combine the 4 Plots in a single Graph
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grid.arrange( RawSPGC.IndvYrs.dp2, RawSPGC.Diff.dp2, StdSPGC.IndvYrs.dp2,   StdSPGC.Diff.dp2,
              nrow=2, top="SPGC: Individuals Years & Change" )