多種動(dòng)態(tài)圖的繪制(R語(yǔ)言篇)

上一章介紹過(guò)動(dòng)態(tài)氣泡圖的繪制(http://www.itdecent.cn/p/702d02d8333b?),這里繼續(xù)介紹動(dòng)態(tài)條形圖、柱形圖、點(diǎn)圖、折線(xiàn)圖的制作,原理和氣泡圖差不多,非常簡(jiǎn)單易上手。

一、動(dòng)態(tài)條形圖

1.1 加載包

library(gapminder)

library(ggdark)

library(ggplot2)

library(gganimate)

1.2 加載數(shù)據(jù)集

原始數(shù)據(jù):使用gapminder這個(gè)數(shù)據(jù)集合。該數(shù)據(jù)一共有6列,依次為country(國(guó)家)、continent(洲)、year(年份)、lifeExp(生活指數(shù))、pop(人口)、gdpPercap(國(guó)內(nèi)生產(chǎn)總值)。

head(gapminder)

mydata <- gapminder[which(gapminder$country %in% (c('France','Italy','China','Japan','Austria','Brazil','Colombia','Cuba','','Germany','India'))),] #避免圖形太冗雜,只篩選部分國(guó)家

1.3 繪制動(dòng)態(tài)條形圖

使用gganimate這個(gè)包繪制動(dòng)態(tài)圖:

使用函數(shù)transition_time()添加動(dòng)態(tài),并指定動(dòng)態(tài)依據(jù)哪個(gè)變量變化,這里動(dòng)態(tài)變量是year:

ps = ggplot(mydata, aes(x=reorder(country, lifeExp),y=lifeExp, fill=country,frame=year)) +

? ? geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +

? ? geom_text(aes(label=paste0(lifeExp)),col="black",hjust=-0.2)+

? ? theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +

? ? theme(panel.background=element_rect(fill='transparent'))+

? theme(axis.text.y=element_text(angle=0,colour="black",size=12,hjust=1))+

? theme(axis.text.x=element_text(angle=0,colour="white",size=2,hjust=1))+

? theme(panel.grid =element_blank()) +? ## 刪去網(wǎng)格線(xiàn)

? theme(axis.text = element_blank()) +? ## 刪去所有刻度標(biāo)簽

? theme(axis.ticks = element_blank()) +? ## 刪去所有刻度線(xiàn)

? coord_flip()+ #橫縱坐標(biāo)位置轉(zhuǎn)換

? transition_time(year) + #設(shè)置動(dòng)態(tài)

? labs(title = paste('Year:', '{frame_time}'),x = '', y ='各國(guó)生活指數(shù)')+

? ease_aes('linear')

ps

二、動(dòng)態(tài)柱形圖

和條形圖類(lèi)似,取消橫縱坐標(biāo)位置轉(zhuǎn)換(coord_flip()),稍微調(diào)整橫縱坐標(biāo)即可:

ps = ggplot(mydata, aes(x=reorder(country, lifeExp),y=lifeExp, fill=country,frame=year)) +

? ? geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE) +

? ? geom_text(aes(label=paste0(lifeExp)),col="black",hjust=0)+

? ? theme(axis.text.x = element_text(size = 12,angle = 90, hjust = 0.2, vjust = 0.2),legend.position="none") +

? ? theme(panel.background=element_rect(fill='transparent'))+

? theme(axis.text.y=element_text(angle=0,colour="white",size=12,hjust=0))+

? theme(axis.text.x=element_text(angle=0,colour="black",size=9,hjust=0))+

? theme(panel.grid =element_blank()) +? ## 刪去網(wǎng)格線(xiàn)

? theme(axis.text = element_blank()) +? ## 刪去所有刻度標(biāo)簽

? theme(axis.ticks = element_blank()) +? ## 刪去所有刻度線(xiàn)

# Here comes the gganimate specific bits

transition_time(year) +

labs(title = paste('Year:', '{frame_time}'),x = '', y ='各國(guó)生活指數(shù)')+

ease_aes('linear')

ps

三、動(dòng)態(tài)點(diǎn)圖

ps = ggplot(mydata, aes(x=year,y=lifeExp)) +

geom_point(aes(color = country)) +

#Here comes the gganimate specific bits

transition_manual(year, cumulative = T) +

labs(title = paste('Year:', '{current_frame}'),x = '', y ='各國(guó)生活指數(shù)')+ ease_aes('linear')

四、動(dòng)態(tài)折線(xiàn)圖

在點(diǎn)圖的基礎(chǔ)上加上對(duì)點(diǎn)的連線(xiàn),即是動(dòng)態(tài)折線(xiàn)圖啦,只需要加個(gè)簡(jiǎn)單的語(yǔ)句:geom_line(aes(color = country))

ps = ggplot(mydata, aes(x=year,y=lifeExp)) +

? ? geom_point(aes(color = country)) +

geom_line(aes(color = country)) +

# Here comes the gganimate specific bits

transition_manual(year, cumulative = T) +

labs(title = paste('Year:', '{current_frame}'),x = '', y ='各國(guó)生活指數(shù)')+

ease_aes('linear')


總結(jié):

用R繪制動(dòng)態(tài)圖的方法都是先用ggplot繪制出需要的圖形種類(lèi),然后使用transition_time函數(shù)添加動(dòng)態(tài)變量,這個(gè)變量可以是時(shí)間、也可以是其他變量,就能得到對(duì)應(yīng)的動(dòng)態(tài)圖啦。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

更多數(shù)據(jù)相關(guān)文章,歡迎關(guān)注公眾號(hào)“大數(shù)據(jù)會(huì)”。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容