Last active
April 8, 2020 14:33
-
-
Save kenmomd/ae8e54df68dcdf5320a9c075a155516f to your computer and use it in GitHub Desktop.
Rスクリプト。2020-4-7時点での福岡のCOVID-19症例数のグラフ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ライブラリ読み込み | |
library(tidyverse) | |
library(lubridate) | |
#カラーパレット | |
library(ggsci) | |
#パーセント処理 | |
library(scales) | |
#作業フォルダ指定 | |
setwd("~/nCov2019/r/") | |
#csv読み込み | |
gds <- read.csv("gds_kenmo0408.csv") | |
#使用したデータ | |
#COVID-19 都道府県別 検査数分析/陽性者分析用スプレッドシート(ヽ´ん`)のGDS用。いわゆる整然データ | |
#https://docs.google.com/spreadsheets/d/1Cy4W9hYhGmABq1GuhLOkM92iYss0qy03Y1GeTv4bCyg/edit?ts=5e74a1c1#gid=845297461 | |
# 表示用の処理など | |
gds <- gds %>% | |
rename(経路不明 = 日別経路不明数) | |
#差を計算。日別陽性者数-経路不明 | |
gds <- gds %>% mutate(その他 = 日別陽性者数-経路不明) | |
#経路不明,その他をまとめる | |
df <- gds %>% | |
select(県名,日付,経路不明, その他) %>% | |
gather(key = type, value = n, -県名, -日付) # そのまま維持したい項目は「-」をつけて記載しておく | |
#日付処理 | |
df$日付 <- ymd(df$日付) | |
df$日付 <- as.POSIXct(df$日付) | |
#水準の順番指定 | |
df$経路 <- factor(df$type, levels=c("その他","経路不明")) | |
df$都道府県 = factor(df$県名, levels = c("北海道", "青森県", "岩手県", "宮城県", "秋田県", "山形県", "福島県", | |
"茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "東京都", "神奈川県", | |
"新潟県", "富山県", "石川県", "福井県", "山梨県", | |
"長野県", "岐阜県", "静岡県", "愛知県", "三重県", "滋賀県", | |
"京都府", "大阪府", "兵庫県", "奈良県", "和歌山県", | |
"鳥取県", "島根県", "岡山県", "広島県", "山口県", | |
"徳島県", "香川県", "愛媛県", "高知県", | |
"福岡県", "佐賀県", "長崎県", "熊本県", "大分県", "宮崎県", "鹿児島県","沖縄県")) | |
# 複数都道府県の指定の場合 | |
choice <- c("北海道", | |
"茨城県", "栃木県", "群馬県", "埼玉県", "千葉県", "神奈川県", | |
"新潟県", | |
"愛知県", | |
"京都府", "大阪府", "兵庫県", "福岡県") | |
# 指定都道府県のデータフレームの作成。複数都道府県はfilter(都道府県 %in% choice)としてfacetで並べる | |
df_choice <- df %>% | |
filter(都道府県 %in% "福岡県") | |
# 範囲指定 | |
lims <- as.POSIXct(strptime(c("2020-03-10","2020-04-08"), format = "%Y-%m-%d")) | |
# 福岡県の新規発生症例数 | |
g1 <- ggplot(df_choice, aes(x = 日付, y = n, fill=経路))+ | |
geom_bar(stat = "identity",alpha = 0.6) + | |
scale_x_datetime(limits =lims, | |
date_breaks = "2 days", | |
date_labels = "%m-%d", | |
timezone = "Asia/Tokyo") + | |
scale_y_continuous(breaks = seq(0, 40, by = 2), expand = c(0,0), limits = c(0,34))+ | |
#facet_wrap(~ 都道府県) + | |
theme_bw(base_size = 14, base_family = "HiraginoSans-W4")+ | |
theme(axis.text.x = element_text(angle = 90, hjust = 1))+ | |
theme(aspect.ratio=1.2)+ | |
scale_fill_lancet()+ | |
ggtitle("COVID-19 福岡県の新規発生症例数 4/7") | |
g1 | |
# 福岡県の新規発生症例数 割合 | |
g2 <- ggplot(df_choice, aes(x = 日付, y = n, fill=経路))+ | |
geom_bar(stat = "identity", position = "fill", alpha = 0.7) + | |
scale_x_datetime(limits =lims, | |
date_breaks = "2 days", | |
date_labels = "%m-%d", | |
timezone = "Asia/Tokyo") + | |
scale_y_continuous(labels = percent, expand = c(0,0))+ | |
theme_bw(base_size = 14, base_family = "HiraginoSans-W4")+ | |
theme(axis.text.x = element_text(angle = 45, hjust = 1))+ | |
scale_fill_lancet()+ | |
labs( x = "日付", y = "")+ | |
ggtitle("COVID-19 福岡県の新規発生症例の割合 4/7") | |
g2 | |
# 福岡県の累積症例数 | |
df_choice <- df_choice %>% group_by(経路) %>% mutate(累積 = cumsum(n)) | |
g3 <- ggplot(df_choice, aes(x = 日付, y = 累積, fill=経路))+ | |
geom_bar(stat = "identity", position="stack", alpha = 0.6) + | |
scale_x_datetime(limits =lims, | |
date_breaks = "2 days", | |
date_labels = "%m-%d", | |
timezone = "Asia/Tokyo") + | |
scale_y_continuous(breaks = seq(0, 225, by = 25), expand = c(0,0), limits = c(0,225))+ | |
theme_bw(base_size = 14, base_family = "HiraginoSans-W4")+ | |
theme(axis.text.x = element_text(angle = 90, hjust = 1))+ | |
theme(aspect.ratio=0.7)+ | |
scale_fill_lancet()+ | |
labs( x = "日付", y = "累積症例数")+ | |
ggtitle("COVID-19 福岡県の累積症例数 4/7") | |
g3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment