1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

都道府県別のコロナ新規感染者数を7日間平均で図示するコマンド(R)

Last updated at Posted at 2022-01-23

コロナの新規感染者数を7日間平均で図示する場合、Rのsliderパッケージが有用なので、コマンド例として次に公開しておく。
色々と検討してみてほしい。

#必要なパッケージ
library(tidyverse)
library(lubridate)
library(slider)

#新規感染者数のデータセットの読み込み、インターネットから直接読み込み
covid_19_pref <- read_csv("https://covid19.mhlw.go.jp/public/opendata/newly_confirmed_cases_daily.csv")
covid_19_pref$Date <- as.Date(covid_19_pref$Date)
covid_19_pref <- covid_19_pref %>% gather(key = "Prefecture", value = "Newly confirmed cases", "ALL":"Okinawa")
covid_19_pref <- covid_19_pref %>% group_by(Prefecture) %>% 
  mutate(new_case_n_m7 = slide_vec(.x=`Newly confirmed cases`	, .f=mean, .before=6)) 

#累積死亡者数のデータセットの読み込み、インターネットから直接読み込み
death_num <- read_csv("https://covid19.mhlw.go.jp/public/opendata/deaths_cumulative_daily.csv")
death_num$Date <- as.Date(death_num$Date)
death_num <- death_num %>% gather(key = "Prefecture", value ="Deaths(Cumulative)", "ALL":"Okinawa")
covid_19_pref2 <- covid_19_pref %>% select("Date"=Date,"Prefecture"=Prefecture,"Value"=new_case_n_m7)
covid_19_pref2 <- covid_19_pref2 %>% mutate(Depart = "新規感染者(7日平均)")
death_num2 <- death_num %>% select("Date"=Date,"Prefecture"=Prefecture,"Value"=`Deaths(Cumulative)`)
death_num2 <- death_num2 %>% mutate(Depart = "累積死者数")

#人口数データは直接テーブルを組む
Prefecture <- c("ALL", "Hokkaido", "Aomori", "Iwate", "Miyagi", "Akita", "Yamagata", "Fukushima", "Ibaraki", "Tochigi", "Gunma", "Saitama", "Chiba", "Tokyo", "Kanagawa", "Niigata", "Toyama", "Ishikawa", "Fukui", "Yamanashi", "Nagano", "Gifu", "Shizuoka", "Aichi", "Mie", "Shiga", "Kyoto", "Osaka", "Hyogo", "Nara", "Wakayama", "Tottori", "Shimane", "Okayama", "Hiroshima", "Yamaguchi", "Tokushima", "Kagawa", "Ehime", "Kochi", "Fukuoka", "Saga", "Nagasaki", "Kumamoto", "Oita", "Miyazaki", "Kagoshima", "Okinawa")
Population <- c(127094745, 5381733, 1308265, 1279594, 2333899, 1023119, 1123891, 1914039, 2916976, 1974255, 1973115, 7266534, 6222666, 13515271, 9126214, 2304264, 1066328, 1154008, 786740, 834930, 2098804, 2031903, 3700305, 7483128, 1815865, 1412916, 2610353, 8839469, 5534800, 1364316, 963579, 573441, 694352, 1921525, 2843990, 1404729, 755733, 976263, 1385262, 728276, 5101556, 832832, 1377187, 1786170, 1166338, 1104069, 1648177, 1433566)
pop <- data.frame(Prefecture, Population)
pop <-pop %>% mutate(Pop_index = Population/100000)

#新規感染者数と死者数を組み合わせ。人口数をふって、データベースに整形
deaath_and_new <- bind_rows(covid_19_pref2,death_num2)
deaath_and_new <- left_join(x=deaath_and_new,y=pop,by="Prefecture")

#都道府県を順番に並べ漢字に直す
deaath_and_new$Prefecture <- deaath_and_new$Prefecture %>% 
  str_replace_all(c(
    "ALL"="00_全国",
    "Hokkaido"="01_北海道",
    "Aomori"="02_青森県",
    "Iwate"="03_岩手県",
    "Miyagi"="04_宮城県",
    "Akita"="05_秋田県",
    "Yamagata"="06_山形県",
    "Fukushima"="07_福島県",
    "Ibaraki"="08_茨城県",
    "Tochigi"="09_栃木県",
    "Gunma"="10_群馬県",
    "Saitama"="11_埼玉県",
    "Chiba"="12_千葉県",
    "Tokyo"="13_東京都",
    "Kanagawa"="14_神奈川県",
    "Niigata"="15_新潟県",
    "Toyama"="16_富山県",
    "Ishikawa"="17_石川県",
    "Fukui"="18_福井県",
    "Yamanashi"="19_山梨県",
    "Nagano"="20_長野県",
    "Gifu"="21_岐阜県",
    "Shizuoka"="22_静岡県",
    "Aichi"="23_愛知県",
    "Mie"="24_三重県",
    "Shiga"="25_滋賀県",
    "Kyoto"="26_京都府",
    "Osaka"="27_大阪府",
    "Hyogo"="28_兵庫県",
    "Nara"="29_奈良県",
    "Wakayama"="30_和歌山県",
    "Tottori"="31_鳥取県",
    "Shimane"="32_島根県",
    "Okayama"="33_岡山県",
    "Hiroshima"="34_広島県",
    "Yamaguchi"="35_山口県",
    "Tokushima"="36_徳島県",
    "Kagawa"="37_香川県",
    "Ehime"="38_愛媛県",
    "Kochi"="39_高知県",
    "Fukuoka"="40_福岡県",
    "Saga"="41_佐賀県",
    "Nagasaki"="42_長崎県",
    "Kumamoto"="43_熊本県",
    "Oita"="44_大分県",
    "Miyazaki"="45_宮崎県",
    "Kagoshima"="46_鹿児島県",
    "Okinawa"="47_沖縄県"))

#描画コマンド、ファセットで都道府県別に分けたものとフィルターで個別自治体を取り出すコマンド、フィルターの日付を調整すれば期間を変更できる
deaath_and_new %>% 
  filter(Date >= "2021-04-01" & Date <= "2022-01-22") %>% 
  filter(Depart == "新規感染者(7日平均)") %>%
  ggplot(aes(x=Date,y=Value/Pop_index))+
  geom_line()+
  labs(
    title = "7日間平均新規感染者数推移(Source:https://covid19.mhlw.go.jp/)",
    x="2021年4月1日〜2022年1月22日",
    y="人口10万人あたり数",
    colour="")+
  theme_bw(base_family = "HiraKakuPro-W3")+
  facet_wrap(~Prefecture,nrow=8, scale = "free")

deaath_and_new %>% 
  filter(Prefecture =="47_沖縄県") %>%
  filter(Date >= "2021-04-01" & Date <= "2022-01-22") %>% 
  filter(Depart == "新規感染者(7日平均)") %>%
  ggplot(aes(x=Date,y=Value/Pop_index))+
  geom_line()+
  labs(
    title = "沖縄県7日間平均新規感染者数推移",
    x="2021年4月1日〜2022年1月22日",
    y="人口10万人あたり数",
    colour="")+
  theme_bw(base_family = "HiraKakuPro-W3")
#end

20210401_20220122.png
20210401_20220122_okinawa.png

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?