技巧篇06--R语言绘制全网爆火的动态条形图
本期教程:师兄教你用R语言绘制动态条形图
数据:2020-1-22至2020-3-15日中国各省新冠肺炎累计确诊人数
下载地址:https://www.kaggle.com/aestheteaman01/china-covid19-data
代码:
# 安装并载入R包:
install.packages("gifski")
install.packages("devtools")
library(devtools)
install_github("jl5000/barRacer")
library(barRacer)
library(magrittr)
library(ggplot2)
library(dplyr)
# 读取数据:
data <- read.csv("China_edited_data.csv")
names(data)[c(1,8)] <- c("Province","Date")
data$Date <- as.character(data$Date)
data <- data[data$Province != "Hubei",] # 去掉湖北数据;
data$Date <- unlist(lapply(strsplit(data$Date,"-"),function(x) paste(paste(x[3],x[2],sep = "-"),x[1],sep = "-")))
data$Date <- as.Date(data$Date)
COVID19_Data <- data %>% group_by(Province,Date) %>%
summarise(n=sum(Confirmed))
bar_chart_race(COVID19_Data,
cat_col = Province,
val_col = n,
time_col = Date,
max_bars = 20,
duration = 30,
title = "China COVID19 Data")
gganimate::anim_save("COVID19_Data.gif")
网友评论