用emoji表情包來可視化北京市歷史天氣狀況海渊!

The emoji-weather visualization of beijing in 2016

=================================================

Use the emoji-icon to visualize weather state of beijing in 2016!

------------------------------------------------------

library(RCurl)
library(XML)
library(dplyr)
library(stringr)
library(tidyr)
library(plyr)
library(rvest)
library(ggimage)
library(Cairo)
library(showtext)
library(lubridate)


url<-"http://lishi.tianqi.com/beijing/index.html"
myheader <-c("User-Agent"="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36")
webpage<-getURL(url,httpheader=myheader)
mymonthlink<-getHTMLLinks(url,externalOnly=TRUE)%>%grep(".*?2016\\d{2}.html",.,value=T)


a failure attempt:


####
#page1<-getURL(mymonthlink[2],.encoding="gbk")
#rd<-iconv(page1,"gbk","utf-8")
#rdhtml<-htmlParse(rd,encoding="UTF-8")
#cesh<-readHTMLList(rdhtml,trim=TRUE,elFun=xmlValue)%>%grep("\\d{4}-\\d{2}-\\d{2}",.,value=T)
#cesh<-cesh%>%sub("([a-z])(\\()(\\\)","",.)
#cesh<-cesh1%>%str_split(',')%>%plyr::ldply(.fun=NULL)
#cesh$V1<-cesh$V1%>%sub("[a-z]\\(","",.)%>%as.Date()
#names(cesh)<-c("date","high","low","state","wind","index")
####
以上代碼寫了一半寫不下去了,我有rvest為啥要用RCurl赋元,肯定自己腦抽筋了九妈!


then i find a batter way to get the target data.


mynewdata<-c()
for (i in mymonthlink){
mymonthdata<-read_html(i,encoding="gbk")%>%html_nodes("div.tqtongji2>ul")%>%html_text(trim=FALSE)%>%str_trim(.,side="right")%>%.[-1]
mynewdata<-c(mynewdata,mymonthdata)
}


mynewdata1<-mynewdata
mynewdata<-mynewdata1%>%gsub("\t\t\t|\t|\r\n","",.)%>%str_split('   ')%>%plyr::ldply(.fun=NULL)%>%.[,-2]
names(mynewdata)<-c("date","high","low","state","wind","index")
mynewdata$date<-as.Date(mynewdata$date)
mynewdata$high<-as.numeric(mynewdata$high)
mynewdata$low<-as.numeric(mynewdata$low)


#cleanning the dirty data.
unique(mynewdata$state)
happy<-c("晴","陣雨~晴","多云轉(zhuǎn)晴","多云~晴","雷陣雨~晴","陰~晴","霾~晴","浮塵~晴")
depressed<-c("霾","陰","多云","晴~多云","霾~多云","晴~霾","多云~霾","陣雨轉(zhuǎn)多云","多云轉(zhuǎn)陰","陰~多云","多云~陰","晴~陰","陣雨~多云","小雨~多云","小雨~陰","霾~霧","小雪~陰","陰~小雪","小雨~雨夾雪")
angry<-c("小雨","雨夾雪","小雪","雷陣雨","陣雨","中雨","小到中雨","雷陣雨~陰","多云~雷陣雨","陰~雷陣雨","霾~雷陣雨","多云~陣雨","晴~陣雨","陰~小雨","陣雨~小雨")
Terrified<-c("中到大雨","暴雨","雷陣雨~中到大雨")


#create a new factor[categorical] varibale.
mynewdata$mode<-NULL
mynewdata$mood<-ifelse(mynewdata$state%in% happy,"happy",ifelse(mynewdata$state%in% depressed,"depressed",ifelse(mynewdata$state%in% angry,"angry","Terrified")))
mynewdata <- within(mynewdata,{
mood_code <- NA
mood_code[mood=="happy"]<-"1f604"
mood_code[mood=="depressed"]<-"1f633"
mood_code[mood=="angry"]<-"1f62d"
mood_code[mood=="Terrified"]<-"1f621"
})


#tidy the time/date varibales.
mynewdata$month<-as.numeric(as.POSIXlt(mynewdata$date)$mon+1)
mynewdata$monthf<-factor(mynewdata$month,levels=as.character(1:12),labels=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"),ordered=TRUE)
mynewdata$weekday<-as.POSIXlt(mynewdata$date)$wday
mynewdata$weekdayf<-factor(mynewdata$weekday,levels=rev(0:6),labels=rev(c("Sun","Mon","Tue","Wed","Thu","Fri","Sat")),ordered=TRUE)
mynewdata$week <- as.numeric(format(mynewdata$date,"%W"))
mynewdata<-ddply(mynewdata,.(monthf),transform,monthweek=1+week-min(week))
mynewdata$day<-day(mynewdata$date)


setwd("F:/數(shù)據(jù)可視化/R/R語言學(xué)習(xí)筆記/可視化/ggplot2/商務(wù)圖表")
write.table(mynewdata,"historyweather.csv",sep=",",row.names=FALSE)
mynewdata<-read.csv("historyweather.csv",stringsAsFactors = FALSE,check.names = FALSE)
#first theme:
mytheme<-theme(
rect=element_blank(),
axis.ticks=element_blank(),
text=element_text(face="plain",lineheight=0.9,hjust=0.5,vjust=0.5,size=15),
title=element_text(face="plain",lineheight=0.9,hjust=0,vjust=0.5,size=30),
axis.title=element_blank(),
strip.text=element_text(size = rel(0.8)),
plot.margin = unit(c(5,2,5,2),"lines")
)


the first photo:


CairoPNG("emoji1.png",1000,870)
showtext.begin()
ggplot(mynewdata,aes(weekdayf,monthweek,fill=high))+
geom_tile(colour='white')+
scale_fill_gradient(low=NA, high=NA,guide=FALSE)+
ggtitle("The emoji-weather visualization of beijing in 2016")+
scale_y_reverse(breaks=seq(from=6,to=0,by=-1))+
ggimage::geom_emoji(aes(image=mood_code),size=.1)+
facet_wrap(~monthf ,nrow=3)+
mytheme
showtext.end()
dev.off()


second theme:


mytheme2<-theme(
rect=element_blank(),
axis.ticks=element_blank(),
text=element_text(face="plain",lineheight=0.9,hjust=0.5,vjust=0.5,size=15),
title=element_text(face="plain",lineheight=0.9,hjust=0,vjust=0.5,size=30),
axis.title=element_blank(),
strip.text=element_text(size = rel(0.8)),
plot.margin = unit(c(1,1,1,1),"lines")
)


second photo:


CairoPNG("emoji2.png",1200,1200)
showtext.begin()
ggplot(mynewdata,aes(x=factor(day),y=monthf,fill=high))+
geom_tile(colour='white')+
expand_limits(y =c(-12,12))+
scale_x_discrete(position=c("bottom"))+
coord_polar(theta="x")+
scale_fill_gradient(low=NA, high=NA,guide=FALSE)+
ggimage::geom_emoji(aes(image=mood_code),size=.015)+
geom_image(aes(x=0,y=-12),image ="weather.png", size =.15)+
ggtitle("The emoji-weather visualization of beijing in 2016")+
mytheme2
showtext.end()
dev.off()

?聯(lián)系方式:

----------------------------------------------------

wechat:ljty1991

Mail:578708965@qq.com

個人公眾號:數(shù)據(jù)小魔方(datamofang)

團隊公眾號:EasyCharts

qq交流群:[魔方學(xué)院]553270834個人簡介:

-------------------------------------------------

**杜雨**

財經(jīng)專業(yè)研究僧;

偽數(shù)據(jù)可視化達人猎拨;

文科背景的編程小白膀藐;

喜歡研究商務(wù)圖表與地理信息數(shù)據(jù)可視化,愛倒騰PowerBI红省、SAP DashBoard额各、Tableau、R ggplot2吧恃、Think-cell chart等諸如此類的數(shù)據(jù)可視化軟件虾啦,創(chuàng)建并運營微信公眾號“數(shù)據(jù)小魔方”。

Mail:578708965@qq.com


本作品采用知識共享署名-非商業(yè)性使用 4.0 國際許可協(xié)議進行許可痕寓。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末傲醉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子呻率,更是在濱河造成了極大的恐慌硬毕,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,816評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件礼仗,死亡現(xiàn)場離奇詭異吐咳,居然都是意外死亡,警方通過查閱死者的電腦和手機元践,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,729評論 3 385
  • 文/潘曉璐 我一進店門韭脊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人单旁,你說我怎么就攤上這事乾蓬。” “怎么了慎恒?”我有些...
    開封第一講書人閱讀 158,300評論 0 348
  • 文/不壞的土叔 我叫張陵任内,是天一觀的道長。 經(jīng)常有香客問我融柬,道長死嗦,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,780評論 1 285
  • 正文 為了忘掉前任粒氧,我火速辦了婚禮越除,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己摘盆,他們只是感情好翼雀,可當我...
    茶點故事閱讀 65,890評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著孩擂,像睡著了一般狼渊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上类垦,一...
    開封第一講書人閱讀 50,084評論 1 291
  • 那天狈邑,我揣著相機與錄音,去河邊找鬼蚤认。 笑死米苹,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的砰琢。 我是一名探鬼主播蘸嘶,決...
    沈念sama閱讀 39,151評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼陪汽!你這毒婦竟也來了训唱?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,912評論 0 268
  • 序言:老撾萬榮一對情侶失蹤掩缓,失蹤者是張志新(化名)和其女友劉穎雪情,沒想到半個月后遵岩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體你辣,經(jīng)...
    沈念sama閱讀 44,355評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,666評論 2 327
  • 正文 我和宋清朗相戀三年尘执,在試婚紗的時候發(fā)現(xiàn)自己被綠了舍哄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,809評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡誊锭,死狀恐怖表悬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情丧靡,我是刑警寧澤蟆沫,帶...
    沈念sama閱讀 34,504評論 4 334
  • 正文 年R本政府宣布,位于F島的核電站温治,受9級特大地震影響饭庞,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜熬荆,卻給世界環(huán)境...
    茶點故事閱讀 40,150評論 3 317
  • 文/蒙蒙 一舟山、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦累盗、人聲如沸寒矿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,882評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽符相。三九已至,卻和暖如春拆座,著一層夾襖步出監(jiān)牢的瞬間主巍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,121評論 1 267
  • 我被黑心中介騙來泰國打工挪凑, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留孕索,地道東北人。 一個月前我還...
    沈念sama閱讀 46,628評論 2 362
  • 正文 我出身青樓躏碳,卻偏偏與公主長得像搞旭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子菇绵,可洞房花燭夜當晚...
    茶點故事閱讀 43,724評論 2 351

推薦閱讀更多精彩內(nèi)容