-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab 4.qmd
162 lines (152 loc) · 4.72 KB
/
lab 4.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
151
152
153
154
155
156
157
158
159
160
---
title: "lab 4"
format: html
editor: visual
---
#Name: Liying Deng
```{r}
library(tidyverse)
library(data.table)
library(leaflet)
library(dplyr)
```
#Question 1
```{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")
```
#Question 2
```{r}
met <- met[met$temp > -17, ]
met[met$elev==9999.0, ] <- NA
library(lubridate)
met[,date:= as.Date(paste(year, month, day, sep= "-"))]
met_avg <- met[,.(
temp = mean(temp,na.rm=TRUE),
rh = mean(rh,na.rm=TRUE),
wind.sp = mean(wind.sp,na.rm=TRUE),
vis.dist = mean(vis.dist,na.rm=TRUE),
dew.point = mean(dew.point, na.rm = T),
lat = mean(lat),
lon = mean(lon),
elev = mean(elev,na.rm=TRUE)
), by=c("USAFID")]
met_avg[, region := fifelse(lon < -98 & lat >= 39.71, "NW",
fifelse(lon < -98 & lat < 39.71, "SW",
fifelse(lon >= -98 & lat >= 39.71, "NE",
"SE")))]
met_avg[, elev_cat := cut(
x = elev,
breaks = c(-Inf, 0, 1500, 3000, Inf),
labels = c("low", "med", "high", "very high"),
right = FALSE
)]
```
#Question 3
```{r}
met_avg <- met_avg [!is.na(wind.sp)&!is.na(dew.point)]
ggplot(met_avg)+
geom_violin(aes(x=1, y=wind.sp, fill="wind.sp"))+
geom_violin(aes(x=1, y=dew.point, fill="dew.point"))+
facet_grid(~region)
```
# Each region exhibits distinct characteristics, leading to variations in wind speed. The Southwest region, in particular, has significantly higher dew point levels compared to other regions, with a relatively large range of variation.
#Question 4
```{r}
met_avg[!is.na(wind.sp)&!is.na(dew.point)] |>
ggplot(mapping = aes(x = dew.point, y = wind.sp, color = region, linetype = region)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, col = "Blue")
```
#The dew point increases, the wind speed tends to remain relatively constant or decrease slightly
#Question 5
```{r}
met_avg[!is.na(elev_cat) & elev_cat != "clear"] |>
ggplot() +
geom_bar(mapping = aes(x = elev_cat, fill = region), position = "dodge")+
scale_fill_viridis_d()+
labs(title="the weather stations by elevation category",
x='elev_cat',
y="number of weather stations",
fill="Region")+
theme_minimal()
```
#The graph reveals a clear preference for placing weather stations at medium elevations across all regions, particularly in the NW and SE regions.
#Question 6
```{r}
met_avg[!is.na(elev_cat) & elev_cat != "clear"] |>
ggplot() +
stat_summary(mapping = aes(x = region, y = dew.point,fill="dew.point"),
fun.data = "mean_sdl",
geom ="bar")+
stat_summary(mapping = aes(x = region, y = dew.point,fill="dew.point"),
fun.data = "mean_sdl",
geom ="errorbar")+
stat_summary(mapping = aes(x = region, y = wind.sp,fill="wind.sp"),
fun.data = "mean_sdl",
geom ="bar")+
stat_summary(mapping = aes(x = region, y = wind.sp,fill="wind.sp"),
fun.data = "mean_sdl",
geom ="errorbar")+
labs(title="Mean Dew point and wind speed by region",
x="Region",
y="mean value")+
theme_minimal()
```
#The SE region exhibits the highest humidity levels, while the NW region is notably drier and calmer.
#Question 7
```{r}
pal <- colorNumeric(
palette = "YlGnBu",
domain = met_avg$rh,
)
rhmap <- leaflet(met_avg) |>
addProviderTiles('CartoDB.Positron') |>
addCircles(
lat = ~lat, lng=~lon,
label = ~paste0(round(rh,2), ' C'), color = ~pal(rh),
opacity = 1, fillOpacity =1, radius = 5
) |>
addLegend('bottomleft', pal=pal, values=met_avg$rh,
title='Relative Humidity', opacity=1)
top_10 <- met_avg %>%
arrange(desc(rh)) %>%
slice(1:10)
rhmap <- rhmap %>%
addMarkers(
data = top_10,
~lon, ~lat,
popup = ~paste(USAFID, "<br>RH:", rh, "%"),
label = ~USAFID
)
rhmap
```
# Moderate to High RH in the Central U.S, Lower RH in the Western U.S and Higher RH in the Eastern and Southeastern U.S.
#Question 8
```{r}
ggplot(met_avg, aes(x = wind.sp, y = rh, color = region)) +
geom_point(alpha = 0.6, size = 2) +
labs(
title = "Scatter Plot of Wind Speed vs. Relative Humidity by Region",
x = "Wind Speed ",
y = "Relative Humidity "
) +
theme_minimal() +
theme(legend.position = "bottom")
ggplot(met_avg, aes(x = wind.sp, color = region, fill = region)) +
geom_density(alpha = 0.5) + # Add density with transparency
labs(
title = "Density Plot of Wind Speed by Region",
x = "Wind Speed",
y = "Density"
) +
scale_y_continuous(expand = expansion(mult = c(0, 0.05)))
theme_minimal() +
theme(legend.position = "bottom")
```