前言
這篇小教程主要介紹ggplot2的一些作圖技巧,包括一些主題theme的一些細(xì)節(jié)設(shè)定,雖然ggtheme包括許多的主題,但有時(shí)我們也想要自己設(shè)置效果,讓自己的圖看起來與眾不同。
導(dǎo)入所需的包
library(data.table)
library(ggplot2)
library(leafletCN)
本文主要用到的包就是這幾個(gè)。
站點(diǎn)分布繪圖
首先我們對站點(diǎn)數(shù)據(jù)進(jìn)行分析。
geo <- fread('geo_clean.csv')
一個(gè)有七個(gè)字段,大概長這樣子。

我想看看站點(diǎn)的大致分布,這時(shí)我們需要一個(gè)底圖,由于我不想導(dǎo)入外部文件了,我用了郎大為leafletCN中的地圖。
filePath_p <- system.file("geojson/china.json",package = "leafletCN")
province <- read.geoShape(filePath_p)
接下來就可以繪制我們的分布地圖了。
ggplot()+
geom_point(data = geo, aes(lon,lat,color=province))+
geom_path(data=province, aes(long,lat,group=group))+
coord_map()

默認(rèn)畫出來的圖是這樣的,總覺得不太好看,于是我們就可以設(shè)置自己的主題了,我個(gè)人喜歡暗色調(diào)背景。
mytheme <- function(){
theme(text = element_text(family = 'STHeiti',color='gray80'), # 字體及顏色
axis.text = element_blank(), # 去掉坐標(biāo)刻度
axis.title = element_blank(), #去掉坐標(biāo)標(biāo)題
plot.background = element_rect(fill="gray20"),
panel.background = element_rect(fill="gray20"),
panel.border = element_rect(fill=NA, color="gray20", size=0.5, linetype="solid"), # 設(shè)置所有背景色
panel.grid = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(), # 去掉輔助線及坐標(biāo)線
legend.background = element_rect(fill = 'gray20'),
legend.key = element_rect(fill = 'gray20', color = 'gray20')) # 圖例背景色
}
重新繪圖,我們將地圖線條設(shè)為亮色調(diào),看起來比較舒服。
ggplot()+
geom_point(data = geo, aes(lon,lat,color=province),size=0.6)+
geom_polygon(data = province, aes(long,lat,group=group),
fill='transparent',color='gray80',size=0.1)+
coord_map()+
mytheme()

站點(diǎn)數(shù)量繪圖
有了以上設(shè)置樣式,我想知道每個(gè)省的站點(diǎn)數(shù)量,用柱狀圖來表示。
# 統(tǒng)計(jì)站點(diǎn)數(shù)目(此處為國控站點(diǎn)數(shù)目)
bar <- as.data.frame(table(geo$province))
names(bar) <- c('province','number')
ggplot(data = bar,aes(province,number,fill=province))+
geom_bar(stat = 'identity')+
geom_text(aes(label=number, vjust = -0.4, hjust = 0.5),color='gray80')+
theme(plot.background = element_rect(fill="gray20"),
panel.background = element_rect(fill="gray20"),
panel.border = element_rect(fill=NA, color="gray20", size=0.5, linetype="solid"),
panel.grid = element_blank(),
axis.line.x = element_line(color = 'gray80'),
axis.line.y = element_blank(),
axis.title = element_blank(),
axis.ticks = element_line(color = 'gray80'),
axis.ticks.y = element_blank(),
axis.text = element_text(family = 'STHeiti',color = 'gray80',angle = 90),
axis.text.y = element_blank(),
legend.position = 'none')

空氣aqi繪圖
知道了以上主題設(shè)置,我們導(dǎo)入空氣aqi數(shù)據(jù),就可以繪制出以下精彩圖形。
aqidata <- fread('cityaqi_2015.csv')
首先來看每一天的空氣aqi狀況,我們篩選出幾個(gè)典型城市。這里用了一個(gè)漸變色viridis,需要倒入viridis包,這里的主題仿造上面設(shè)置即可。
ggplot(tmp,aes(date,aqi,color=aqi)) +
geom_point(alpha=0.6,size=0.6)+
scale_x_date(date_breaks = "4 month",
date_labels = '%m-%d') +
facet_wrap(~region,nrow = 2,labeller = label_value)+
scale_color_viridis()+
xlab('date') + ylab('') +mytheme()

可以看出北京,石家莊,武漢等城市空氣質(zhì)量不好的天數(shù)遠(yuǎn)高于其他城市,普遍集中在1月份和12月份,我們還可以做一個(gè)熱力圖來觀察下。
熱力圖
ggplot(hotdata[hotdata$type=='AQI',], aes(province, month, fill = value)) +
geom_tile(colour="white", size=0.1, stat="identity") +
scale_fill_viridis(option="D",name='AQI') +
scale_y_continuous(expand = c(0,0),breaks = 1:12,labels = c('一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月')) +
ggtitle('2015全國各省市AQI月度熱力圖') +
mytheme()

后記
最后留一張空間分布給大家去實(shí)現(xiàn),感受ggplot2的可創(chuàng)造性。更多作圖細(xì)節(jié)及源代碼可關(guān)注我的公眾號‘曲圖工作室’。
