转自Y叔
(1)检索疫情数据
(2)各省市的疫情历史数据
(3)疫情地图
(1)检索疫情数据
主要函数:get_nCov2019,summary
remotes::install_github("GuangchuangYu/nCov2019")
library(nCov2019)
x <-get_nCov2019()#当前最新数据
open(x)#打开腾讯浏览器
画图
library(forcats)
library(ggplot2)
d = x['广东',]
d$confirm=as.numeric(d$confirm)
d$name = fct_reorder(d$name, d$confirm)
ggplot(d, aes(name, confirm)) + geom_col(fill='steelblue') + coord_flip()
#转换坐标轴
+ geom_text(aes(y = confirm, label=confirm), hjust=0)
#加上数目
+ theme_minimal(base_size=14)
+ scale_y_continuous(expand=c(0,10)) + xlab(NULL) + ylab(NULL)
每日新增
ggplot(summary(x), aes(as.Date(date, "%m.%d"), as.numeric(confirm))) + geom_col(fill='firebrick') + theme_minimal(base_size = 14) + xlab(NULL) + ylab(NULL) + labs(caption = paste("accessed date:", time(x)))
(2)各省市的疫情历史数据
主要函数:load_nCov2019
x<-load_nCov2019()#获得历史数据
x['湖北', c(1:6, 9:11)][1:3,]#累积和新增的数据
画图
require(ggrepel)
d <- x['河南',]
ggplot(d,aes(time, as.numeric(cum_confirm), group=city, color=city)) + geom_point() + geom_line()
+ geom_text_repel(aes(label=city), data=d[d$time == time(x), ], hjust=1)
#加城市名字,加时间
+ theme_minimal(base_size = 14) + theme(legend.position='none') + xlab(NULL) + ylab(NULL)
各个省的总结数据
d2 <- summary(x)[,1:5]
ggplot(d2,
aes(time, as.numeric(cum_confirm), group=province, color=province)) +
geom_point() + geom_line() +
geom_text_repel(aes(label=province), data=d2[d2$time == time(x), ], hjust=1) +
theme_minimal(base_size = 14) + theme(legend.position='none') +
xlab(NULL) + ylab(NULL) + scale_y_log10()
(3)疫情地图
世界地图
require(nCov2019)
x = get_nCov2019()
plot(x)
中国地图
require(chinamap)
cn = get_map_china()
plot(x, chinamap=cn)
plot(x, region="china", chinamap=cn)#只画中国地图;参数date="2020-02-01"指定历史数据
动图历史数据
library(magick)
y <- load_nCov2019()
d <- c(paste0("2020-01-", 20:31), paste0("2020-02-0", 1:2))
img <- image_graph(600, 450, res = 96)
out <- lapply(d, function(date){
p <- plot(y, region="china", chinamap=cn, date=date,
label=FALSE, continuous_scale=FALSE)
print(p)
})
dev.off()
animation <- image_animate(img, fps = 2)
print(animation)