--- disqus: ahb0222 GA : G-VF9ZT413CG --- # 使用R擷取觀測資料查詢系統氣象資料並繪製四季風花圖 > [color=#40f1ef][name=LHB阿好伯, 2022/03/27][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `R` `可視化` `ggplot2` [TOC] :::danger 2024/01/20更新 非常可惜的是以前的網站更新了QQ 沒辦法在像之前一樣更改網址爬取資料 目前解決方式只好先分成兩步驟 先使用Python的Selenium下載CSV檔 再用ggplot2畫圖 相關程式碼我就分享出來 拋磚引玉看看有沒有大神能夠將整個程式碼在優化一下 或是有更好用的爬蟲套件能分享一下 不然光Selenium的安裝跟使用就耗費我好多時間XD Chrome的版本太新就是一個大問題chromedriver支援還沒跟上QQ 要想像之前架設一個Shiny能自動爬取跟畫圖看來是有困難QQ [使用Python selenium 爬取codis氣候資料服務系統資料並使用ggplot2繪製風花圖](/LW3Ys5qwQ4a4j6zrOdc8HA) ::: 開始構思如何增加[Shinydashboard(儀錶板)手動添加數據繪製風花圖](/Kd2uUuldTrmfafDFt_GfCA)中的功能 目前想到可以增加如每月分或每季的比較 初步是使用patchwork套件做組合 ![](https://hackmd.io/_uploads/r1_1U8pG9.png) ```r= library("patchwork") library("shiny") library("jsonlite") library("rvest") library("magrittr") library("lubridate") library("ggplot2") library("shinydashboard") library("DataEditR") library("jsonlite") library("lubridate") library("shinydashboard") start_date <- "2021/1/1" end_date <- "2021/12/31" #終止時間 lab_max <- 30 url_start <- paste0("https://e-service.cwb.gov.tw/HistoryDataQuery/DayDataController.do?command=viewMain&station=",station_number,"&stname=%25E9%259E%258D%25E9%2583%25A8&datepicker=") start_date <- ymd(start_date) #起始時間 end_date <- ymd(end_date) #終止時間 data1 <- data.frame() data2 <- data.frame() for(d in c(0:(end_date - start_date))){ url <- paste0(url_start,as.character(start_date + d),"&altitude=15m") #時間資料 time_H <- url %>% read_html() %>% html_nodes(xpath='//*[(@id = "MyTable")]//td[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>% html_text(trim = T) %>% as.numeric() #風速資料 Windspeed <- url %>% read_html() %>% html_nodes(xpath='//td[(((count(preceding-sibling::*) + 1) = 7) and parent::*)]') %>% html_text(trim = T) %>% gsub("X","-9999", .) %>% as.numeric() #風向 windDirection<- url %>% read_html() %>% html_nodes(xpath='//td[(((count(preceding-sibling::*) + 1) = 8) and parent::*)]') %>% html_text(trim = T) %>% gsub("X","-9999", .) %>% as.numeric() data1 <- cbind(as.character(start_date + d), time_H, Windspeed,windDirection) #合併資料 data2 <- rbind(data2, data1) print(start_date + d) } str(data2) windD <- (c("N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW","N")) FourSeasons2 <- factor(c("冬","春","夏","秋","冬")) data3 <- na.omit(data2) #刪除包含NA值的列 data3$windDirection_N <- cut(as.numeric(data3$windDirection), breaks = c(0, 11.26, 33.76, 56.26, 78.76, 101.26, 127.76, 146.26, 168.76, 191.26, 213.76, 236.26, 258.76, 281.26, 303.76, 326.26, 348.75, 360), windD, include.lowest = TRUE) data3$Windspeed_N <- cut(as.numeric(data3$Windspeed), breaks =c(0, 2.1, 4.1, 6.1, Inf), labels = c("0~2", "2.1~4", "4.1~6", ">6.1"), right = F) data3$FourSeasons <- force(cut(month(data3$V1), breaks = c(0, 1, 4, 7, 10, 12), FourSeasons2, include.lowest = TRUE)) data3$month <- month(data3$V1) data3$year <- year(data3$V1) data3$Windspeed <- as.numeric(data3$Windspeed) p1 <- data3 %>% dplyr::filter(FourSeasons == "春") %>% ggplot( aes(x = windDirection_N, fill = Windspeed_N))+ geom_bar( aes(y = (..count..)/sum(..count..)*100 ), position = position_stack(reverse = TRUE)) + theme_bw() + scale_x_discrete(drop = FALSE, labels = windD)+ scale_fill_discrete(guide = guide_legend(reverse=TRUE), name = "Wind Speed (m/s)",breaks = c("0~2", "2.1~4", "4.1~6", ">6.1"), labels= c("0~2", "2.1~4", "4.1~6", ">6.1"),drop = F)+ coord_polar(start = -0.2) + xlab("") + #ggtitle(paste(station_number,"(",start_date,"~",end_date,")"))+ theme(axis.title=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())+ annotate("text",x = "NE", y = c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))), label = paste(c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))),"%")) #ggsave("P1.png") p2 <- data3 %>% dplyr::filter(FourSeasons == "夏") %>% ggplot( aes(x = windDirection_N, fill = Windspeed_N))+ geom_bar( aes(y = (..count..)/sum(..count..)*100 ), position = position_stack(reverse = TRUE)) + theme_bw() + scale_x_discrete(drop = FALSE, labels = windD)+ scale_fill_discrete(guide = guide_legend(reverse=TRUE), name = "Wind Speed (m/s)",breaks = c("0~2", "2.1~4", "4.1~6", ">6.1"), labels= c("0~2", "2.1~4", "4.1~6", ">6.1"),drop = F)+ coord_polar(start = -0.2) + xlab("") + #ggtitle(paste(station_number,"(",start_date,"~",end_date,")"))+ theme(axis.title=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())+ annotate("text",x = "NE", y = c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))), label = paste(c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))),"%")) p3 <- data3 %>% dplyr::filter(FourSeasons == "秋") %>% ggplot( aes(x = windDirection_N, fill = Windspeed_N))+ geom_bar( aes(y = (..count..)/sum(..count..)*100 ), position = position_stack(reverse = TRUE)) + theme_bw() + scale_x_discrete(drop = FALSE, labels = windD)+ scale_fill_discrete(guide = guide_legend(reverse=TRUE), name = "Wind Speed (m/s)",breaks = c("0~2", "2.1~4", "4.1~6", ">6.1"), labels= c("0~2", "2.1~4", "4.1~6", ">6.1"),drop = F)+ coord_polar(start = -0.2) + xlab("") + #ggtitle(paste(station_number,"(",start_date,"~",end_date,")"))+ theme(axis.title=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())+ annotate("text",x = "NE", y = c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))), label = paste(c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))),"%")) p4 <- data3 %>% dplyr::filter(FourSeasons == "冬") %>% ggplot( aes(x = windDirection_N, fill = Windspeed_N))+ geom_bar( aes(y = (..count..)/sum(..count..)*100 ), position = position_stack(reverse = TRUE)) + theme_bw() + scale_x_discrete(drop = FALSE, labels = windD)+ scale_fill_discrete(guide = guide_legend(reverse=TRUE), name = "Wind Speed (m/s)",breaks = c("0~2", "2.1~4", "4.1~6", ">6.1"), labels= c("0~2", "2.1~4", "4.1~6", ">6.1"),drop = F)+ coord_polar(start = -0.2) + xlab("") + #ggtitle(paste(station_number,"(",start_date,"~",end_date,")"))+ theme(axis.title=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank())+ annotate("text",x = "NE", y = c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))), label = paste(c(scales::extended_breaks(5)(range(0,as.numeric(lab_max)))),"%")) (p1+p2)/(p3+p4)+ plot_annotation(tag_levels = list(c("春","夏","秋","冬")))+ plot_layout(guides='collect') ``` 每一季還好但若是每月使用patchwork就不適合了 但若是使用 facet_wrap的方式結果會是不一樣的QQ 目前還沒找到問題 若是有人知道問題在哪麻煩跟我說 ```r= p5 <- ggplot(data3, aes(x = windDirection_N, fill = Windspeed_N))+ geom_bar( aes(y = (..count..)/sum(..count..)*100 ), position = position_stack(reverse = TRUE)) + theme_bw() + scale_x_discrete(drop = FALSE, labels = windD)+ scale_fill_discrete(guide = guide_legend(reverse=TRUE), name = "Wind Speed (m/s)",breaks = c("0~2", "2.1~4", "4.1~6", ">6.1"), labels= c("0~2", "2.1~4", "4.1~6", ">6.1"),drop = F)+ coord_polar(start = -0.2) + xlab("") + #ggtitle(paste(station_number,"(",start_date,"~",end_date,")"))+ theme(axis.title=element_blank(), axis.text.y=element_blank(), axis.ticks=element_blank()) p5 + facet_wrap(~FourSeasons, ncol = 2) ``` ![](https://hackmd.io/_uploads/BkuWP8Tfc.png) 🌟 🌟全文可以至下方連結觀看或是補充 全文分享至 https://www.facebook.com/LHB0222/ https://www.instagram.com/ahb0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/nHEcVmm.jpg)