The *.R
and *.Rmd
scripts on this page
illustrate a range of possible approaches for building a “project”
*.html
document. The examples range from a very simple R
Markdown Notebook to a multi-page website. This page focues on the
scripts and not the procedures for constructing a GitHub repository with
a GitHub Pages web page, nor for serving web pages on
https://pages.uoregon.edu
/.
The examples can be replicated by copying the scripts, and creating
the appropriate files in RStudio (e.g. as
alpha_plot_RScript.R
for this first example).
Important: When replicating Example 5 (a multi-page
*.html
document), the file _site.yml
must be
present in the R working directory. This file describes the “navbar”
(navigation bar) and the layout of the page. If it is present, it will
influence the other examples. So, for example, if _site.yml
is present, it will try to add a navbar to the *.html
file
created by, say, Example 4, which has its own formating information this
will produce unexpected results. To repeat, _site.yml
should be in the working directory only for Example 5, and
should not be in the working directory for the other examples.
(Note, instead of adding and deleting _site.yml
, just
rename it: not_site.yml
.)
This is an example of “bare” code that reads a netCDF file, does some
setup, and plots a map. The output could conceivably be collected from
the Console and Plot windows, and used to make an *.html
file, but, that’s what computers are for. This would involve:
*.md
file, perhaps with VS Code,alpha.png
and then including it in the Markdown file using
the code fragment ![](alpha.png)
), and then finally*.md
file as an *.html
file,
or using Pandoc to render the *.md
file.That would be tedious.
It would still be worth running the script, chunk-by-chunk, to get an idea of what the basic task of inputing data, setting up, and plotting is.
Note: Copy everything in the boxes (except the first line if it’s blank).
# alpha_plot_RScript.R
# reads and plots "alpha" (Priestley-Taylor coefficient) (AE/PE)
# load libraries
library(maps)
library(sf)
library(stars)
library(ggplot2)
# get a world outline object for plotting
# world_sf
world_sf <- st_as_sf(maps::map("world", plot = FALSE, fill = TRUE))
world_otl_sf <- st_geometry(world_sf)
# ggplot map of world_outline
ggplot() +
geom_sf(data = world_otl_sf, fill = NA, col = "black") +
scale_x_continuous(breaks = seq(-180, 180, 30)) +
scale_y_continuous(breaks = seq(-90, 90, 30)) +
coord_sf(xlim = c(-180, +180), ylim = c(-90, 90), expand = FALSE) +
theme_bw()
# read alpha from a netCDF file of bioclimatic variables using stars
# change path as necessary
nc_path <- "/Users/bartlein/Projects/RESS/data/nc_files/"
nc_name <- "cru10min30_bio.nc"
nc_file <- paste(nc_path, nc_name, sep="")
alpha <- read_ncdf(nc_file, var="alpha", proxy = FALSE)
# list alpha
alpha
# convert stars to sf
alpha_sf <- st_as_sf(alpha, as_points = TRUE)
alpha_sf
plot(alpha_sf, pch = 16, cex = 0.3)
# setup for plotting alpha
# make a data.frame
lon <- st_coordinates(alpha_sf)[,1]
lat <- st_coordinates(alpha_sf)[,2]
alpha <- as.vector(alpha_sf)
alpha_df <- data.frame(lon, lat, alpha)
dim(alpha_df)
head(alpha_df)
# set axis labels (breaks)
breaks_x <- c(seq(-180, 180, by = 60))
breaks_y <- c(seq(-90, 90, by = 30))
labels_x <- as.character(breaks_x)
labels_y <- as.character(breaks_y)
# plot alpha
# ggplot2 map of alpha
ggplot() +
geom_tile(data = alpha_df, aes(x = lon, y = lat, fill = alpha)) +
scale_fill_gradient2(low = "brown", mid="white", high = "darkgreen", midpoint = 0.50) +
geom_sf(data = world_otl_sf, col = "black", fill = NA) +
coord_sf(xlim = c(-180, +175.0), ylim = c(-90, 90), expand = FALSE) +
scale_x_continuous(breaks = breaks_x) +
scale_y_continuous(breaks = breaks_y) +
labs(x = "Longitude", y = "Latitude", title="Priestley-Taylor Coefficient (alpha) (AE/PE)", fill="alpha") +
theme_bw()
.html
file.This R Notebook *.Rmd
file produces the simplest of
.html output. Note that it does not do any Markdown-type formatting, but
simply embeds the output in the document. What makes this an R Notebook
file (despite having the *.Rmd
extension), is the presence
of a “YAML” (“Yet Another Markup Language/YAML Ain’t Markup Language”)
header that looks like the following:
---
title: "Example 2 -- An RMarkdown Notebook file "
output: html_document
---
This header specifies that the output of “kniting” the file should be
an *.html
file. Here’s the code:
---
title: "Example 2 -- An RMarkdown Notebook file "
output: html_document
---
This RNotebook script reads a netCDF file consisting of several bioclimatic variables, and plots one of them, `alpha`
```{r alpha-plot-RNotebook-1 }
# load libraries
library(maps)
library(sf)
library(stars)
library(ggplot2)
```
Get a world outline object for plotting.
```{r alpha-plot-RNotebook-2 }
# world_sf
world_sf <- st_as_sf(maps::map("world", plot = FALSE, fill = TRUE))
world_otl_sf <- st_geometry(world_sf)
```
```{r alpha-plot-RNotebook-3 }
# ggplot map of world_outline
ggplot() +
geom_sf(data = world_otl_sf, fill = NA, col = "black") +
scale_x_continuous(breaks = seq(-180, 180, 30)) +
scale_y_continuous(breaks = seq(-90, 90, 30)) +
coord_sf(xlim = c(-180, +180), ylim = c(-90, 90), expand = FALSE) +
theme_bw()
```
Read alpha from a netCDF file of bioclimatic variables using `stars`.
```{r alpha-plot-RNotebook-4 }
# change path as necessary
nc_path <- "/Users/bartlein/Projects/RESS/data/nc_files/"
nc_name <- "cru10min30_bio.nc"
nc_file <- paste(nc_path, nc_name, sep="")
alpha <- read_ncdf(nc_file, var="alpha", proxy = FALSE)
```
```{r alpha-plot-RNotebook-5 }
# list alpha
alpha
```
Convert to an `sf` object.
```{r alpha-plot-RNotebook-6 }
# convert stars to sf
alpha_sf <- st_as_sf(alpha, as_points = TRUE)
alpha_sf
plot(alpha_sf, pch = 16, cex = 0.3)
```
Setup for making a `{ggplot2}` map of alpha.
```{r alpha-plot-RNotebook-7 }
# make a data.frame
lon <- st_coordinates(alpha_sf)[,1]
lat <- st_coordinates(alpha_sf)[,2]
alpha <- as.vector(alpha_sf)
alpha_df <- data.frame(lon, lat, alpha)
dim(alpha_df)
head(alpha_df)
```
```{r alpha-plot-RNotebook-8 }
# set axis labels (breaks)
breaks_x <- c(seq(-180, 180, by = 60))
breaks_y <- c(seq(-90, 90, by = 30))
labels_x <- as.character(breaks_x)
labels_y <- as.character(breaks_y)
```
Plot alpha.
```{r alpha-plot-RNotebook-9 }
# ggplot2 map of alpha
ggplot() +
geom_tile(data = alpha_df, aes(x = lon, y = lat, fill = alpha)) +
scale_fill_gradient2(low = "brown", mid="white", high = "darkgreen", midpoint = 0.50) +
geom_sf(data = world_otl_sf, col = "black", fill = NA) +
coord_sf(xlim = c(-180, +175.0), ylim = c(-90, 90), expand = FALSE) +
scale_x_continuous(breaks = breaks_x) +
scale_y_continuous(breaks = breaks_y) +
labs(x = "Longitude", y = "Latitude", title="Priestley-Taylor Coefficient (alpha) (AE/PE)", fill="alpha") +
theme_bw()
```
Some discussion could go here.
*.html
file.This example produces a single-page .html
file as
output, this time styled using the html-md-01.css
stylesheet (the same one used for the course web page). The YAML header
looks like
---
title: "Example 3 – A single-page *.html file, with some styling"
output:
html_document:
css: html-md-01.css
---
Here’s the code:
---
title: "Example 3 – A single-page *.html file, with some styling"
output:
html_document:
css: html-md-01.css
---
# Introduction #
This RMarkdown document describes reading a netCDF file consisting of several bioclimatic variables, and plots one of them, `alpha`.
## Load packages ##
```{r alpha-plot-RMarkdown-1page-1 }
# load libraries
library(sf)
library(stars)
library(ggplot2)
```
# Read data #
## World outline ##
### Read map data from the `{maps}` package ###
Read a map file of world coastlines and countries, and convert to an `sf` object.
```{r alpha-plot-RMarkdown-1page-2 }
# world_sf
world_sf <- st_as_sf(maps::map("world", plot = FALSE, fill = TRUE))
world_otl_sf <- st_geometry(world_sf)
```
### plot the world outline ###
```{r alpha-plot-RMarkdown-1page-3 }
# ggplot map of world_outline
ggplot() +
geom_sf(data = world_otl_sf, fill = NA, col = "black") +
scale_x_continuous(breaks = seq(-180, 180, 30)) +
scale_y_continuous(breaks = seq(-90, 90, 30)) +
coord_sf(xlim = c(-180, +180), ylim = c(-90, 90), expand = FALSE) +
theme_bw()
```
## Read alpha ##
Now read in alpha alpha from a netCDF file of bioclimatic variables using stars.
```{r alpha-plot-RMarkdown-1page-4 }
# read alpha (AE/PE)
# change path as necessary
nc_path <- "/Users/bartlein/Projects/RESS/data/nc_files/"
nc_name <- "cru10min30_bio.nc"
nc_file <- paste(nc_path, nc_name, sep="")
alpha <- read_ncdf(nc_file, var="alpha", proxy = FALSE)
```
List alpha to show contents of the object.
```{r alpha-plot-RMarkdown-1page-5}
# list alpha
alpha
```
Convert the `stars` object to an `sf` object:
```{r alpha-plot-RMarkdown-1page-6}
# convert stars to sf
alpha_sf <- st_as_sf(alpha, as_points = TRUE)
alpha_sf
plot(alpha_sf, pch = 16, cex = 0.3)
```
# Plot the data #
## Setup ##
Do some setting up befor plotting.
```{r alpha-plot-RMarkdown-1page-7}
# setup for plotting alpha
# make a data.frame
lon <- st_coordinates(alpha_sf)[,1]
lat <- st_coordinates(alpha_sf)[,2]
alpha <- as.vector(alpha_sf)
alpha_df <- data.frame(lon, lat, alpha)
dim(alpha_df)
head(alpha_df)
```
Set axis labels or breaks:
```{r alpha-plot-RMarkdown-1page-8}
# set axis labels (breaks)
breaks_x <- c(seq(-180, 180, by = 60))
breaks_y <- c(seq(-90, 90, by = 30))
labels_x <- as.character(breaks_x)
labels_y <- as.character(breaks_y)
```
## Plot alpha ##
Make a `{ggplot2}` maps of alpha.
```{r alpha-plot-RMarkdown-1page-9}
# ggplot2 map of alpha
ggplot() +
geom_tile(data = alpha_df, aes(x = lon, y = lat, fill = alpha)) +
scale_fill_gradient2(low = "brown", mid="white", high = "darkgreen", midpoint = 0.50) +
geom_sf(data = world_otl_sf, col = "black", fill = NA) +
coord_sf(xlim = c(-180, +175.0), ylim = c(-90, 90), expand = FALSE) +
scale_x_continuous(breaks = breaks_x) +
scale_y_continuous(breaks = breaks_y) +
labs(x = "Longitude", y = "Latitude", title="Priestley-Taylor Coefficient (alpha) (AE/PE)", fill="alpha") +
theme_bw()
```
# Discussion #
(A few paragraphs on map patterns, trends, etc.)
Important To replicate this example, it would be best to creat an entirely new project in a differnt folder than the one you may have been working in.
A multi-page R Markdown website, as the name suggests, includes a
collection of *.Rmd
files (that are each knit to create
*.html
files, plus a support file (_site.yml
)
that controls the navigation bar layout. A sixth file is
README.md
file that appears on the main page of the GitHub
repository. The steps below will create a minimalist multi-page web
site. The site will include an index.html
page with a brief
overview, a “Topics” tab with two pages, one an introduction, and the
other being the “Raster mapping” page, and an “About” tab, implemented
by four *.Rmd
files (plus the _site.yml
file).
Although R Studio contains great script-editing features, it’s a little
clunky as a text editor compared to Markdown-specific editors. (In
practice, if there is a lot of text to compose, edit, and format (as is
the case with this page), it may be more efficient to do that in a
Markdown editor, or VS Code, previewing while you go, and then include
the contents of the *.md
file created by the editor in a
*.Rmd
file using the Knitr “child document” chunk option
(e.g. {r child="index.md"}
)).
Start by creating an R Markdown file that will produce a single-page
.html
file as output, which will be incorporated into the
web site later:
index.Rmd
alpha_plot_RMarkdown_Site.Rmd
;*.html
files.Here is the code:
---
title: "GEOG4/590: R for Earth-System Science"
output:
html_document:
fig_caption: no
number_sections: no
toc: no
toc_float: false
collapsed: no
---
```{r set-options, echo=FALSE}
options(width = 105)
knitr::opts_chunk$set(dev='png', dpi=300, cache=TRUE)
pdf.options(useDingbats = TRUE)
```
## Project web page for GEOG 4/595 *R for Earth-System Science* ##
Topics covered here include:
- An introduction to the project
- A simple example of plotting a raster slice
- (more to come…)
---
title: "Introduction to the project"
output:
html_document:
fig_caption: no
number_sections: no
toc: no
toc_float: false
collapsed: no
---
```{r set-options, echo=FALSE}
options(width = 105)
knitr::opts_chunk$set(dev='png', dpi=300, cache=TRUE)
pdf.options(useDingbats = TRUE)
```
The visualization developed here uses the `{terra}` and `{rasterVis}` packages to map the global
pattern of alpha, calculated using the CRU CL2 climate data set. The maps also include a
world outline with coastlines and country borders.
More introductory material...
[alpha_plot_RMarkdown_Site.Rmd]
---
title: Raster mapping
output:
html_document:
css: html-md-01.css
fig_caption: yes
highlight: haddock
number_sections: yes
theme: spacelab
toc: yes
toc_float: false
collapsed: no
---
```{r set-options, echo=FALSE, strip.white=TRUE}
options(width = 105)
knitr::opts_chunk$set(dev='png', dpi=300, cache=TRUE)
pdf.options(useDingbats = TRUE)
```
# Introduction #
This RMarkdown document describes reading a netCDF file consisting of several bioclimatic variables, and plots one of them, `alpha`.
## Load packages ##
```{r alpha-plot-RMarkdown_Site-1 }
# load libraries
library(sf)
library(stars)
library(ggplot2)
```
# Read data #
## World outline ##
### Read map data from the `{maps}` package ###
Read a map file of world coastlines and countries, and convert to an `sf` object.
```{r alpha-plot-RMarkdown_Site-2 }
# world_sf
world_sf <- st_as_sf(maps::map("world", plot = FALSE, fill = TRUE))
world_otl_sf <- st_geometry(world_sf)
```
### Plot the world outline ###
```{r alpha-plot-RMarkdown_Site-3 }
# ggplot map of world_outline
ggplot() +
geom_sf(data = world_otl_sf, fill = NA, col = "blue") +
scale_x_continuous(breaks = seq(-180, 180, 30)) +
scale_y_continuous(breaks = seq(-90, 90, 30)) +
coord_sf(xlim = c(-180, +180), ylim = c(-90, 90), expand = FALSE) +
theme_bw()
```
## Read alpha ##
Now read in alpha from a netCDF file of bioclimatic variables using stars.
```{r alpha-plot-RMarkdown_Site-4 }
# read alpha (AE/PE)
# change path as necessary
nc_path <- "/Users/bartlein/Projects/RESS/data/nc_files/"
nc_name <- "cru10min30_bio.nc"
nc_file <- paste(nc_path, nc_name, sep="")
alpha <- read_ncdf(nc_file, var="alpha", proxy = FALSE)
```
List alpha to show contents of the object.
```{r alpha-plot-RMarkdown_Site-5 }
# list alpha
alpha
```
Convert the `stars` object to an `sf` object:
```{r alpha-plot-RMarkdown_Site-6 }
# convert stars to sf
alpha_sf <- st_as_sf(alpha, as_points = TRUE)
alpha_sf
plot(alpha_sf, pch = 16, cex = 0.3)
```
# Plot the data #
## Setup ##
Do some setting up befor plotting.
```{r alpha-plot-RMarkdown_Site-7 }
# setup for plotting alpha
# make a data.frame
lon <- st_coordinates(alpha_sf)[,1]
lat <- st_coordinates(alpha_sf)[,2]
alpha <- as.vector(alpha_sf)
alpha_df <- data.frame(lon, lat, alpha)
dim(alpha_df)
head(alpha_df)
```
Set axis labels or breaks:
```{r alpha-plot-RMarkdown_Site-8 }
# set axis labels (breaks)
breaks_x <- c(seq(-180, 180, by = 60))
breaks_y <- c(seq(-90, 90, by = 30))
labels_x <- as.character(breaks_x)
labels_y <- as.character(breaks_y)
```
## Plot alpha ##
Make a `{ggplot2}` maps of alpha.
```{r alpha-plot-RMarkdown_Site-9 }
# ggplot2 map of alpha
ggplot() +
geom_tile(data = alpha_df, aes(x = lon, y = lat, fill = alpha)) +
scale_fill_gradient2(low = "brown", mid="white", high = "darkgreen", midpoint = 0.50) +
geom_sf(data = world_otl_sf, col = "black", fill = NA) +
coord_sf(xlim = c(-180, +175.0), ylim = c(-90, 90), expand = FALSE) +
scale_x_continuous(breaks = breaks_x) +
scale_y_continuous(breaks = breaks_y) +
labs(x = "Longitude", y = "Latitude", title="Priestley-Taylor Coefficient (alpha) (AE/PE)", fill="alpha") +
theme_bw()
```
# Discussion #
(A few paragraphs on map patterns, trends, etc.)
---
title: "GEOG 4/590: R for Earth-System Science"
output:
html_document:
fig_caption: no
number_sections: no
toc: no
toc_float: false
collapsed: no
---
```{r set-options, echo=FALSE}
options(width = 105)
knitr::opts_chunk$set(dev='png', dpi=300, cache=TRUE)
pdf.options(useDingbats = TRUE)
```
P.J. Bartlein
Dept. Geography
Univ. Oregon
bartlein@uoregon.edu
The GitHub repository for this web site is at:
[https://github.com/pjbartlein/geog490](https://github.com/pjbartlein/geog490)
# geog490project
GEOG 4/590 *R for Earth-System Science*
Project Web Page Repository
The web page is at: [https://pjbartlein.github.io/geog490project/](https://pjbartlein.github.io/geog490project/)
name: GEOG 4/590
navbar:
title: "Project Web Page: R for Earth-System Science"
left:
- text: Topics
menu:
- text: "Introduction"
href: intro.html
- text: "Raster mapping example"
href: alpha_plot_RMarkdown_Site.html
right:
- text: "About"
href: about.html
output:
html_document:
theme: cosmo
version: 4
highlight: haddock
css: html-md-01.css
fig_caption: no
number_sections: yes
toc: yes
toc_float: no
collapsed: no
lib_dir: site_libs
self_contained: no
output_dir: docs
The knitr()
package is quite fussy about formatting of
the _site.yml
file so check it first in the case of later
errors.
In RStudio, the “build tools” needed to create the web site will need to be configured. On the RStudio menu, click on Build. If there is a menu choice that says “Install Tools” do that first, otherwise click on “Configure Build Tools…” A “Project Options” dialog will open. On the “Project build tools:” dropdown box, select “Website” The initial choices are probably fine, with the “Site directory” set to “(Project Root)” and the “Preview” and “Re-knit” checkboxes checked. Upon clicking OK, a “Build” tab should appear in the upper-right pane. If it doesn’t, restart R Studio (Session > Restart R). Select the build tab, and click on “Build Website”.
At this point, the project folder should contain:
/.git
/.Rproj.user
/docs
/alpha_plot_RMarkdown_Site_cache
/alpha_plot_RMarkdown_Site_files
html-md-01.css
.gitignore
.RData
.Rhistory
about.Rmd
index.Rmd
intro.Rmd
alpha_plot_RMarkdown_Site.Rmd
geog490.Rproj
README.md
_site.yml
… and the docs folder,
/site_libs
/plot_alpha_RMarkdown_Site_files
html-md-01.css
about.html
index.html
intro.html
alpha_plot_RMarkdown_Site.html
README.html
The website should be viewable at: https://pjbartlein.github.io/geog490/index.html
(the project’s web page)