-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab4_EY.qmd
150 lines (115 loc) · 4.05 KB
/
Lab4_EY.qmd
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "Lab 4"
author: "Erin Yu"
format: pdf
editor: visual
---
# Lab 4
#Read in the Data
```{r}
if (!file.exists("met_all.gz")) {
download.file(
url = "https://raw.githubusercontent.com/USCbiostats/data-science-data/master/02_met/met_all.gz",
destfile = "met_all.gz",
method = "libcurl",
timeout = 60
)
}
met <- data.table::fread("met_all.gz")
```
#Prepare the Data
```{r}
#Remove temperatures less than -17C
met <- met[temp >= -17] [elev == 9999.0, elev := NA]
#Generate a date variable
met[, date := as.Date(paste(year, month, day, sep = "-"))]
met <- met[data.table::week(date) == 1]
#Compute means by station
met_avg <- met[, .(
mean_temp = mean(temp, na.rm = TRUE),
mean_rh = mean(rh, na.rm = TRUE),
mean_wind_sp = mean(wind.sp, na.rm = TRUE),
mean_vis_dist = mean(vis.dist, na.rm = TRUE),
mean_dew_point = mean(dew.point, na.rm = TRUE),
lat = mean(lat, na.rm = TRUE),
lon = mean(lon, na.rm = TRUE),
elev = mean(elev, na.rm = TRUE)
), by = USAFID]
#Create Region Variables
met_avg[, region := ifelse(lon < -98 & lat > 39.71, 'NW',
ifelse(lon < -98 & lat <= 39.71, 'SW',
ifelse(lon >= -98 & lat > 39.71, 'NE',
'SE')))]
#Create Categorical variables
met_avg[, elev_cat := ifelse(elev > 252, "high", "low")]
```
#Use geom_violin
```{r}
install.packages("ggplot2")
library(ggplot2)
met_avg[!is.na(region) & !is.na(mean_wind_sp) & !is.na(mean_dew_point)] |>
ggplot(aes(x = region, y = mean_wind_sp, fill = region))+
geom_violin() +
facet_wrap(~ region, nrow = 1) +
labs(title = "Violin Plot of Wind Speed by Region") +
labs(x = "Region", y = "Wind Speed")
```
It was difficult for me to interpret this data because the graph would not generate.
#Use geom_jitter with stat_smooth
```{r}
install.packages("ggplot2")
library(ggplot2)
ggplot(met_avg, aes(x = mean_dew_point, y = mean_wind_sp, color = region)) +
geom_jitter() +
stat_smooth(method = "lm", se = FALSE) +
labs(title = "Association Between Dew Point and Wind Speed by Region") +
theme_minimal()
```
It was difficult for me to interpret this data because the graph was blank
#Bar plot of weather stations
```{r}
library(ggplot2)
ggplot(met_avg, aes(x = elev_cat, fill = region)) +
geom_bar(position = "dodge") +
scale_fill_brewer(palette = "Set3") +
labs(title = "Weather Stations by Elevation Category", x = "Elevation Category", y = "Count") +
theme_minimal()
```
It was difficult for me to interpret this data because the graph was blank
#Statistical Summary
```{r}
ggplot(met_avg, aes(x = region, y = mean_dew_point, fill = region)) +
stat_summary(fun.data = "mean_sdl", geom = "bar", color = "black") +
stat_summary(fun.data = "mean_sdl", geom = "errorbar", width = 0.2) +
labs(title = "Mean Dew Point by Region with Error Bars") +
theme_minimal()
ggplot(met_avg, aes(x = region, y = mean_wind_sp, fill = region)) +
stat_summary(fun.data = "mean_sdl", geom = "bar", color = "black") +
stat_summary(fun.data = "mean_sdl", geom = "errorbar", width = 0.2) +
labs(title = "Mean Wind Speed by Region with Error Bars") +
theme_minimal()
```
It was difficult for me to interpret this data because the graph was blank
#Map showing spatial trend
```{r}
pal <- colorNumeric(palette = "Blues", domain = met_avg$mean_rh)
leaflet(met_avg) %>%
addTiles() %>%
addCircles(lng = ~lon, lat = ~lat, color = ~pal(mean_rh), radius = 50000) %>%
addLegend("bottomright", pal = pal, values = ~mean_rh, title = "Relative Humidity") %>%
addMarkers(lng = ~lon[rank(-mean_rh) <= 10], lat = ~lat[rank(-mean_rh) <= 10],
popup = ~paste("RH: ", round(mean_rh, 2)))
```
#Use ggplot extension
```{r}
install.packages("gganimate")
library(gganimate)
p <- ggplot(met_avg, aes(x = lon, y = lat, color = mean_wind_sp, size = mean_rh)) +
geom_point() +
scale_color_viridis_c() +
theme_minimal() +
labs(title = 'Wind Speed and Humidity: {frame_time}', x = 'Longitude', y = 'Latitude') +
transition_time(year)
animate(p)
```
It was difficult for me to interpret this data because the graph was blank