疫情数据可视化(动态变化)
用R进行数据可视化
不需要任何外部文件,下面代码一步到位:
library(ggplot2)
library(scales)
library(gganimate)
source('https://github.com/mcanouil/DEV/raw/master/R/theme_black.R')
#get data
raw_data <- jsonlite::fromJSON("http://ncov.nosensor.com:8080/api/")
data <- raw_data$province
total_conf <- sapply(data$NcovNum, FUN = function(x) {sum(unlist(x))})
plot_data <- data.frame(time = rev(as.Date(data$Time)),
total_conf = rev(total_conf))
# plot
gganimate::animate(
plot = ggplot(data = plot_data, aes(x = time, y = total_conf)) +
geom_path(size = 1.5, lineend = "round", linejoin = "round", colour = "white") +
scale_x_date(
date_breaks = "1 day",
# date_labels = "%",
expand = expand_scale(mult = c(0.05, 0.08))
) +
scale_y_continuous(
#breaks = seq(0, ceiling(nrow(max()) / 100) * 100, 2500),
expand = expand_scale(mult = c(0.01, 0.05)),
labels = comma
) +
labs(
x = NULL,
y = NULL,
title = "每日总人数",
caption = paste("Compiled on", Sys.Date())
) +
theme_black() +
theme(panel.grid.minor = element_blank()) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
transition_reveal(along = as.Date(time)),
width = 6.3,
height = 4.7,
units = "in",
res = 300,
duration = 3,
bg = theme_get()$plot.background$colour,
renderer = gifski_renderer("total_people.gif")
)
欢迎关注:::R语言,主要是为了把握数据可视化和未来方向