疫情數據可視化(動態變化)

疫情數據可視化(動態變化)

用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語言,主要是爲了把握數據可視化和未來方向

pypi


數據來源於整理的肺炎數據, 請大家不要造謠傳謠,做好當代大學生的本分工作,緊緊團結在黨中央周圍


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章