COVID-19 in africa

2020-07-01  本文已影响0人  jamesjin63

ggplot2-绘制非洲地区新冠肺炎(COVID19)

最近接触COVID的项目,目前COVID已经在世界上普遍大流行,而且WHO官网及霍普金斯大学等都有相对应的全球COVID19监测平台。每天实时更新。

image.png

其实最早出现的新冠肺炎监测平台是2020年1月27日上线的丁香园疫情动态。
好了,今天要介绍的主要是,如何在R里面绘制非洲地区的COVID地图。

1.数据获取

通过上述文章,我们知道了如何获取非洲地区的geojson文件link,然后去WHO官网下载COVID疫情数据,保存成csv。然后通过 covid中的countryafrica中的country进行数据关联。
cd=left_join(africa,COVID),将covid数据关联到africa sf数据库中。

library(tidyverse)
library(sf)
library(ggspatial)
library(ggthemes)
library(RColorBrewer)
rm(list = ls())
africa = read_sf("africa.geojson") 

africa %>% 
  ggplot() + 
  geom_sf()

# get data covid
# https://covid19.who.int/?gclid=Cj0KCQjw6PD3BRDPARIsAN8pHuEwbdWQXOSPpKqWOeSVU72ODgyfZIWF8PIRJjtpdbcj_9p5L-I4shQaAiWREALw_wcB

# Source: World Health Organization
COVID=read.csv("WHO-COVID-19-global-data.csv") %>% 
  mutate(time=as.Date(Date_reported),
         iso_a2=as.character(Country_code),
         admin1=as.character(Country)) %>% 
  filter(time=="2020-06-30") %>% 
  select(iso_a2,admin1,Cumulative_cases) 

cd=left_join(africa,COVID)

image.png

2.添加图例

关联好数据后,我们来简单画一个图。默认的颜色好像有点丑,而且与WHO的图不一样,Cumulative_cases是连续性变量,需要对Cumulative_cases进行变量转换。
这里提一下,在ArcGis中,可以选择几何分段,jenks分段,等间距分段对连续性数值进行转换。得到分类的图例数据。R里面也有对应的函数cartography::getBreaks()可以实现对应的功能。
首先我们对Cumulative_cases进行分类,按照WHO的分类条件,分成5类。这里用到的函数为cut,主要是breasklabel。break是按照给定数值进行切分。然后赋予对应的label。

cut(x, breaks, labels = NULL,
include.lowest = FALSE, right = TRUE, dig.lab = 3,
ordered_result = FALSE, ...)

# plot
ggplot(cd) +
    geom_sf(aes(geometry = geometry, fill = Cumulative_cases))
    
 # break
cd=cd %>% mutate(Cumulative_cases=replace_na(Cumulative_cases,-99)) %>% 
  mutate(covid=cut(Cumulative_cases,
                           breaks = c(-Inf,0,1000,10000,50000,300000,Inf),
                           labels = c("A","B","C","D","E","F")
                           ))
ggplot(cd) +
  geom_sf(aes(geometry = geometry, fill = covid))
                                                 
image.png

但是最后的结果图,我们发现,label排序很乱,而且对应的颜色是ggplot默认给定的。
下一步,我们继续设置图例颜色与顺序。参考GGPLOT LEGEND TITLE, POSITION AND LABELS

3.更改图例颜色

更改颜色及顺序用到scale_fill_manual函数,这里是分类变量的设定。如果是连续性变量那就用到scale_fill_gradient(). 我们来找一下对应颜色语言配色方案: Colors in R。找到对应颜色后,按照从低到高排列好,values=后面
guide = guide_legend(reverse=TRUE)用来设定排序问题。

library(RColorBrewer)
# 通过指定名字来查看调色板
display.brewer.pal(n = 8, name = 'RdBu')
# 十六进制颜色规格
brewer.pal(n = 8, name = "RdBu")

p=ggplot(cd) +
  geom_sf(aes(geometry = geometry, fill = covid))

# plot
p +
  scale_fill_manual(name="COVID-19",
                    values = c("gray","#D1E5F0","#92C5DE","#4393C3","#2166AC"),
                    guide = guide_legend(reverse=TRUE))

image.png

4.添加比例尺与指北针

参考R 地图绘制-比例尺与指北针如何改变比例尺大小及位置。

p +
  scale_fill_manual(name="COVID-19",
                    values = c("gray","#D1E5F0","#92C5DE","#4393C3","#2166AC"),
                    guide = guide_legend(reverse=TRUE)) +
  annotation_scale(location = "bl", width_hint = 0.4,
                   height = unit(0.15, "cm"),
                   text_cex = 0.5) +
  annotation_north_arrow(location = "tr", which_north = "true", 
                         height = unit(0.8, "cm"),
                         width  = unit(0.8, "cm"),
                         pad_x = unit(0.05, "in"), pad_y = unit(0.05, "in"),
                         style = north_arrow_fancy_orienteering) +
  labs(title = "WHO COVID-19 in Africa",
       subtitle = "Data last updated:2020/06/30",
       x="Longitude",y="Latitude")
image.png

4.添加点与标签

有时候需要在地图上指出哪个国家比较严重,将其标记出来,并显示标签。
我们新建两个数据。Tanzania 与Nigeria,将他们标在地图上,并用细线标注。
这里需要注意的是,我们用geom_segment来表示点之间的细线。
所用新建的数据包括:


label=tibble(
  x=c(34.8,8.1),
  y=c(-6.3,9.5),
  xend=c(45.7,0.8),
  yend=c(-3.6,1.4),
  label=c("Tanzania","Nigeria"),
  xat=c(47,0.8),
  yat=c(-2.7,1.4))

label
# A tibble: 2 x 7
      x     y  xend  yend label      xat   yat
  <dbl> <dbl> <dbl> <dbl> <chr>    <dbl> <dbl>
1  34.8  -6.3  45.7  -3.6 Tanzania  47    -2.7
2   8.1   9.5   0.8   1.4 Nigeria    0.8   1.4

# add point
p +
  scale_fill_manual(name="COVID-19",
                    values = c("gray","#D1E5F0","#92C5DE","#4393C3","#2166AC"),
                    guide = guide_legend(reverse=TRUE)) +
  geom_point(data=label,aes(x=x,y=y),col="#D6604D",size=2,alpha=0.9)


# add lines
p +
  scale_fill_manual(name="COVID-19",
                    values = c("gray","#D1E5F0","#92C5DE","#4393C3","#2166AC"),
                    guide = guide_legend(reverse=TRUE)) +
  geom_point(data=label,aes(x=x,y=y),col="#D6604D",size=2,alpha=0.9) +
  # line
  geom_segment(data = label,
               aes(x = x, y = y, xend = xend, yend = yend)) +
  #label
  geom_text(data=label,
            aes(x=xat,y=yat,label=label),size=2.8)
  
  
image.png
image.png

当然也可以更改直线为曲线,参见Line segments and curves

最后,我门汇总在一起,看一下效果

p +
  scale_fill_manual(name="COVID-19",
                    values = c("gray","#D1E5F0","#92C5DE","#4393C3","#2166AC"),
                    guide = guide_legend(reverse=TRUE)) +
  geom_point(data=label,aes(x=x,y=y),col="#D6604D",size=2,alpha=0.9) +
  # line
  geom_segment(data = label,
               aes(x = x, y = y, xend = xend, yend = yend)) +
  #label
  geom_text(data=label,
            aes(x=xat,y=yat,label=label),size=2.8)+
  annotation_scale(location = "bl", width_hint = 0.4,
                   height = unit(0.15, "cm"),
                   text_cex = 0.5) +
  annotation_north_arrow(location = "tr", which_north = "true", 
                         height = unit(0.8, "cm"),
                         width  = unit(0.8, "cm"),
                         pad_x = unit(0.05, "in"), pad_y = unit(0.05, "in"),
                         style = north_arrow_fancy_orienteering) +
  labs(title = "WHO COVID-19 in Africa",
       subtitle = "Data last updated:2020/06/30",
       x="Longitude",y="Latitude")

  
image.png

其实还可以更改里面字体类型及大小。
后续会更新

参考

  1. ggplot2 legend : Easy steps to change the position and the appearance of a graph legend in R software
  2. How To Customize ggplot2
  3. HOW TO CHANGE GGPLOT LEGEND SIZE
  4. GGPLOT LEGEND TITLE, POSITION AND LABEL
  5. Saving High-Resolution ggplots
上一篇下一篇

猜你喜欢

热点阅读