-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample_plot_code.html
More file actions
77 lines (57 loc) · 4.01 KB
/
Copy pathsample_plot_code.html
File metadata and controls
77 lines (57 loc) · 4.01 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2>R code for reproducing the sample plot</h2>
<pre>
<code>
#Loading necessary packages
listofpackages = c('tidyverse','magrittr','rvest','ggeasy','ggnewscale')
newpackages = listofpackages[!(listofpackages %in% installed.packages()[,’Package’])]
if(length(newpackages)) install.packages(newpackages)
lapply(listofpackages, library, character.only = T)
#extract the weather info in 2011_december
hko_2011_12 = read_html('https://www.hko.gov.hk/en/wxinfo/pastwx/metob201112.htm')
table_hko_2011_12 = hko_2011_12 %>% html_table()
table_hko_2011_12[[1]] = table_hko_2011_12[[1]][-c(1,nrow(table_hko_2011_12[[1]])-2,nrow(table_hko_2011_12[[1]])-1,nrow(table_hko_2011_12[[1]])),]
names(table_hko_2011_12[[1]])[c(3:5,7,9)] = c('max','air_temperature_2','min','mean_relative_humidity_percent','total_rainfall_mm')
table_hko_2011_12[[1]] %<>% select('DateDECEMBER','max','air_temperature_2','min','mean_relative_humidity_percent','total_rainfall_mm')
table_hko_2011_12[[2]] = table_hko_2011_12[[2]][-c(nrow(table_hko_2011_12[[2]])-2,nrow(table_hko_2011_12[[2]])-1,nrow(table_hko_2011_12[[2]])),]
names(table_hko_2011_12[[2]])[c(7)] = c('mean_wind_speed_km_h')
table_hko_2011_12[[2]] %<>% select('DateDECEMBER','mean_wind_speed_km_h')
table_hko_2011_12 %<>% map(.,\(x){x$Date = seq.Date(as.Date("2011-12-01"),as.Date("2011-12-31"),"days");
x = x %>% relocate(Date,.before = DateDECEMBER);
x$DateDECEMBER = NULL;
return(x)}) %>%
do.call(full_join,.)
table_hko_2011_12$Date %<>% as.character()
table_hko_2011_12$max %<>% as.numeric
table_hko_2011_12$air_temperature_2 %<>% as.numeric
table_hko_2011_12$min %<>% as.numeric
table_hko_2011_12$mean_relative_humidity_percent %<>% as.numeric
table_hko_2011_12$total_rainfall_mm %<>% as.numeric
table_hko_2011_12$total_rainfall_mm = case_when(is.na(table_hko_2011_12$total_rainfall_mm) ~ 0,
TRUE ~ table_hko_2011_12$total_rainfall_mm)
table_hko_2011_12$mean_wind_speed_km_h %<>% as.numeric
table_hko_2011_12 %<>% pivot_longer(cols = c('max','min'), names_to = 'temp', values_to = 'value')
#plot by ggplot2
table_hko_2011_12 %>%
ggplot(aes(x = as.Date(Date), y = value)) +
geom_line(aes(color = temp), linewidth = 1) +
scale_color_manual(name = "Temperature before smoothing", values = c("brown1", "cyan"), labels = c("Maximum temperature", "Minimum temperature")) +
ggnewscale::new_scale_color() +
geom_smooth(aes(color = temp),method = 'loess', span = 0.2, alpha = 0.2) +
scale_color_manual(name = "Temperature after smoothing", values = c("darkred", "darkblue"), labels = c("Maximum temperature", "Minimum temperature")) +
ggtitle('Hong Kong Temperature in December, 2011 from Hong Kong Observatory') +
xlab('Date') +
ylab('Temperature (°C)') +
theme_classic() +
ggeasy::easy_center_title()
</code>
</pre>
</body>
</html>