fork download
  1. library(tidyverse)
  2. library(ggplot2)
  3. library(cowplot)
  4. library(patchwork)
  5. confirmed_df <- read_csv("https://r...content-available-to-author-only...t.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
  6. deaths_df <- read_csv("https://r...content-available-to-author-only...t.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")
  7. recovered_df <- read_csv("https://r...content-available-to-author-only...t.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv")
  8.  
  9. sd <- length(confirmed_df) - 21 # start date
  10. ed <- length((confirmed_df)) # end date
  11. dates <- colnames(confirmed_df[, sd:ed])
  12. dates <- as.Date(dates,format = "%m/%d/%y")
  13. dates <- as.POSIXct(dates,tz = "GMT")
  14. getCountrydata <- function(Country,
  15. dates = dates,
  16. confirmed_df = confirmed_df,
  17. deaths_df = deaths_df,
  18. recovered_df = recovered_df,
  19. sd = sd, ed = ed) {
  20. if (Country == "all") {
  21. cases <- confirmed_df %>%
  22. #select(-(1:400)) %>%
  23. select(sd:ed) %>%
  24. colSums()
  25. death <- deaths_df %>%
  26. #select(-(1:400)) %>%
  27. select(sd:ed) %>%
  28. colSums()
  29. recovered <- recovered_df %>%
  30. #select(-(1:400)) %>%
  31. select(sd:ed) %>%
  32. colSums()
  33. }
  34. else {
  35. Country <- enquo(Country)
  36. cases <- confirmed_df %>%
  37. filter(`Country/Region` == !! Country) %>%
  38. #select(-(1:400)) %>%
  39. select(sd:ed) %>%
  40. colSums()
  41. death <- deaths_df %>%
  42. filter(`Country/Region` == !! Country) %>%
  43. #select(-(1:400)) %>%
  44. select(sd:ed) %>%
  45. colSums()
  46. recovered <- recovered_df %>%
  47. filter(`Country/Region` == !! Country) %>%
  48. #select(-(1:400)) %>%
  49. select(sd:ed) %>%
  50. colSums()
  51. }
  52. res.df <- tibble(dates,
  53. cases = cases,
  54. death = death,
  55. recovery = recovered,
  56. mortality_rate = death/cases,
  57. recovery_rate = recovery/cases)
  58. return(res.df)
  59. }
  60. world.df <- getCountrydata(Country = "all",
  61. dates = dates,
  62. confirmed_df = confirmed_df,
  63. deaths_df = deaths_df,
  64. recovered_df = recovered_df, sd, ed)
  65. #Taiwan
  66. taiwan.df <- getCountrydata(Country = "Taiwan*",
  67. dates = dates,
  68. confirmed_df = confirmed_df,
  69. deaths_df = deaths_df,
  70. recovered_df = recovered_df, sd, ed)
  71.  
  72. tmp.cases.plot <- function(df.plot, Country) {
  73. df.plot %>%
  74. mutate(cases_k = cases) %>%
  75. ggplot( aes(x=dates, y=cases_k)) +
  76. geom_line(color="#69b3a2") +
  77. geom_point(color="#69b3a2", size=1) +
  78. scale_x_datetime(breaks = world.df$dates,
  79. date_labels = '%m/%d')+
  80. ggtitle(paste0(Country," Evolution of COVID-19 cases")) +
  81. ylab("cases") +
  82. theme_cowplot() +
  83. theme(axis.text.x = element_text(size = 10,
  84. vjust = 0.5,
  85. hjust = 0.5,
  86. angle = 90))
  87. }
  88.  
  89. tmp.deaths.plot <- function(df.plot, Country) {
  90. df.plot %>%
  91. ggplot( aes(x=dates, y=mortality_rate)) +
  92. geom_line(color="#69b3a2") +
  93. geom_point(color="#69b3a2", size=1) +
  94. scale_x_datetime(breaks = world.df$dates,
  95. date_labels = '%m/%d')+
  96. ggtitle(paste0(Country," Evolution of COVID-19 death rates")) +
  97. ylab("Mortality rates(Death/Cases)") +
  98. theme_cowplot()+
  99. theme(axis.text.x = element_text(size = 10,
  100. vjust = 0.5,
  101. hjust = 0.5,
  102. angle = 90))
  103. }
  104. tmp.recover.plot <- function(df.plot, Country) {
  105. df.plot %>%
  106. ggplot( aes(x=dates, y=recovery_rate)) +
  107. geom_line(color="#69b3a2") +
  108. geom_point(color="#69b3a2", size=1) +
  109. scale_x_datetime(breaks = world.df$dates,
  110. date_labels = '%m/%d') +
  111. scale_y_continuous(breaks=seq(0,1,0.2),limits = c(0,1)) +
  112. ggtitle(paste0(Country," Evolution of COVID-19 recovery rates")) +
  113. ylab("Recovery rates(Recovery/Cases)") +
  114. theme_cowplot()+
  115. theme(axis.text.x = element_text(size = 10,
  116. vjust = 0.5,
  117. hjust = 0.5,
  118. angle = 90))
  119. }
  120. #----------
  121. sd <- length(confirmed_df) - 22; sd
  122. ed <- sd; ed
  123. dates <- colnames(confirmed_df[, sd:ed])
  124. df <- getCountrydata(Country = "Taiwan*",
  125. dates = dates,
  126. confirmed_df = confirmed_df,
  127. deaths_df = deaths_df,
  128. recovered_df = recovered_df, sd, ed)
  129. first_data <- df$cases[[1]] ; first_data
  130. taiwan.df['daily'] <- NA; taiwan.df
  131. for(i in 1:nrow(taiwan.df)) {
  132. if(i == 1)
  133. taiwan.df$daily[i] <- taiwan.df$cases[i]- first_data
  134. else
  135. taiwan.df$daily[i] <- taiwan.df$cases[i]- taiwan.df$cases[i - 1]
  136. }; tail(taiwan.df, 10)
  137.  
  138. tmp.daily.plot <- function(df.plot, Country) {
  139. df.plot %>%
  140. mutate(daily = daily) %>%
  141. ggplot(aes(x = dates, y = daily)) +
  142. geom_line(color="#69b3a2") +
  143. geom_point(color="red", size=1) +
  144. scale_x_datetime(breaks = world.df$dates, date_labels = '%m/%d')+
  145. ggtitle(paste0(Country," Evolution of COVID-19 daily")) +
  146. ylab("daily") +
  147. theme_cowplot() +
  148. theme(axis.text.x = element_text(size = 10,
  149. vjust = 0.5,
  150. hjust = 0.5,
  151. angle = 90))
  152. }
  153. pic1 <- tmp.daily.plot(df.plot = taiwan.df, Country = "Taiwan"); pic1
  154. pic2 <- tmp.cases.plot(df.plot = taiwan.df, Country = "Taiwan"); pic2
  155. pic3 <- tmp.deaths.plot(df.plot = taiwan.df, Country = "Taiwan"); pic3
  156. pic4 <- tmp.recover.plot(df.plot = taiwan.df, Country = "Taiwan"); pic4
  157. pic1 + pic2 + pic3 + pic4 + plot_layout(ncol = 2)
  158.  
  159. ggplot(taiwan.df, aes(x = dates, y = taiwan$daily ))
  160. #Bar char
  161. ggplot(taiwan.df, aes(x = dates, y = daily)) +
  162. geom_bar(stat = "identity", fill = "lightblue") +
  163. scale_x_datetime(breaks = world.df$dates, date_labels = '%m/%d')
  164.  
Success #stdin #stdout #stderr 0.21s 39088KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error in library(tidyverse) : there is no package called ‘tidyverse’
Execution halted