Try   HackMD

R 語言學習心得
空汙篇

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


openair

If you are a total beginner to this, start here!

  1. Download R / Rstudio
  2. Open it ! (I do all the code in R)
  3. Choose a comfortable chair
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  4. Start learning!

基礎準備

  1. 基本套件 :
install.packages('openair')
install.packages('dplyr') #處理數據的function
  1. 下載region5air 的數據集 :
install.packages('region5air')
# 使種方法能解決該數據集不支援R 3.6.3版本問題
install.packages('remotes')
remotes::install_github("NateByers/region5air")

檢查數據

library(region5air)
data(chicago_air)
head(chicago_air)
#檢查一下日期
class(chicago_air$date)
## [1] "character"  // 發現是character!!!!

簡單日期整理

# 使用as.POSIXct()來使日期變成YYYY-MM-DD // tz = time zone
chicago_air$date <- as.POSIXct(chicago_air$date, tz = "America/Chicago")

更多日期的教學


summaryPlot()

library(openair)
library(dplyr)
summaryPlot(select(chicago_air, date:solar))

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


日期問題修復方法

#如果圖的顯示有問題的話,可能是日期的關係
#建立一個新的csv檔 在把它讀取進來並設定日期格式
write.csv(chicago_air, file = "chicago_air.csv", row.names = FALSE)

chicago_air <- import(file = "chicago_air.csv", date.format = "%Y-%m-%d",
                      tzone = "America/Chicago")

windRose()

data(chicago_wind)
head(chicago_wind)
#馬上就看到warning 資料// 時間顯示超怪的
#再用一次as.POSIXct()
chicago_wind$datetime <- as.POSIXct(chicago_wind$datetime, format ="%Y%m%dT%H%M",
                                    tz = "America/Chicago")
#改好之後用dplyr的 rename()來重新命名
#(make writing code faster by using short,intuitive names)
chicago_wind <- rename(chicago_wind, date = datetime, ws = wind_speed, wd = wind_dir)


windRose()-畫圖八

windRose(chicago_wind, key.footer = "knots") # default is m/s

#可以更改顏色 用c("color1","color2","color3","color4")
# 可更改的顏色直接打colours() 來查看完整的list // 這邊提供一組範例
windRose(chicago_wind, key.footer = "knots",cols = c("yellow", "green", "blue", "black"))

# 也可以更改type來看時間更短的統計情況 // year,season,weekday
windRose(chicago_wind, type = "weekday", key.footer = "knots")

windRose()-畫圖八

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


pollutionRose()

#跟剛剛的風向很像,只是改成呈現汙染物
# ozone = 臭氧 // breaks 可以設定顏色分層數值,這招同樣可以用在風速
pollutionRose(chicago_wind, pollutant = "ozone",      
              breaks = c(0, .02, .04, .06, .07, .08))

#break 風速範例 // 不過要考慮會不會有些數值會在你設定的最小值以下
windRose(chicago_wind, type = "season", key.footer = "knots",breaks = c(0,2,4,6,8,10,20))

#汙染也可以看每個月的發展
pollutionRose(chicago_wind, pollutant = "ozone", type = "month")

pollutionRose()-畫圖八

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


pollutionRose()-畫圖八

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


percentileRose()

#這個有點厲害,可以快速畫出視覺化的圖案,馬上了解汙染程度
percentileRose(chicago_wind, pollutant = "ozone", smooth  =TRUE)

#這邊提供一個特別一點顏色給大家參考
percentileRose(chicago_wind, pollutant = "ozone", smooth  =TRUE,col = "brewer1")

percentileRose()-畫圖八

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


timePlot()

#大家最喜歡的time series
timePlot(chicago_air, pollutant = c("ozone", "temp", "solar"))

#可以增加group = TRUE來讓3條線畫在同一張圖中
timePlot(chicago_air, pollutant = c("ozone", "temp", "solar"),group = TRUE)

#一樣可以自訂顏色
timePlot(chicago_air, pollutant = c("ozone", "temp", "solar"),group=TRUE,cols = c("yellow", "green", "blue"))

calendarPlot()

#蠻酷的一個東西 可以在日曆上顯示風向和汙染
calendarPlot(chicago_air, pollutant = "ozone")

#annote後面可以接: ws / ws / date /value
#wd = 平均風速方向 / ws = 基於風速的平均風速方向
# date = 幾月幾號 / value = 日平均
calendarPlot(chicago_wind, pollutant = "ozone", annotate = "ws")

#顏色選擇可以有點變化 以下舉例
calendarPlot(chicago_wind, pollutant = "ozone", annotate = "wd",cols ="jet")
calendarPlot(chicago_wind, pollutant = "ozone", annotate = "wd",cols ="heat")
calendarPlot(chicago_wind, pollutant = "ozone", annotate = "wd",cols ="increment")

calendarPlot()-畫圖八


calendarPlot()-畫圖八


calendarPlot()-畫圖八


More tutorial / note

  1. my coding-blog
  2. my movie-blog
tags: R beginner cat tutorial