Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active August 21, 2021 13:44
Show Gist options
  • Save yutannihilation/893bc11f663d695c71e7aa33d4e0860a to your computer and use it in GitHub Desktop.
Save yutannihilation/893bc11f663d695c71e7aa33d4e0860a to your computer and use it in GitHub Desktop.
library(tidyverse)
library(patchwork)

excel_file <- tempfile(fileext = ".xlsx")
curl::curl_download("https://www.fukushihoken.metro.tokyo.lg.jp/iryo/kansen/kensa/kensuu.files/syousaisenryakukensa.xlsx", excel_file)

d_raw <- readxl::read_excel(excel_file, skip = 2)
d_raw
#> # A tibble: 18 × 4
#>    期間                            検査実施場所            検査実施件数 陽性件数
#>    <chr>                           <chr>                          <dbl>    <dbl>
#>  1 "2021年4/1~5/1までの累計"      繁華街・飲食店・事業所…        11563       16
#>  2 "2021年5月第1週\r\n(5/2~5/9…  <NA>                            2524        2
#>  3 "2021年5月第2週\r\n(5/10~5/16… <NA>                            4816       16
#>  4 "2021年5月第3週\r\n(5/17~5/23… <NA>                           11463        8
#>  5 "2021年5月第4週\r\n(5/24~5/30… <NA>                           11091       12
#>  6 "2021年6月第1週\r\n(5/31~6/6…  <NA>                           13071       12
#>  7 "2021年6月第2週\r\n(6/7~6/13…  <NA>                           18236        3
#>  8 "2021年6月第3週\r\n(6/14~6/20… <NA>                           19236       11
#>  9 "2021年6月第4週\r\n(6/21~6/27… <NA>                           23500       15
#> 10 "2021年6月第5週\r\n(6/28~7/4…  <NA>                           19467       20
#> 11 "2021年7月第1週\r\n(7/5~7/11…  <NA>                            5975        3
#> 12 "2021年7月第2週\r\n(7/12~7/18… <NA>                            8891        9
#> 13 "2021年7月第3週\r\n(7/19~7/25… <NA>                           10978       15
#> 14 "2021年7月第4週\r\n(7/26~8/1…  <NA>                           13677       34
#> 15 "2021年8月第1週\r\n(8/2~8/8)" <NA>                           11156       43
#> 16 "2021年8月第2週\r\n(8/9~8/15…  <NA>                           10065       57
#> 17 "累計"                          <NA>                          195709      276
#> 18 "※ 速報値として公表するもの…   <NA>                              NA       NA

d <- d_raw |> 
  mutate(
    period = str_extract(期間, "\\d{1,2}/\\d{1,2}\uff5e\\d{1,2}/\\d{1,2}"),
    period = str_replace(period, "\uff5e", "\n\uff5e\n"), # 改行を挟む
    period = factor(period, levels = period),             # 勝手に並べ変わらないように元の順でfactorにしておく
    .after = 1L
  ) |> 
  filter(!is.na(period))

do_plot <- function(y, breaks, labels) {
  ggplot(d, aes(period, {{y}}, group = 1)) +
    geom_point() +
    geom_line() +
    scale_y_continuous(limits = c(0, NA), breaks = breaks, labels = labels) +
    theme_minimal() +
    # theme(axis.text.x = element_text(hjust = 0, angle = -20)) +
    labs(
      x = NULL,
      y = NULL,
      title = rlang::as_label(enexpr(y))
    )
}

p1 <- do_plot(検査実施件数, breaks = scales::breaks_extended(), labels = scales::label_comma())
p2 <- do_plot(陽性件数 / 検査実施件数, breaks = scales::breaks_width(0.001), labels = scales::label_percent())

p1 / p2

Created on 2021-08-21 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment