熱線電話:13121318867

登錄
首頁精彩閱讀讓你的數據動起來-動態柱狀圖
讓你的數據動起來-動態柱狀圖
2020-09-09
收藏

作者:小伍哥

 來源:AI入門學習

上次的動圖畫的差不多了,但是還差一個柱狀圖沒畫,重新補上。數據還是原來的數據,只是需要重新處理下,上文鏈接:讓你的數據動起來!

提取繪圖需要的數據,選擇top10的國家

data = gapminder
select.country = c(as.character(head(data$country[data$year==2007]
                                     [order(data$pop[data$year==2007],decreasing = TRUE)],10)))
data = data[data$country %in%select.country,]
data$country = factor(data$country, order = TRUE, levels =select.country)

制作Gif

ggplot(data, aes(country, lifeExp,fill= country))
  +geom_bar(stat='identity', position ='dodge',show.legend = FALSE)
  +theme(axis.text.x = element_text(angle =45, hjust =0.5, vjust =0.5))
  +coord_flip()
  +labs(title ='Year: {frame_time}', x ='country', y ='life expectancy')
  +transition_time(year)
  +ease_aes('linear')

顏色有點丑,我們換個顏色看看

ggplot(data, aes(country, lifeExp,fill= country)) 
  +geom_bar(stat= 'identity', position = 'dodge',show.legend = FALSE)
  +theme(axis.text.x = element_text(angle = 45, hjust = 0.5, vjust = 0.5)) +coord_flip()
  +scale_fill_manual(
  values = alpha(c("red", 'Aquamarine1',"MistyRose", "Magenta","Cyan", "DarkOrchid1","Gold1","Purple","Blue2","DarkCyan")))
  +labs(title = 'Year: {frame_time}', x = 'country', y = 'life expectancy') 
  +transition_time(year)
  +ease_aes('linear')

當然,這個也可以轉換成視頻,配上音樂,就是前段時間流行的圖表了,制作比較簡單。

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢