上次的動圖畫的差不多了,但是還差一個柱狀圖沒畫,重新補上。數據還是原來的數據,只是需要重新處理下,上文鏈接:讓你的數據動起來!
提取繪圖需要的數據,選擇top10的國家
data = gapminderselect.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')
當然,這個也可以轉換成視頻,配上音樂,就是前段時間流行的圖表了,製作比較簡單
閱讀更多 野生算法協會 的文章