
ggplot2によるグラフの作成
2020年5月9日以降
データの出所:
https://covid19.mhlw.go.jp/public/opendata/deaths_cumulative_daily.csv
library(readr)
death_total <- read_csv("https://covid19.mhlw.go.jp/public/opendata/deaths_cumulative_daily.csv")
dat <- death_total[,c(1,2)]
colnames(dat)[2] <- "死亡者数"
head(dat)
tail(dat)
Sys.setlocale("LC_TIME","C") #英語表記
dat$Date <- as.Date(dat$Date)
as_of <- dat$Date[nrow(dat)]
datebreaks <- seq(as.Date("2020-05-01"),as.Date("2023-02-01"),by ="4 month")
library(ggplot2)
library(stringr)
library(scales)
d1 <- ggplot(dat,aes(x=Date,y=死亡者数))+
geom_line(color="grey20")+
ylim(0,70000)+
labs(title = "新型コロナウイルス死亡者数",subtitle = str_c(as_of,"までの結果"),caption = "厚生労働省オープンデータ\nhttps://covid19.mhlw.go.jp/public/opendata/deaths_cumulative_daily.csv",x="日付") +
scale_x_date(breaks = datebreaks,labels=date_format("%Y %b"))+
theme_minimal(base_family="HiraKakuProN-W3")+
theme(axis.text.x=element_text(angle = 30,hjust = 1))
d1
1日の増加を計算
# すべてにゼロを入れる。
dat$前日 <- numeric(nrow(dat))
# 2日目から最後の日までのところに前日のデータを入れる。
dat$前日[2:nrow(dat)] <- dat$死亡者数[1:nrow(dat)-1]
dat$増分<- dat$死亡者数 - dat$前日
head(dat)
tail(dat)
# データを2日目からのものにする。行数1減。
dat <- dat[2:nrow(dat),]
Sys.setlocale("LC_TIME","C") #英語表記
dat$Date <- as.Date(dat$Date)
as_of <- dat$Date[nrow(dat)]
datebreaks <- seq(as.Date("2020-05-01"),as.Date("2023-02-01"),by ="4 month")
library(ggplot2)
library(stringr)
library(scales)
d1 <- ggplot(dat,aes(x=Date,y=増分))+
geom_line(color="grey20")+
ylim(0,450)+
labs(title = "新型コロナウイルス死亡者数",subtitle = str_c(as_of,"までの結果"),caption = "厚生労働省オープンデータ\nhttps://covid19.mhlw.go.jp/public/opendata/deaths_cumulative_daily.csv",x="日付", y="死亡者数") +
scale_x_date(breaks = datebreaks,labels=date_format("%Y %b"))+
theme_minimal(base_family="HiraKakuProN-W3")+
theme(axis.text.x=element_text(angle = 30,hjust = 1))
d1
移動平均を計算
平均2 <- vector("numeric",nrow(dat))
for (i in 7:nrow(dat)) {
平均2[i] <- (dat[i,4] + dat[i-1,4] + dat[i-2,4] + dat[i-3,4] + dat[i-4,4] + dat[i-5,4] + dat[i-6,4])/7
}
平均2 <- as.numeric(平均2)
dat$平均2 <- 平均2
dat$Date <- as.Date(dat$Date)
as_of <- dat$Date[nrow(dat)]
datebreaks <- seq(as.Date("2020-05-01"),as.Date("2023-02-01"),by ="4 month")
head(dat,10)
tail(dat,10)
as_of <- dat$Date[nrow(dat)]
datebreaks <- seq(as.Date("2020-05-01"),as.Date("2023-02-01"),by ="4 month")
library(ggplot2)
library(stringr)
library(scales)
d2 <- ggplot(dat,aes(x=Date,y=平均2))+
geom_line(color="grey20")+
ylim(0,400)+
labs(title = "新型コロナウイルス死亡者数",subtitle = str_c(as_of,"までの結果"),caption = "厚生労働省オープンデータ\nhttps://covid19.mhlw.go.jp/public/opendata/deaths_cumulative_daily.csv",x="日付", y="死亡者数(移動平均)") +
scale_x_date(breaks = datebreaks,labels=date_format("%Y %b"))+
theme_minimal(base_family="HiraKakuProN-W3")+
theme(axis.text.x=element_text(angle = 30,hjust = 1))
d2
