COVID-19 的病例數(shù)據(jù)來源于COVID-19 (coronavirus) by Our World in Data催束,并通過 OWID data 繪制一張新冠肺炎新增病例的趨勢圖。
rm(list = ls())
options(digits = 4)
setwd("D:/xxxxxxxxxx/test")
library(xlsx)
covid <- read.csv("owid-covid-data.csv")
為了得到每個國家對應(yīng)的中文名稱伏社,還需要導(dǎo)入“國家和地區(qū)代碼.xlsx”文件抠刺。
country <- read.xlsx("國家和地區(qū)代碼.xlsx",
sheetIndex="Sheet1",
header=F,
startRow=9)
選取 Brazil 作為分析對象塔淤,對數(shù)據(jù)做一些簡單的處理和 mapping
covid1 <- subset(covid,
subset = (iso_code=="BRA"))
covid2 <- transform(covid1,
peo_vac=people_vaccinated/10000,
peo_fvac=people_fully_vaccinated/10000,
ana_new=new_cases,
ana_dea=new_deaths,
low=0
)
country1 <- subset(country,
subset = (X6!="NA"),
select = c(X2,X3,X6))
library(dplyr)
anadata1 <- left_join(covid2,
country1,
by=c("iso_code"="X6"))
定義一個移動平均數(shù)的函數(shù),得到移動平均值
mav <- function(a,n=3){
stats::filter(a,rep(1/n,n),sides = 1)
}
anadata2 <- transform(anadata1,
mean_new=mav(ana_new,7),
mean_dea=mav(ana_dea,7))
利用 ggplot2 繪圖
library(ggplot2)
gtitle <- paste(anadata2$X2,"_",anadata2$X3,sep='')[1]
p <- ggplot(anadata2,aes(x=as.Date(date))) +
geom_col(aes(y=ana_new,fill="g_col")) +
geom_line(aes(y=mean_new,color="g_line"),size=1) +
ggtitle(gtitle) +
labs(x=NULL,y=NULL) +
scale_x_date(date_label="%y/%m/%d",
date_breaks = "3 month",
minor_breaks = "1 month") +
scale_fill_manual(breaks = c("g_col"),
values = c("#cad5e5"),
label = c("New Case")) +
scale_color_manual(breaks = c("g_line"),
values = c("blue"),
label = c("Monving Average")) +
theme(plot.title =element_text(hjust = 0.5, vjust = 0.5),
legend.position = "bottom",
legend.title = element_blank(),
legend.background = element_blank())
p + theme(panel.background=element_rect(fill='transparent',
color='gray'),
legend.key=element_rect(fill='transparent',
color='transparent'))
最后展示一下輸出的plot
Rplot.png