今天我们来重复一下这张图,首先用我们的nCov2019包,我们可以容易地访问到历史记录,nCov2019包中的全球历史数据, 然后稍微操作一下,就可以拿到所有确诊数超过100的国家,把这个时间当做0点,然后我们就可以对比一下发展趋势了。

require(nCov2019)
require(dplyr)

d <- load_nCov2019()
dd <- d['global'] %>% 
  as_tibble %>%
  rename(confirm=cum_confirm) %>%
  filter(confirm > 100) %>%
  group_by(country) %>%
  mutate(days_since_100 = as.numeric(time - min(time))) %>%
  ungroup 

我们来看看,这个数据,大概是这样子的:

> tail(dd)
# A tibble: 6 x 6
  time       country       confirm cum_heal cum_dead days_since_100
  <date>     <chr>           <int>    <int>    <int>          <dbl>
1 2020-03-11 Malaysia          149       26        0              2
2 2020-03-11 Netherlands       503        0        5              5
3 2020-03-11 Norway            436        0        0              4
4 2020-03-11 Sweden            473        0        0              4
5 2020-03-11 Singapore         178       96        0             11
6 2020-03-11 United States    1004       10       31              8

画图

require(ggplot2)

breaks=c(100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000)


p <- ggplot(dd, aes(days_since_100, confirm, color = country)) +
  geom_smooth(method='lm', aes(group=1),
              data = . %>% filter(!country %in% c("China", "Japan", "Singapore")), 
              color='grey10', linetype='dashed') +
  geom_line(size = 0.8) +
  geom_point(pch = 21, size = 1) +
  scale_y_log10(expand = expansion(add = c(0,0.1)), 
                breaks = breaks, labels = breaks) +
  scale_x_continuous(expand = expansion(add = c(0,1))) +
  theme_minimal(base_size = 14) +
  theme(
    panel.grid.minor = element_blank(),
    legend.position = "none",
    plot.margin = margin(3,15,3,3,"mm")
  ) +
  coord_cartesian(clip = "off") +
  labs(x = "Number of days since 100th case", y = "", 
       subtitle = "Total number of cases")

首先,我们把中国、日本和新加坡这三个控制得比较好的国家去掉,然后画个回归线,看看这增长趋势如何。 代码没啥好讲的,如果你对clip="off"不了解的话,看这一篇:《ggplot2 - 3.0.0版本的新功能

然后就是加国家名了,这里为了让国家名能够凸显出来,我们需要用geom_shadowtext而不是geom_text,如果你不懂这个包,那你应该看这篇:《带你装逼带你飞,画图的文本打出阴影立体效果 》。

require(shadowtext)
p <- p + geom_shadowtext(aes(label = paste0(" ",country)), hjust=0, vjust = 0, 
                  data = . %>% group_by(country) %>% top_n(1, days_since_100), 
                  bg.color = "white") 

最后出图如下: