如何用R繪製新冠病毒世界格局(1)

 

今天這篇我們先用ggplot2包做一下主要地區和城市的疫情格局,用點(point)的形式來展示數據,好處是可以看出較小的國家和具體城市的情況。

下一篇我們再給大家展示用多邊形(polygon)的形式展示數據,來看出各個國家/地區的整體情況。

 

利用install.packages("package_name")安裝幾個必要的安裝包,加載備用。

install.packages("tidyverse")
install.packages("ggplot2")
install.packages("maps")
install.packages("viridis")
install.packages("readr")
install.packages("viridisLite")

library(tidyverse)
library(ggplot2)
library(maps)
library(viridis)
library(readr)

 

下載數據

最新數據可以從Johns Hopkins University Center for SystemsScience and Engineering (JHU CSSE)下載:https://systems.jhu.edu

Github上有整理好的數據(https://github.com/CSSEGISandData/COVID-19),我們可以直接下載。

先來下載確診病例的數據。

Confirmed <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")).

以下是原始數據截圖,有的國家比如中國美國有細化到省/州的數據,較小的國家就只有全國整體數據,比如泰國,日本等。

 

查看最新的數據

這個數據每天23:59會更新,寫這篇文章的時候最新的日期是2020年3月15日,所以我們用這個日期。上面截圖中只顯示到了1月30日,但後邊每天都會添加新的一欄數據。用下面這個代碼查看最新日期的數據

select(Confirmed,tail(names(Confirmed),1))

加載世界地圖

world <-map_data("world")
ggplot() +geom_polygon(data=world, aes(x=long, y=lat, group=group), fill="grey")

 

開始作圖了

ggplot()+
  geom_polygon(data=world,aes(x=long,y=lat,group=group),fill="grey",alpha=0.3) +
  geom_point(data=Confirmed,aes(x=Long,y=Lat,size=`3/15/20`,color=`3/15/20`),alpha=0.5)

粗略做出效果:

mybreaks<- c(1, 20, 100, 1000, 50000)
mylabels<- c("1-19", "20-99", "100-999","1,000-49,999", "50,000+")
ggplot() +
  geom_polygon(data=world, aes(x=long, y=lat,group=group), fill="grey", alpha=0.3) +
  geom_point(data=Confirmed, aes(x=Long, y=Lat,size=`3/15/20`, color=`3/15/20`), alpha=0.5) +
  scale_size_continuous(name="Confirmedcases", trans="log", range=c(1,7), breaks=mybreaks,labels=mylabels) +
  scale_colour_viridis_c(option="inferno", direction=-1,name="Confirmed cases", trans="log", breaks=mybreaks,labels=mylabels) +
  guides(colour=guide_legend()) +
  theme_void() +
  theme(legend.position="bottom")

細調參數進行美化:

做死亡病例和治癒病例的地圖

Deaths <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Deaths.csv"))
Recovered <-read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Recovered.csv"))

 

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