使用工具:RStudio
使用包:RMySQL,Tidyverse,Openair
數(shù)據(jù)來源:真氣網(wǎng)
繪圖所用數(shù)據(jù):長春市每日數(shù)據(jù)
前言:
在前期的文章中,
我們學會了如何用
Python實現(xiàn)各地空氣質(zhì)量數(shù)據(jù)的抓取,
往期文章:Python爬取真氣網(wǎng)天氣數(shù)據(jù)
那時的我們邁出了走向人生巔峰的第一步,
學會了如何獲取大量可靠的數(shù)據(jù)。
這樣就滿足了嗎?
不可能的,
這輩子都不可能的。
在這篇文章中,
我們來學習如何利用RStudio實現(xiàn)數(shù)據(jù)可視化,
看看一年有多少天適宜戶外活動。
春天到了,
又到了一年一度的~~~
一、獲取所需數(shù)據(jù)
往期文章:Python爬取真氣網(wǎng)天氣數(shù)據(jù)
二、加載所用包and讀取數(shù)據(jù)
本次所用的數(shù)據(jù)存儲在MySQL數(shù)據(jù)庫,
如果你的數(shù)據(jù)存儲在Excel或TXT中,
可以使用readr這個包讀取數(shù)據(jù),
具體過程不再此贅述.
library(RMySQL)#RMySQL用于連接MySQL數(shù)據(jù)庫
library(tidyverse)#非常強大的包,提供數(shù)據(jù)導入、分析、可視化一條龍服務
library(openair) #繪制日歷圖
conn=dbConnect(MySQL(),host='localhost',user='root',password='lixq9010',dbname='changchun',port=3306)#連接數(shù)據(jù)庫
dbSendQuery(conn =conn,'set names gbk') #防止出現(xiàn)中文亂碼
<MySQLResult:2,0,0>#運行結(jié)果
dbListTables(conn= conn) #返回數(shù)據(jù)庫中的所有表格
[1]"air_quality" #運行結(jié)果
res <-dbSendQuery(conn = conn,'select * from air_quality') #建立air_quality查詢對象
air <-dbFetch(res = res,n=-1) #(n=-1)表示提取res對象中的所有數(shù)據(jù)
三、整理數(shù)據(jù)
整理數(shù)據(jù)時出現(xiàn)的問題,
1、日期數(shù)據(jù)必須為Date類型,
轉(zhuǎn)換函數(shù):as.Date()
2、轉(zhuǎn)化數(shù)據(jù)時涉及data frame的操作
air$date表示操作air名稱表格中的date列。
class(air) #返回air的數(shù)據(jù)類型,方便后期對air進行操作
[1]"data.frame" #數(shù)據(jù)框類型
air <-air[,-2:-3] #刪除air中的第二及第三列
head(air) #返回air的前幾行數(shù)據(jù)
date PM2.5 PM10SO2 CO NO2 O31 2017-01-01 173 194 74 2.5 54 402 2017-01-02 87 108 69 2.0 52 563 2017-01-03 56 78 60 1.7 50 544 2017-01-04 75 102 79 1.9 73 305 2017-01-05 113 132 69 2.0 80 426 2017-01-06 119 138 94 2.3 80 46air$date <-as.Date(air$date) #將air中date的字符串類型轉(zhuǎn)換為日期類型(重要,不轉(zhuǎn)換后期會報錯)
air <-as_tibble(air) #將air轉(zhuǎn)換為tibble類型
head(air) #返回air的前幾行(觀察到第二行多了各個數(shù)據(jù)的類型,不用單獨查了,實在是太方便)
# A tibble: 6 x 7Date PM2.5 PM10 SO2 CO NO2 O3
<date> <int> <int> <int><dbl> <int> <int>1 2017-01-01 173 194 74 2.5 54 40
2 2017-01-02 87 108 69 2 52 56
3 2017-01-03 56 78 60 1.7 50 54
4 2017-01-04 75 102 79 1.9 73 30
5 2017-01-05 113 132 69 2 80 42
6 2017-01-06 119 138 94 2.3 80 46
air <-dplyr::arrange(air,date) #將air按照date的大小排序head(air) #返回air的前幾行
# A tibble: 6 x 7 Date PM2.5 PM10 SO2 CO NO2 O3<date> <int> <int> <int><dbl> <int> <int>1 2014-01-01 26 51 50 0.6 25 55
2 2014-01-02 47 100 64 1 44 47
3 2014-01-03 90 136 79 1.1 54 40
4 2014-01-04 109 148 72 1.5 63 43
5 2014-01-05 195 264 92 2.3 82 37
6 2014-01-06 129 172 94 1.5 61 53
air2017 <-air[1035:1399,] #返回air的1035至1399行數(shù)據(jù)并賦給air2017
四、繪制時間序列曲線
1、數(shù)據(jù)記錄時間:2014-01-01至2018-10-14
plot.ts(subset(air,select=-date),col='red')#繪制時間序列圖
數(shù)據(jù)顯示結(jié)果非常有趣,可以看到長春市的六種環(huán)境檢測物質(zhì)呈現(xiàn)出一定的周期性。尤其是SO2,這峰型也太TM標準了。而且其波動高度逐年降低,由此可以推測長春市環(huán)保部門對環(huán)境保護工作的認識也在逐漸加深,監(jiān)管力度逐漸增強。
2、當然了我們也可以使用summaryplot()函數(shù)快速概覽數(shù)據(jù)整體的情況,時間序列變化、統(tǒng)計指標、頻數(shù)分布等等
summaryPlot(air) #快速概覽數(shù)據(jù)整體情況
五、繪制污染物日歷圖
這樣還是不過癮,我們想做出一張日歷,
這樣每天看看日歷就能今天是否適宜出行。
通過調(diào)用openair中的calendarplot()函數(shù)繪制出污染物日歷圖。本次以PM2.5為例,當我們要出門時不妨看看我們自己繪制的污染物日歷圖,
嗯,今日不宜出行,
還是接著睡覺吧
calendarPlot(air2017,pollutant= 'PM2.5') #繪制日歷圖
我們根據(jù)有關部門的標準,按照標準將PM2.5日均濃度分為幾個等級并繪制污染物日歷圖,使數(shù)據(jù)結(jié)果更直觀。
levels <-c(0,35,75,115,150,250,350) #空氣質(zhì)量分類
labels <-c("優(yōu)","良","輕度污染","中度污染","重度污染","嚴重污染")
cols <- c('green','yellow','orange','red','purple','maroon') #定義顏色
calendarPlot(air2017,pollutant= 'PM2.5',breaks = levels,labels = labels,cols = cols,statistic ='mean',main='Daily PM2.5 in 2017 Changchun') #繪制日歷圖

硬廣:個人公眾號:此地古同