Prof. Marc Los Huertos • Pomona College • Environmental Analysis
process_all_stations().
create_heatmap().
What this activity covers. Every weather station tells a hundred-year story, but a single thermometer cannot show where and when a state is changing fastest. In this activity you will install the Regional Climate Narratives package,
RegionalClimateNarratives(v0.3.1), download daily records for your assigned state’s 50 longest-running NOAA stations, convert raw observations into normals, anomalies, and century-scale trends, and map those trends across your state. The maps become the evidence base for your climate narrative video.
Regional_Climate_Narratives_0.3.1.tar.gz – the Regional
Climate Narratives package installer
install_package.R – a script that installs all required
packages for you, with clear pass/fail reporting
example_workflow.R – a complete worked example you can
adapt
Regional_Climate_Narratives–Package_Reference.md –
reference for every function
Save the zip somewhere you can find it (Downloads is fine). In Part 1
you will create a regional_climate_narratives folder on the
server and upload the zip there.
You also need:
CA for California, TX for Texas)You only do Part 1 once, the very first time. After the package is installed, you never need to repeat these steps.
Open your web browser and go to the RStudio Server URL your instructor gave you. Log in with your username and password. You should see the RStudio interface with a Console pane on the left.
regional_climate_narratives (all lowercase, with
underscores), then click OKregional_climate_narratives folder to open
itRegional_Climate_Narratives--Student_Files.zip from your
computer, and click OKRStudio Server automatically unzips the file for you, so the files
land directly inside regional_climate_narratives.
✅ Check The
regional_climate_narratives folder should now contain
Regional_Climate_Narratives_0.3.1.tar.gz,
install_package.R, example_workflow.R, and the
package reference. If you instead see the files sitting in your Home
folder, you uploaded before opening the folder in step 3; move them into
regional_climate_narratives (check the boxes, then More
> Move).
In the Console (bottom left pane), type:
setwd("~/regional_climate_narratives")
Then verify the installer file is there:
file.exists("Regional_Climate_Narratives_0.3.1.tar.gz")
This must print TRUE. If it prints
FALSE:
regional_climate_narratives? Check
the Files pane (see the Step 2 check).setwd(...) line exactly, including
underscores? Run getwd() to see where R is looking.⚠️ Do not continue until file.exists(…) returns
TRUE. Every installation problem in Step 5 traces
back to this check being skipped.
The package needs several other R packages to work. Install them first. Copy and paste this entire block into the Console and press Enter:
install.packages(c(
"dplyr", "tidyr", "ggplot2", "lubridate",
"sp", "sf", "gstat", "raster", "stars",
"viridis", "maps", "mapdata", "patchwork"
))
This takes several minutes. You will see a lot of text scrolling by;
that is normal. Wait until you see the > prompt again
before continuing.
✅ Check After it finishes, look for any lines
containing had non-zero exit status. That means a package
failed to install. If you see it, note the package name and try
installing it individually, e.g., install.packages(“sf”).
If a package keeps failing, tell your instructor; it may need a system
library installed on the server.
Now install the actual package. Type:
install.packages("Regional_Climate_Narratives_0.3.1.tar.gz", repos = NULL, type = "source")
Wait for it to finish. The last lines should include:
* DONE (RegionalClimateNarratives)
If you see ERROR or
had non-zero exit status instead: Read the error
message. It usually names a missing package. Go back to Step 4 and
install that package, then try Step 5 again.
Load the package. Note the name here has no underscores: R package
names cannot contain them, so the package is
RegionalClimateNarratives even though the files use
Regional_Climate_Narratives:
library(RegionalClimateNarratives)
You should see this startup message:
===========================================================
Regional Climate Narratives v0.3.1
===========================================================
Regional Climate Trends Analysis and Visualization
Type ?RegionalClimateNarratives for help
===========================================================
If you see a !!! warning about missing packages
instead, run check_dependencies(), then try
library(RegionalClimateNarratives) again.
✅ Part 1 complete If the startup message shows
v0.3.1, the package is installed permanently on the server.
You will not need to repeat Part 1.
Do this once per state.
initialize_project("XX")
Replace XX with your state code. For example:
initialize_project("CA") # California
initialize_project("TX") # Texas
initialize_project("NY") # New York
What this does:
~/Regional_Climate_Narratives_CA/)Data/, Output/, and
Figures/ subfolders inside itsetwd() yourself)You should see output ending with Setup Complete! and a
count of potential stations.
select_stations(n = 50)
This picks the 50 stations with the longest, most complete records in your state.
download_stations()
⏰ This takes 10 to 30 minutes. You will see
progress for each station. You can keep working in another browser tab
while it runs. If it gets interrupted (connection drops, you close your
laptop, etc.), just run download_stations() again; it skips
stations already downloaded.
load_and_save_stations(cleanup = TRUE)
This converts the downloaded files into a single compressed file that
loads much faster in the future. Setting cleanup = TRUE
deletes the temporary files to save disk space.
Part 2 is done. Your data is saved to disk and ready for analysis.
When your R session ends (you log out, your session times out, or you restart R), all variables in memory are erased. Your files on disk are fine; they are saved permanently. But R needs to reload them.
library(RegionalClimateNarratives)
initialize_project("XX") # your state code, e.g., "CA"
load_stations()
library(RegionalClimateNarratives) loads
the package functions (they are not available until you do this)
initialize_project(“XX”) sets your working
directory to ~/Regional_Climate_Narratives_XX/ and creates
the path variables (datafolder,
figuresfolder). This replaces any need to call
setwd() yourself.
load_stations() loads your previously
saved weather data from the Data/ folder back into memory
You do NOT need to call setwd(), re-download anything,
or re-install the package.
trends <- process_all_stations()
This runs the full analysis: cleaning dates, converting units, calculating anomalies, and fitting trend lines. It takes a minute or two.
You may see messages like:
"Station USC00045123: Normals period = 1971-2000" -
This is normal. That station did not have enough data in the standard
1961-1990 baseline period, so the package used a different 30-year
window. The station is still included."Station USC00067890: Cannot compute normals... Skipping."
- That station did not have enough data for any baseline period. It is
excluded. A few skipped stations is expected.Check the results:
cat("Stations analyzed:", nrow(trends), "\n")
summary(trends$annual_trend_TMAX)
create_spatial_objects(trends)
This converts your trend data into a spatial format that can be mapped.
Annual maximum temperature trend:
map_tmax <- create_heatmap(
trends_sp,
trend_var = "annual_trend_TMAX",
title = paste(my.state, "- Annual Maximum Temperature Trend"),
subtitle = "Change in degrees C per 100 years",
state = my.state,
colors = "temp"
)
print(map_tmax)
Save it as a high-resolution image:
ggsave(paste0(figuresfolder, "TMAX_annual_", my.state, ".png"),
map_tmax, width = 10, height = 8, dpi = 300)
The file saves into your Figures/ folder. Find it in the
Files pane under
~/Regional_Climate_Narratives_XX/Figures/.
Annual minimum temperature trend:
map_tmin <- create_heatmap(
trends_sp,
trend_var = "annual_trend_TMIN",
title = paste(my.state, "- Annual Minimum Temperature Trend"),
subtitle = "Change in degrees C per 100 years",
state = my.state,
colors = "temp"
)
ggsave(paste0(figuresfolder, "TMIN_annual_", my.state, ".png"),
map_tmin, width = 10, height = 8, dpi = 300)
Precipitation trend (uses a different color scheme):
map_prcp <- create_heatmap(
trends_sp,
trend_var = "annual_trend_PRCP",
title = paste(my.state, "- Annual Precipitation Trend"),
subtitle = "Change in mm per 100 years",
state = my.state,
colors = "precip"
)
ggsave(paste0(figuresfolder, "PRCP_annual_", my.state, ".png"),
map_prcp, width = 10, height = 8, dpi = 300)
library(patchwork)
winter_map <- create_heatmap(trends_sp, "winter_trend_TMAX",
"Winter (Dec-Feb)", state = my.state, resolution = 0.15) +
theme(legend.position = "none")
spring_map <- create_heatmap(trends_sp, "spring_trend_TMAX",
"Spring (Mar-May)", state = my.state, resolution = 0.15) +
theme(legend.position = "none")
summer_map <- create_heatmap(trends_sp, "summer_trend_TMAX",
"Summer (Jun-Aug)", state = my.state, resolution = 0.15) +
theme(legend.position = "right")
fall_map <- create_heatmap(trends_sp, "fall_trend_TMAX",
"Fall (Sep-Nov)", state = my.state, resolution = 0.15) +
theme(legend.position = "none")
seasonal_panel <- (winter_map | spring_map) / (summer_map | fall_map) +
plot_annotation(
title = paste("Seasonal TMAX Trends -", my.state),
subtitle = "Change in degrees C per 100 years by season"
)
ggsave(paste0(figuresfolder, "Seasonal_TMAX_", my.state, ".png"),
seasonal_panel, width = 12, height = 10, dpi = 300)
Copy this block, change the state code on line 2, and run it:
library(RegionalClimateNarratives)
initialize_project("CA") # <-- CHANGE THIS TO YOUR STATE
load_stations()
trends <- process_all_stations()
create_spatial_objects(trends)
map <- create_heatmap(trends_sp, "annual_trend_TMAX",
paste(my.state, "Annual TMAX Trend"), state = my.state)
print(map)
ggsave(paste0(figuresfolder, "TMAX_", my.state, ".png"),
map, width = 10, height = 8, dpi = 300)
🌡️ Normals, anomalies, and trends A normal is the average value for each month over a 30-year baseline period; the standard is 1961–1990 (set by the World Meteorological Organization). If a station does not have enough data in that window, the package uses a later period and tells you. An anomaly is the observed value minus the normal: positive means warmer (or wetter) than the baseline average, negative means cooler (or drier). A trend is the slope of a line fitted through the anomalies over time, reported as change per century.
Anomaly = Observed value − Normal TMAX trend of +1.5
degrees C/century = max temperatures increased ~1.5 degrees C over 100
years
PRCP trend of −25 mm/century = annual precipitation decreased
~25 mm over 100 years
| Variable | What it shows |
|---|---|
annual_trend_TMAX |
Annual maximum temperature trend |
annual_trend_TMIN |
Annual minimum temperature trend |
annual_trend_PRCP |
Annual precipitation trend |
winter_trend_TMAX |
Winter (Dec-Feb) max temp trend |
spring_trend_TMAX |
Spring (Mar-May) max temp trend |
summer_trend_TMAX |
Summer (Jun-Aug) max temp trend |
fall_trend_TMAX |
Fall (Sep-Nov) max temp trend |
Replace TMAX with TMIN or PRCP
for min temperature or precipitation versions.
| What | Saved to disk? | What to do next session |
|---|---|---|
The RegionalClimateNarratives package |
Yes, installed permanently | Run library(RegionalClimateNarratives) |
| Your downloaded weather data | Yes, in ~/Regional_Climate_Narratives_XX/Data/ |
Run load_stations() |
| Your saved heat map images | Yes, in ~/Regional_Climate_Narratives_XX/Figures/ |
Nothing; the PNG files are already saved |
Variables in memory (trends, my.state,
map_tmax, etc.) |
No, R clears memory | Re-run initialize_project() and
load_stations() |
| Your working directory setting | No, resets when session ends | initialize_project() sets it for you |
?initialize_project # Help for a specific function
?RegionalClimateNarratives # Package overview
help(package = "RegionalClimateNarratives") # List all functions
If you are stuck, show your instructor the exact error message. Copy the red text from the Console.
file.exists(...) returns FALSE in Step 3The installer file is not in your current working directory. Check:
regional_climate_narratives? Check
the Files pane.setwd("~/regional_climate_narratives")? Run
getwd() to see where R is looking.A required package is missing. Install it:
install.packages("sf") # replace with the package name from the error
Or install all dependencies at once (copy-paste from Part 1, Step 4). Or run the included installer script, which handles everything with pass/fail reporting:
setwd("~/regional_climate_narratives")
source("install_package.R")
You need to run initialize_project("XX") at the start of
every R session. This is the most common error. It just means you
started a new session and forgot the setup lines.
Your data has not been downloaded yet, or the download did not finish. Go back to Part 2 (Steps 8-10).
Either the package is not loaded, or an old version is installed. Try:
library(RegionalClimateNarratives)
If that does not help, reinstall:
setwd("~/regional_climate_narratives")
install.packages("Regional_Climate_Narratives_0.3.1.tar.gz", repos = NULL, type = "source")
Then go to Session > Restart R in the RStudio menu, and try
library(RegionalClimateNarratives) again.
NOAA servers can be slow. For 50 stations, expect 10 to 30 minutes.
If it stops with errors, run download_stations() again. It
skips stations already downloaded.
"Normals period = 1971-2000" is informational. The
station is still analyzed. This is not an error.
"Cannot compute normals for TMAX. Skipping." means that
station did not have enough data. A few skipped stations is
expected.
resolution = 0.15 for smaller states,
resolution = 0.08 for large statestrends_sp exists: run
create_spatial_objects(trends)Run getwd() to see your current project folder. Your
files are in Data/ (weather data), Figures/
(saved heat maps), and Output/ (analysis results). Or
navigate in the Files pane (bottom right of RStudio).