forked from AdamWilsonLabEDU/SpatialDataScience
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS_02.Rmd
99 lines (76 loc) · 4.07 KB
/
CS_02.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: My grandfather says climate is cooling
week: 2
type: Case Study
subtitle: Import data, generate and save a graphic.
reading:
- The [ggplot2 vignette](https://ggplot2.tidyverse.org/)
tasks:
- Create a new R script in RStudio
- Load data from a comma-separated-values formatted text file hosted on a website
- Graph the annual mean temperature in June, July and August (`JJA`) using ggplot
- Add a smooth line with `geom_smooth()`
- Add informative axis labels using `xlab()` and `ylab()` including [units](https://data.giss.nasa.gov/cgi-bin/gistemp/stdata_show.cgi?id=425003010120&dt=1&ds=5)
- Add a graph title with `ggtitle()`
- Save a graphic to a png file using `png()` and `dev.off()` OR [`ggsave`](https://ggplot2.tidyverse.org/reference/ggsave.html)
- Save the script
- Click 'Source' in RStudio to run the script from beginning to end to re-run the entire process
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(knitr); library(rmarkdown)
source("functions.R")
```
# Reading
```{r reading,results='asis',echo=F}
md_bullet(rmarkdown::metadata$reading)
```
# Background
You are at a family gathering at Niagara Falls and your grandfather, who lives nearby, claims that summer temperatures are colder now than they were when he was a kid in the 1920s. Your inner data scientist thinks this is unlikely but you decide to look into it.
You will use the NASA GISS temperature record for the Buffalo Niagara Airport available [from this website](https://data.giss.nasa.gov/cgi-bin/gistemp/stdata_show.cgi?id=425003010120&dt=1&ds=5). Later we'll learn how to use APIs to interact with online databases, but for now we'll work with a simple csv file.
# Instructions
## Download the temperature data
```{r, message=F}
library(tidyverse)
# define the link to the data - you can try this in your browser too
dataurl="https://raw.githubusercontent.com/AdamWilsonLab/SpatialDataScience/master/docs/02_assets/buffaloweather.csv"
```
This url points to a CSV file with monthly mean temperatures from the GISS dataset. You can [check out the file here](`r dataurl`). Now use `read_csv()` to download and import the CSV file directly from the website (cool, huh!?).
```{r, message=F, eval=F}
temp=read_csv(dataurl,
skip=1, #skip the first line which has column names
na="999.90", # tell R that 999.90 means missing in this dataset
col_names = c("YEAR","JAN","FEB","MAR", # define column names
"APR","MAY","JUN","JUL",
"AUG","SEP","OCT","NOV",
"DEC","DJF","MAM","JJA",
"SON","metANN"))
# renaming is necessary becuase they used dashes ("-")
# in the column names and R doesn't like that.
```
## Explore the data
Now use your tools to explore the dataset. You can try `View(temp)` to open the table in a browsable 'excel-like' window. Or `summary(temp)` to get summaries of each column.
## Develop the graphic
You want to make a nice graphic to show your grandfather at your next family gathering. Be sure to include informative axis labels, a graph title, a graph subtitle describing the source of the data, the raw data, and a smoothed line showing the overal trend through time.
```{r, eval=F,echo=F}
p1=ggplot(temp,aes(x=YEAR,y=JJA))+
geom_line(col=grey(.2))+
geom_smooth(col="red")+
ylab("Mean Summer Temperatures (C)")+
ggtitle("Mean Summer Temperatures in Buffalo, NY (1880-2018)",
subtitle = "Summer includes June, July, and August\nData from the Global Historical Climate Network\nRed line is a LOESS smooth")+
xlab("Year")
p1
```
```{r, eval=F,echo=F}
png("02_assets/buffalo.png")
p1
dev.off()
```
# Specific Tasks
```{r tasks,results='asis',echo=F}
md_bullet(rmarkdown::metadata$tasks)
```
What do you tell your grandfather?
# Extra time?
If you have extra time, use the [station selector](https://data.giss.nasa.gov/gistemp/stdata/) to find the links to download additional stations and make additional plots. You could even merge different stations into the same table and plot them together.