R语言数据可视化日历图
日历图,在环境与生态指标的动态监测中应用普遍,特别适用于显示不同时间段的指标情况。比如污染物中重金属含量、空气中PM2.5变化情况。在金融行业中检测股票收盘价、回测信号等指标中也很常见。生物医药领域的血糖或血压日记录值,新型冠状病毒的逐日确诊数量等等。通过时间分布的日历图动态监测数据,以弥补普通线图的不足。
01
随机生成数据
#构建数据框
>Data<data.table(date=seq(as.Date("1/01/2019","%d/%m/%Y"),as.Date("31/12/2019","%d/%m/%Y"),"days"),PM2.5=runif(365,50,350)
> Data
date PM2.5
1: 2019-01-01 87.79283
2: 2019-01-02 319.75490
3: 2019-01-03 325.30976
4: 2019-01-04 188.61730
5: 2019-01-05 144.84692
---
361: 2019-12-27 137.78985
362: 2019-12-28 128.80200
363: 2019-12-29 250.66169
364: 2019-12-30 159.78224
365: 2019-12-31 207.37999
02
数据处理
> Data$Year<-as.integer(strftime(Data$date,'%Y'))#年份
> Data$month<-as.integer(strftime(Data$date,'%m'))#月份
> Data$week<-as.integer(strftime(Data$date,'%W')) #周数,通常是以星期一作为一周的第一天
> Data$weekday<-as.integer(strftime(Data2$date,'%u'))#周数,以小数形式表示的工作日(1 - 7,星期一是1)
> Data$day<-strftime(Data2$date,'%d')#天数,以十进制数(01-31)表示的月份的日期。
> Data$month1<-month.abb[Data$month]#月份英文缩写
> Data$weekday1<-factor(Data$weekday,levels=(1:7),labels=(c("Mon","Tue","Wed","Thu","Fri","Sat","Sun")),ordered=TRUE)
> Data<-Data%>%group_by(month1)%>%mutate(monthweek=1+week-min(week))
> Data
# A tibble: 365 x 10
# Groups: month1 [12]
date PM2.5 Year month week weekday day month1 weekday1 monthweek
<date> <dbl> <int> <int> <int> <int> <chr> <chr> <ord> <dbl>
1 2019-01-01 87.8 2019 1 0 2 01 Jan Tue 1
2 2019-01-02 320. 2019 1 0 3 02 Jan Wed 1
3 2019-01-03 325. 2019 1 0 4 03 Jan Thu 1
4 2019-01-04 189. 2019 1 0 5 04 Jan Fri 1
5 2019-01-05 145. 2019 1 0 6 05 Jan Sat 1
6 2019-01-06 109. 2019 1 0 7 06 Jan Sun 1
7 2019-01-07 138. 2019 1 1 1 07 Jan Mon 2
8 2019-01-08 207. 2019 1 1 2 08 Jan Tue 2
9 2019-01-09 229. 2019 1 1 3 09 Jan Wed 2
10 2019-01-10 63.5 2019 1 1 4 10 Jan Thu 2
# ... with 355 more rows
03
绘图主题
#设置图片中的线条,字体,颜色等参数
>mytheme=theme(plot.title = element_text(face = "bold",hjust = 0.5,size=20,color = "black"),axis.title=element_text(face = "bold",hjust = 0.5,size=12,color="black"))
04
绘图
>ggplot(Data,aes(weekday1,monthweek,fill=PM2.5))
+geom_tile(colour="white")
+scale_fill_gradient(low="#56B1F7",high="#132B43",space="Lab",na.value = "grey50",guide="colourbar",aesthetics = "fill")
+geom_text(aes(label=day),size=3,colour="black",position="identity")
+facet_wrap(~month1,nrow=4)
+scale_y_reverse()+labs(title="Daily PM2.5 in 2019")
+xlab("Weekday")+ylab("Monthweek")+mytheme
>ggplot(Data,aes(weekday1,monthweek,fill=PM2.5))
+geom_tile(colour="white")
+scale_fill_gradient(low="Yellow",high="Red",space="Lab",na.value = "grey50",guide="colourbar",aesthetics = "fill")
+geom_text(aes(label=day),size=3,colour="black",position="identity")
+facet_wrap(~month1,nrow=4)+scale_y_reverse()
+labs(title="Daily PM2.5 in 2019")+xlab("Weekday")
+ylab("Monthweek")+mytheme
小结
1、日期值相关处理见基础知识 | R语言数据处理之日期值的转换
2、strftime()是日期、时间转换成为字符的函数
http://127.0.0.1:14091/library/base/html/strptime.html
3、as.integer()是创建整数向量的函数。
http://127.0.0.1:14091/library/base/html/integer.html
4、scale_fill_gradientn()创建颜色梯度。
http://127.0.0.1:14091/library/ggplot2/html/scale_gradient.html
5、geom_tile()函数是ggplot2中利用tile中心和大小绘制矩形图
http://127.0.0.1:14091/library/ggplot2/html/geom_tile.html
6、facet_wrap()函数主要是用于分面。一维变成二维。
http://127.0.0.1:14091/library/ggplot2/html/facet_wrap.html