Skip to content

Commit

Permalink
differences for PR #398
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Mar 17, 2023
1 parent 2d21e0b commit f758edd
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 1,179 deletions.
121 changes: 66 additions & 55 deletions 13-plot-time-series-rasters-in-r.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,40 @@ source: Rmd

## Things You'll Need To Complete This Episode

See the [lesson homepage](.) for detailed information about the software,
data, and other prerequisites you will need to work through the examples in this episode.
See the [lesson homepage](.) for detailed information about the software, data,
and other prerequisites you will need to work through the examples in this
episode.


::::::::::::::::::::::::::::::::::::::::::::::::::

This episode covers how to customize your raster plots using the `ggplot2` package
in R to create publication-quality plots.
This episode covers how to customize your raster plots using the `ggplot2`
package in R to create publication-quality plots.

## Before and After

In [the previous episode](12-time-series-raster/), we learned how to plot multi-band
raster data in R using the `facet_wrap()` function. This created a separate panel in our plot
for each raster band. The plot we created together is shown below:
In [the previous episode](12-time-series-raster/), we learned how to plot
multi-band raster data in R using the `facet_wrap()` function. This created a
separate panel in our plot for each raster band. The plot we created together
is shown below:

<img src="fig/13-plot-time-series-rasters-in-r-rendered-levelplot-time-series-before-1.png" style="display: block; margin: auto;" />

Although this plot is informative, it isn't something we would expect to see in a journal publication. The x
and y-axis labels aren't informative. There is a lot of unnecessary gray background and the titles of
each panel don't clearly state that the number refers to the Julian day the data was collected. In this
episode, we will customize this plot above to produce a publication quality graphic. We will go through these steps iteratively. When we're done, we will have created the plot shown below.
Although this plot is informative, it isn't something we would expect to see in
a journal publication. The x and y-axis labels aren't informative. There is a
lot of unnecessary gray background and the titles of each panel don't clearly
state that the number refers to the Julian day the data was collected. In this
episode, we will customize this plot above to produce a publication quality
graphic. We will go through these steps iteratively. When we're done, we will
have created the plot shown below.

<img src="fig/13-plot-time-series-rasters-in-r-rendered-levelplot-time-series-after-1.png" style="display: block; margin: auto;" />

## Adjust the Plot Theme

The first thing we will do to our plot remove the x and y-axis labels and axis ticks, as these are
unnecessary and make our plot look messy. We can do this by setting the plot theme to `void`.
The first thing we will do to our plot remove the x and y-axis labels and axis
ticks, as these are unnecessary and make our plot look messy. We can do this by
setting the plot theme to `void`.


```r
Expand All @@ -68,13 +74,16 @@ ggplot() +

<img src="fig/13-plot-time-series-rasters-in-r-rendered-adjust-theme-1.png" style="display: block; margin: auto;" />

Next we will center our plot title and subtitle. We need to do this **after** the `theme_void()` layer,
because R interprets the `ggplot` layers in order. If we first tell R to center our plot title,
and then set the theme to `void`, any adjustments we've made to the plot theme will be over-written
by the `theme_void()` function. So first we make the theme `void` and then we center the title.
We center both the title and subtitle by using the `theme()` function and setting the `hjust`
parameter to 0.5. The `hjust` parameter stands for "horizontal justification" and takes any value between
0 and 1. A setting of 0 indicates left justification and a setting of 1 indicates right justification.
Next we will center our plot title and subtitle. We need to do this **after**
the `theme_void()` layer, because R interprets the `ggplot` layers in order. If
we first tell R to center our plot title, and then set the theme to `void`, any
adjustments we've made to the plot theme will be over-written by the
`theme_void()` function. So first we make the theme `void` and then we center
the title. We center both the title and subtitle by using the `theme()`
function and setting the `hjust` parameter to 0.5. The `hjust` parameter stands
for "horizontal justification" and takes any value between 0 and 1. A setting
of 0 indicates left justification and a setting of 1 indicates right
justification.


```r
Expand All @@ -93,9 +102,9 @@ ggplot() +

## Challenge

Change the plot title (but not the subtitle) to bold font. You can
(and should!) use the help menu in RStudio or any internet resources
to figure out how to change this setting.
Change the plot title (but not the subtitle) to bold font. You can (and
should!) use the help menu in RStudio or any internet resources to figure out
how to change this setting.

::::::::::::::: solution

Expand Down Expand Up @@ -127,12 +136,13 @@ ggplot() +
Next, let's adjust the color ramp used to render the rasters. First, we can
change the blue color ramp to a green one that is more visually suited to our
NDVI (greenness) data using the `colorRampPalette()` function in combination
with `colorBrewer` which requires loading the `RColorBrewer` library. Then we use `scale_fill_gradientn` to pass the list of
colours (here 20 different colours) to ggplot.
with `colorBrewer` which requires loading the `RColorBrewer` library. Then we
use `scale_fill_gradientn` to pass the list of colours (here 20 different
colours) to ggplot.

First we need to create a set of colors to use. We will select a set of
nine colors from the "YlGn" (yellow-green) color palette. This returns a
set of hex color codes:
First we need to create a set of colors to use. We will select a set of nine
colors from the "YlGn" (yellow-green) color palette. This returns a set of hex
color codes:


```r
Expand All @@ -154,9 +164,9 @@ green_colors <- brewer.pal(9, "YlGn") %>%
colorRampPalette()
```

We can
tell the `colorRampPalette()` function how many discrete colors within this color range to
create. In our case, we will use 20 colors when we plot our graphic.
We can tell the `colorRampPalette()` function how many discrete colors within
this color range to create. In our case, we will use 20 colors when we plot our
graphic.


```r
Expand All @@ -180,7 +190,8 @@ pixels that are more green have a higher NDVI value.

## Data Tip

For all of the `brewer.pal` ramp names see the [brewerpal page](https://www.datavis.ca/sasmac/brewerpal.html).
For all of the `brewer.pal` ramp names see the
[brewerpal page](https://www.datavis.ca/sasmac/brewerpal.html).


::::::::::::::::::::::::::::::::::::::::::::::::::
Expand All @@ -189,19 +200,19 @@ For all of the `brewer.pal` ramp names see the [brewerpal page](https://www.data

## Data Tip

Cynthia Brewer, the creator of
ColorBrewer, offers an online tool to help choose suitable color ramps, or to
create your own. [ColorBrewer 2.0; Color Advise for Cartography](https://colorbrewer2.org/)
Cynthia Brewer, the creator of ColorBrewer, offers an online tool to help
choose suitable color ramps, or to create your own.
[ColorBrewer 2.0; Color Advise for Cartography](https://colorbrewer2.org/)


::::::::::::::::::::::::::::::::::::::::::::::::::

## Refine Plot \& Tile Labels

Next, let's label each panel in our plot with the Julian day that the
raster data for that panel was collected. The current names come from the band
"layer names"" stored in the
`RasterStack` and the first part of each name is the Julian day.
Next, let's label each panel in our plot with the Julian day that the raster
data for that panel was collected. The current names come from the band "layer
names"" stored in the `RasterStack` and the first part of each name is the
Julian day.

To create a more meaningful label we can remove the "x" and replace it with
"day" using the `gsub()` function in R. The syntax is as follows:
Expand All @@ -225,9 +236,9 @@ names(NDVI_HARV_stack)
```

Now we will use the `gsub()` function to find the character string
"\_HARV\_ndvi\_crop" and replace it with a blank string (""). We will
assign this output to a new object (`raster_names`) and look
at that object to make sure our code is doing what we want it to.
"\_HARV\_ndvi\_crop" and replace it with a blank string (""). We will assign
this output to a new object (`raster_names`) and look at that object to make
sure our code is doing what we want it to.


```r
Expand Down Expand Up @@ -283,14 +294,15 @@ ggplot() +
## Change Layout of Panels

We can adjust the columns of our plot by setting the number of columns `ncol`
and the number of rows `nrow` in `facet_wrap`. Let's make our plot so that
it has a width of five panels.
and the number of rows `nrow` in `facet_wrap`. Let's make our plot so that it
has a width of five panels.


```r
ggplot() +
geom_raster(data = NDVI_HARV_stack_df , aes(x = x, y = y, fill = value)) +
facet_wrap(~variable, ncol = 5, labeller = labeller(variable = labels_names)) +
facet_wrap(~variable, ncol = 5,
labeller = labeller(variable = labels_names)) +
ggtitle("Landsat NDVI", subtitle = "NEON Harvard Forest") +
theme_void() +
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
Expand All @@ -306,18 +318,17 @@ Now we have a beautiful, publication quality plot!

## Challenge: Divergent Color Ramps

When we used the `gsub()` function to modify the tile labels we replaced the beginning of each
tile title with "Day". A more descriptive name could be "Julian Day". Update the
plot above with the following changes:
When we used the `gsub()` function to modify the tile labels we replaced the
beginning of each tile title with "Day". A more descriptive name could be
"Julian Day". Update the plot above with the following changes:

1. Label each tile "Julian Day" with the julian day value
following.
1. Label each tile "Julian Day" with the julian day value following.
2. Change the color ramp to a divergent brown to green color ramp.

**Questions:**
Does having a divergent color ramp represent the data
better than a sequential color ramp (like "YlGn")? Can you think of other data
sets where a divergent color ramp may be best?
Does having a divergent color ramp represent the data better than a sequential
color ramp (like "YlGn")? Can you think of other data sets where a divergent
color ramp may be best?

::::::::::::::: solution

Expand All @@ -343,8 +354,8 @@ ggplot() +
<img src="fig/13-plot-time-series-rasters-in-r-rendered-final-figure-1.png" style="display: block; margin: auto;" />

For NDVI data, the sequential color ramp is better than the divergent as it is
more akin to the process
of greening up, which starts off at one end and just keeps increasing.
more akin to the process of greening up, which starts off at one end and just
keeps increasing.



Expand Down
95 changes: 0 additions & 95 deletions config.yaml

This file was deleted.

Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified fig/dc-spatial-raster/GreennessOverTime.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified fig/dc-spatial-raster/RGBSTack_1.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified fig/dc-spatial-raster/UTM_zones_18-19.jpg
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified fig/dc-spatial-raster/spatial_extent.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 25 additions & 25 deletions md5sum.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
"file" "checksum" "built" "date"
"CODE_OF_CONDUCT.md" "c93c83c630db2fe2462240bf72552548" "site/built/CODE_OF_CONDUCT.md" "2023-03-10"
"LICENSE.md" "afaf427b4223952624dcb6d8ded53ec0" "site/built/LICENSE.md" "2023-03-10"
"about.md" "2ae9402fd9f37560bd85430a98bf4847" "site/built/about.md" "2023-03-10"
"config.yaml" "f0dcd1572840ebf581f25c7ffdcc64d0" "site/built/config.yaml" "2023-03-10"
"index.md" "9906c09342f273189e212812417598f3" "site/built/index.md" "2023-03-10"
"episodes/01-raster-structure.Rmd" "28f964e013567b3d721f4ef8e252f3e0" "site/built/01-raster-structure.md" "2023-03-10"
"episodes/02-raster-plot.Rmd" "3bc30659ae4d91c31a1175ff7127531a" "site/built/02-raster-plot.md" "2023-03-13"
"episodes/03-raster-reproject-in-r.Rmd" "33ba79931847b29cf3a4fe540ecfc9ec" "site/built/03-raster-reproject-in-r.md" "2023-03-13"
"episodes/04-raster-calculations-in-r.Rmd" "9cd7052c0eb42d448248863ad32c3a70" "site/built/04-raster-calculations-in-r.md" "2023-03-10"
"episodes/05-raster-multi-band-in-r.Rmd" "45ed132303337e833d006e385df08cce" "site/built/05-raster-multi-band-in-r.md" "2023-03-13"
"episodes/06-vector-open-shapefile-in-r.Rmd" "97dc525d8128e62d8d64ad731a67c28c" "site/built/06-vector-open-shapefile-in-r.md" "2023-03-10"
"episodes/07-vector-shapefile-attributes-in-r.Rmd" "fd6706b5f58308b1426993e672ea5cba" "site/built/07-vector-shapefile-attributes-in-r.md" "2023-03-10"
"episodes/08-vector-plot-shapefiles-custom-legend.Rmd" "86da76a98f58fa6d74d064fbfe4a554e" "site/built/08-vector-plot-shapefiles-custom-legend.md" "2023-03-10"
"episodes/09-vector-when-data-dont-line-up-crs.Rmd" "63642fe99ff9b37868140c75daacecf3" "site/built/09-vector-when-data-dont-line-up-crs.md" "2023-03-10"
"episodes/10-vector-csv-to-shapefile-in-r.Rmd" "ca63f43a907517ee786cec6c76150452" "site/built/10-vector-csv-to-shapefile-in-r.md" "2023-03-10"
"episodes/11-vector-raster-integration.Rmd" "271815a10c81fb075dc6c84f43d7590f" "site/built/11-vector-raster-integration.md" "2023-03-10"
"episodes/12-time-series-raster.Rmd" "426b8c0cc92621999c78edd78ec94c04" "site/built/12-time-series-raster.md" "2023-03-10"
"episodes/13-plot-time-series-rasters-in-r.Rmd" "b03ca21b7cab90e0079c79d5bc1e1b1c" "site/built/13-plot-time-series-rasters-in-r.md" "2023-03-10"
"episodes/14-extract-ndvi-from-rasters-in-r.Rmd" "66a6255a04821114c06152bdc0bb007c" "site/built/14-extract-ndvi-from-rasters-in-r.md" "2023-03-10"
"instructors/instructor-notes.md" "ee08a61434133d404ac4ac692c0f64f1" "site/built/instructor-notes.md" "2023-03-10"
"learners/discuss.md" "2758e2e5abd231d82d25c6453d8abbc6" "site/built/discuss.md" "2023-03-10"
"learners/reference.md" "4c14f896eebe1782c8c8476670475436" "site/built/reference.md" "2023-03-10"
"learners/setup.md" "fc2da78ac62877df7a54a69de2b833e8" "site/built/setup.md" "2023-03-10"
"profiles/learner-profiles.md" "60b93493cf1da06dfd63255d73854461" "site/built/learner-profiles.md" "2023-03-10"
"renv/profiles/lesson-requirements/renv.lock" "5d910e4302d952f38c6f6f9ec20aecad" "site/built/renv.lock" "2023-03-10"
"CODE_OF_CONDUCT.md" "c93c83c630db2fe2462240bf72552548" "site/built/CODE_OF_CONDUCT.md" "2023-03-17"
"LICENSE.md" "afaf427b4223952624dcb6d8ded53ec0" "site/built/LICENSE.md" "2023-03-17"
"about.md" "2ae9402fd9f37560bd85430a98bf4847" "site/built/about.md" "2023-03-17"
"config.yaml" "f0dcd1572840ebf581f25c7ffdcc64d0" "site/built/config.yaml" "2023-03-17"
"index.md" "9906c09342f273189e212812417598f3" "site/built/index.md" "2023-03-17"
"episodes/01-raster-structure.Rmd" "28f964e013567b3d721f4ef8e252f3e0" "site/built/01-raster-structure.md" "2023-03-17"
"episodes/02-raster-plot.Rmd" "3bc30659ae4d91c31a1175ff7127531a" "site/built/02-raster-plot.md" "2023-03-17"
"episodes/03-raster-reproject-in-r.Rmd" "33ba79931847b29cf3a4fe540ecfc9ec" "site/built/03-raster-reproject-in-r.md" "2023-03-17"
"episodes/04-raster-calculations-in-r.Rmd" "9cd7052c0eb42d448248863ad32c3a70" "site/built/04-raster-calculations-in-r.md" "2023-03-17"
"episodes/05-raster-multi-band-in-r.Rmd" "45ed132303337e833d006e385df08cce" "site/built/05-raster-multi-band-in-r.md" "2023-03-17"
"episodes/06-vector-open-shapefile-in-r.Rmd" "97dc525d8128e62d8d64ad731a67c28c" "site/built/06-vector-open-shapefile-in-r.md" "2023-03-17"
"episodes/07-vector-shapefile-attributes-in-r.Rmd" "fd6706b5f58308b1426993e672ea5cba" "site/built/07-vector-shapefile-attributes-in-r.md" "2023-03-17"
"episodes/08-vector-plot-shapefiles-custom-legend.Rmd" "86da76a98f58fa6d74d064fbfe4a554e" "site/built/08-vector-plot-shapefiles-custom-legend.md" "2023-03-17"
"episodes/09-vector-when-data-dont-line-up-crs.Rmd" "63642fe99ff9b37868140c75daacecf3" "site/built/09-vector-when-data-dont-line-up-crs.md" "2023-03-17"
"episodes/10-vector-csv-to-shapefile-in-r.Rmd" "ca63f43a907517ee786cec6c76150452" "site/built/10-vector-csv-to-shapefile-in-r.md" "2023-03-17"
"episodes/11-vector-raster-integration.Rmd" "271815a10c81fb075dc6c84f43d7590f" "site/built/11-vector-raster-integration.md" "2023-03-17"
"episodes/12-time-series-raster.Rmd" "426b8c0cc92621999c78edd78ec94c04" "site/built/12-time-series-raster.md" "2023-03-17"
"episodes/13-plot-time-series-rasters-in-r.Rmd" "8035b6301e382fbed8837a9fd4d8dc16" "site/built/13-plot-time-series-rasters-in-r.md" "2023-03-17"
"episodes/14-extract-ndvi-from-rasters-in-r.Rmd" "66a6255a04821114c06152bdc0bb007c" "site/built/14-extract-ndvi-from-rasters-in-r.md" "2023-03-17"
"instructors/instructor-notes.md" "ee08a61434133d404ac4ac692c0f64f1" "site/built/instructor-notes.md" "2023-03-17"
"learners/discuss.md" "2758e2e5abd231d82d25c6453d8abbc6" "site/built/discuss.md" "2023-03-17"
"learners/reference.md" "4c14f896eebe1782c8c8476670475436" "site/built/reference.md" "2023-03-17"
"learners/setup.md" "fc2da78ac62877df7a54a69de2b833e8" "site/built/setup.md" "2023-03-17"
"profiles/learner-profiles.md" "60b93493cf1da06dfd63255d73854461" "site/built/learner-profiles.md" "2023-03-17"
"renv/profiles/lesson-requirements/renv.lock" "37e0321143bd790f6c626bf57875b492" "site/built/renv.lock" "2023-03-17"
Loading

0 comments on commit f758edd

Please sign in to comment.