網頁

2021年5月24日 星期一

R:COVID-19 疫情統計(2.繪圖)

這篇我講解一下如將統計資料做量化的圖表比較分析這樣比較有助於理解

(每日確診數比較:台灣,新加坡,南韓,中國大陸)

上一篇有提到如何將全球COVID-19統計資料抓取下來並存在data farm,例如我取出了台灣,
新加坡,南韓,中國大陸的4個國家最近365天的資料,可以用 tail(data) 來看最近的6筆也就是近6天的統計資料。

台灣在5/22 每日確診數達高峰 723例

新加坡維持每日30~40例

南韓每日約500~600多例

中國大陸10~20幾例

有了這些data frame 就可以用圖表方式來呈現要表示的資料,如果我要比較這4個國家的死亡率(Mortality_Rate)程式如下:

ggplot(taiwan.df, aes(dates, mortality_rate)) +
      geom_line(aes(color="Taiwan")) +
      geom_line(data=singapore.df, aes(color="Singapore")) +
      geom_line(data=korea.df, aes(color="Korea")) +
      geom_line(data=china.df, aes(color="China")) +
      guides(colour = guide_legend("Country")) +
      ggtitle('Mortality Rate Taiwan vs Singapore vs Korea vs China') +
      xlab('') +
      ylab('')

輸出:(新加坡死亡率很低)

(死亡率比較圖)


如果要比較總確診數程式如下:(Y軸我除以1000以K為單位)

ggplot(taiwan.df, aes(dates, cases/10^3)) +
      geom_line(aes(color="Taiwan")) +
      geom_line(data=singapore.df, aes(color="Singapore")) +
      geom_line(data=korea.df, aes(color="Korea")) +
      geom_line(data=china.df, aes(color="China")) +
      guides(colour = guide_legend("Country")) +
      ggtitle('Total Case Taiwan vs Singapore vs Korea vs China') +
      xlab('') +
      ylab('(K)')

輸出:(可以看到韓國確診數非常陡峭)

(總確診數比較)

如果比較每日確診數程式如下:

#Daily Case
ggplot(taiwan.df, aes(dates, daily)) +
    geom_line(aes(color="Taiwan")) +
    geom_line(data=singapore.df, aes(color="Singapore")) +
    geom_line(data=korea.df, aes(color="Korea")) +
    geom_line(data=china.df, aes(color="China")) +
    guides(colour = guide_legend("Country")) +
    ggtitle('Daily Case Taiwan vs Singapore vs Korea va China') +
    xlab('') +
    ylab('')

輸出:

(台灣在五月有很明顯的上升趨勢,韓國在2021/1月高峰後有下降,4月後明顯有增加趨勢,新加坡在2020/8後控制得很不錯)

 

(每日確診數比較)

2021年5月23日 星期日

R:COVID-19 疫情統計

約翰·霍普金斯大學的Github 有分享了全球 COVID-19 疫情的統計資料,點選開啟連結

time_series_covid19_confirmed_global.csv
time_series_covid19_deaths_global.csv
time_series_covid19_recovered_global.csv
這3個檔案分別是全球確診數,死亡人數,復原人數統計

 

將這3個csv檔爬下來後用R的資料篩選,發現其中 5/22/2021台灣的確診數是723例,
並不是指揮中心公布的321例。

R的程式在此,有興趣的人可以參考: 

https://ideone.com/kw4Heg

如果不會寫程式其中網頁也有提供了查詢各個地區國家的疫情統計。

COVID-19 Dashboard


library(tidyverse)
library(ggplot2)
library(cowplot)
library(patchwork)
path <- "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/"
confirm_file <- "time_series_covid19_confirmed_global.csv"
deaths_file <- "time_series_covid19_deaths_global.csv"
recovered_file <- "time_series_covid19_recovered_global.csv"

confirmed_df <- read_csv(paste0(path, confirm_file))
deaths_df <- read_csv(paste0(path, deaths_file))
recovered_df <- read_csv(paste0(path, recovered_file))

sd <- length(confirmed_df) - 21  # start date
ed <- length((confirmed_df))     # end date
dates <- colnames(confirmed_df[, sd:ed])
dates <- as.Date(dates,format = "%m/%d/%y")
dates <- as.POSIXct(dates,tz = "GMT")
getCountrydata <- function(Country,
                           dates = dates,
                           confirmed_df = confirmed_df,
                           deaths_df = deaths_df,
                           recovered_df = recovered_df,
                           sd = sd, ed = ed) {
  if (Country == "all") {
    cases <- confirmed_df %>%
      #select(-(1:400)) %>%
      select(sd:ed) %>%
      colSums()
    death <- deaths_df %>%
      #select(-(1:400)) %>%
      select(sd:ed) %>%
      colSums()
    recovered <- recovered_df %>%
      #select(-(1:400)) %>%
      select(sd:ed) %>%
      colSums()
  }
  else {
    Country <- enquo(Country)
    cases <- confirmed_df %>%
      filter(`Country/Region` == !! Country) %>%
      #select(-(1:400)) %>%
      select(sd:ed) %>%
      colSums()
    death <- deaths_df %>%
      filter(`Country/Region` == !! Country) %>%
      #select(-(1:400)) %>%
      select(sd:ed) %>%
      colSums()
    recovered <- recovered_df %>%
      filter(`Country/Region` == !! Country) %>%
      #select(-(1:400)) %>%
      select(sd:ed) %>%
      colSums()
  }
  res.df <- tibble(dates,
                   cases = cases,
                   death = death,
                   recovery = recovered,
                   mortality_rate = death/cases,
                   recovery_rate = recovery/cases)
  return(res.df)
}
world.df <- getCountrydata(Country = "all",
                           dates = dates,
                           confirmed_df = confirmed_df,
                           deaths_df = deaths_df,
                           recovered_df = recovered_df, sd, ed)
#Taiwan
taiwan.df <- getCountrydata(Country = "Taiwan*",
                            dates = dates,
                            confirmed_df = confirmed_df,
                            deaths_df = deaths_df,
                            recovered_df = recovered_df, sd, ed)

tmp.cases.plot <- function(df.plot, Country) {
  df.plot %>%
    mutate(cases_k = cases) %>%
    ggplot( aes(x=dates, y=cases_k)) +
    geom_line(color="#69b3a2") +
    geom_point(color="#69b3a2", size=1) +
    scale_x_datetime(breaks = world.df$dates,
                     date_labels = '%m/%d')+
    ggtitle(paste0(Country," Evolution of COVID-19 cases")) +
    ylab("cases") +
    theme_cowplot() +
    theme(axis.text.x = element_text(size = 10,
                                     vjust = 0.5,
                                     hjust = 0.5,
                                     angle = 90))
}

tmp.deaths.plot <- function(df.plot, Country) {
  df.plot %>%
    ggplot( aes(x=dates, y=mortality_rate)) +
    geom_line(color="#69b3a2") +
    geom_point(color="#69b3a2", size=1) +
    scale_x_datetime(breaks = world.df$dates,
                     date_labels = '%m/%d')+
    ggtitle(paste0(Country," Evolution of COVID-19 death rates")) +
    ylab("Mortality rates(Death/Cases)") +
    theme_cowplot()+
    theme(axis.text.x = element_text(size = 10,
                                     vjust = 0.5,
                                     hjust = 0.5,
                                     angle = 90))
}
tmp.recover.plot <- function(df.plot, Country) {
  df.plot %>%
    ggplot( aes(x=dates, y=recovery_rate)) +
    geom_line(color="#69b3a2") +
    geom_point(color="#69b3a2", size=1) +
    scale_x_datetime(breaks = world.df$dates,
                     date_labels = '%m/%d') +
    scale_y_continuous(breaks=seq(0,1,0.2),limits = c(0,1)) +
    ggtitle(paste0(Country," Evolution of COVID-19 recovery rates")) +
    ylab("Recovery rates(Recovery/Cases)") +
    theme_cowplot()+
    theme(axis.text.x = element_text(size = 10,
                                     vjust = 0.5,
                                     hjust = 0.5,
                                     angle = 90))
}
#----------
sd <- length(confirmed_df) - 22; sd
ed <- sd; ed
dates <- colnames(confirmed_df[, sd:ed])
df <- getCountrydata(Country = "Taiwan*",
                     dates = dates,
                     confirmed_df = confirmed_df,
                     deaths_df = deaths_df,
                     recovered_df = recovered_df, sd, ed)
first_data <- df$cases[[1]] ; first_data
taiwan.df['daily'] <- NA; taiwan.df
for(i in 1:nrow(taiwan.df)) {
  if(i == 1)
    taiwan.df$daily[i] <- taiwan.df$cases[i]- first_data
  else
    taiwan.df$daily[i] <- taiwan.df$cases[i]- taiwan.df$cases[i - 1]
}; tail(taiwan.df, 10)

tmp.daily.plot <- function(df.plot, Country) {
  df.plot %>%
    mutate(daily = daily) %>%
    ggplot(aes(x = dates, y = daily)) +
    geom_line(color="#69b3a2") +
    geom_point(color="red", size=1) +
    scale_x_datetime(breaks = world.df$dates, date_labels = '%m/%d')+
    ggtitle(paste0(Country," Evolution of COVID-19 daily")) +
    ylab("daily") +
    theme_cowplot() +
    theme(axis.text.x = element_text(size = 10,
                                     vjust = 0.5,
                                     hjust = 0.5,
                                     angle = 90))
}
pic1 <- tmp.daily.plot(df.plot = taiwan.df, Country = "Taiwan"); pic1
pic2 <- tmp.cases.plot(df.plot = taiwan.df, Country = "Taiwan"); pic2
pic3 <- tmp.deaths.plot(df.plot = taiwan.df, Country = "Taiwan"); pic3
pic4 <- tmp.recover.plot(df.plot = taiwan.df, Country = "Taiwan"); pic4
pic1 + pic2 + pic3 + pic4 + plot_layout(ncol = 2)

輸出:



2021年5月19日 星期三

如何建立實聯制Google表單

 1.將COVID-19 防疫實聯制建立副本到自己的雲端硬碟

將Google試算表「COVID-19防疫實聯制措施」建立副本到你自己的雲端硬碟

2.點選【建立副本】

 3.此時畫面會出現: 上方會有副本字樣,雲端硬碟也會有此檔案


 4.請點選執行「創用CC授權(姓名標示)」這項功能,目的是在首次執行客製功能前會自動跳出要求你完成授權同意程序。


5.會虛線需要授權提示,按下【繼續】

6.選擇你的【帳號】:

7.點選【進階】

 8.點選【前往「COVID-19防疫實聯制措施」(不安全)】

9.點選【允許】上述條款

10. 此時再點選「創用CC授權(姓名標示)」這項功能,右下角會出現下圖的訊息就表示授權成功


 11.下拉選單【COVID-19防疫實聯制功能】,選擇【建立掃瞄表單網址QRcode】

右下角出現【已建立表單網址QRcode文件】表示QRcode已經建立


 12.到我的雲端硬碟查看會有一個COVID-19防疫實聯制措施(列印QRcode)檔案

13.點選開啟檔案就可看到QRcode


14.如果你是店家可以使用「COVID-19防疫實聯制措施之連鎖企業版

15.例如:分店號碼1234


產生的QRcode


 16.用QRcode APP掃描後填寫表單資料,在雲端硬碟的檔案即可看到掃描的資料。