Usage and Demo of R Package Ncov2019

Usage and Demo of R Package Ncov2019

Usage and demo of R package nCov2019 Installation Query the latest data Access detailed historical data Geographic map visualization Shiny Dashboard Installation To start off, users could utilize the ‘remotes’ package to install it directly from GitHub by running the following in R: require('remotes') remotes::install_github("GuangchuangYu/nCov2019", dependencies = TRUE) Query the latest data To query the latest data, you can load it in with get_nCov2019(). By default, the language setting is automatically set to Chinese or English based on the user's system environment. Of course, users can also use parameter lang = 'zh' or lang = 'en' to set it explicitly. Since most of confirmed cases concentrated in China, researchers may more concern about the details in China. So, print the object x, you could get the total number of confirmed cases in China. library('nCov2019') x <- get_nCov2019(lang = 'en') x ## China (total confirmed cases): 79968 ## last update: 2020-03-01 12:40:24 And then you could use summary(x) to get recent Chinese data. head(summary(x)) ## confirm suspect dead heal nowConfirm nowSevere deadRate healRate date ## 1 41 0 1 0 0 0 2.4 0.0 01.13 ## 2 41 0 1 0 0 0 2.4 0.0 01.14 ## 3 41 0 2 5 0 0 4.9 12.2 01.15 ## 4 45 0 2 8 0 0 4.4 17.8 01.16 ## 5 62 0 2 12 0 0 3.2 19.4 01.17 ## 6 198 0 3 17 0 0 1.5 8.6 01.18 While no region is specified, x[] will return the provincial level outbreak statistics in China. head(x[]) ## name confirm suspect dead deadRate showRate heal healRate showHeal ## 1 Hubei 66907 0 2761 4.13 FALSE 31187 46.61 TRUE ## 2 Guangdong 1349 0 7 0.52 FALSE 1009 74.80 TRUE ## 3 Henan 1272 0 22 1.73 FALSE 1185 93.16 TRUE ## 4 Zhejiang 1205 0 1 0.08 FALSE 1027 85.23 TRUE ## 5 Hunan 1018 0 4 0.39 FALSE 853 83.79 TRUE ## 6 Anhui 990 0 6 0.61 FALSE 888 89.70 TRUE To obtain a more granular scale data, you only need to specify the province name. For example, to obtain data in Hubei Province. head(x['Hubei']) ## name confirm suspect dead deadRate showRate heal healRate showHeal ## 1 Wuhan 49122 0 2195 4.47 FALSE 19227 39.14 TRUE ## 2 Xiaogan 3518 0 118 3.35 FALSE 2215 62.96 TRUE ## 3 Huanggang 2905 0 115 3.96 FALSE 2171 74.73 TRUE ## 4 Jingzhou 1579 0 46 2.91 FALSE 1034 65.48 TRUE ## 5 Ezhou 1391 0 47 3.38 FALSE 784 56.36 TRUE ## 6 Suizhou 1307 0 40 3.06 FALSE 835 63.89 TRUE In addition, by using the argument by = 'today', the number of newly added cases will be return. head(x['Hubei', by = 'today']) ## name confirm confirmCuts isUpdated ## 1 Wuhan 565 0 TRUE ## 2 Xiaogan 0 0 TRUE ## 3 Huanggang 1 0 TRUE ## 4 Jingzhou 0 0 TRUE ## 5 Ezhou 1 0 TRUE ## 6 Suizhou 0 0 TRUE Getting global data is also easy, by using x ['global'], the data frame for the global landscape view of each country will be returned. head(x['global']) ## name confirm suspect dead deadRate showRate heal healRate ## 1 China 79968 851 2873 3.59 FALSE 41675 52.11 ## 2 Republic of Korea 3150 0 13 0.41 FALSE 24 0.76 ## 3 Diamond Princess 705 0 6 0.85 FALSE 0 0.00 ## 4 Italy 653 0 17 2.60 FALSE 45 6.89 ## 5 Iran 270 0 26 9.63 FALSE 49 18.15 ## 6 Japan 240 0 5 2.08 FALSE 1 0.42 If you wanted to visualize the cumulative summary data, an example plot could be the following: require(ggplot2) ggplot(summary(x), aes(as.Date(date, "%m.%d"), as.numeric(confirm))) + geom_col(fill = 'firebrick') + theme_minimal(base_size = 14) + xlab(NULL) + ylab(NULL) + scale_x_date(date_labels = "%Y/%m/%d") + labs(caption = paste("accessed date:", time(x))) And the bar-plot of the latest confirmed diagnosis in Anhui province could be plotted as follow: library(ggplot2) d = x['Anhui', ] # you can replace Anhui with any province d = d[order(d$confirm), ] ggplot(d, aes(name, as.numeric(confirm))) + geom_col(fill = 'firebrick') + theme_minimal(base_size = 14) + xlab(NULL) + ylab(NULL) + labs(caption = paste("accessed date:", time(x))) + scale_x_discrete(limits = d$name) + coord_flip() Access detailed historical data The method for accessing historical data is basically the same as getting the latest data, but entry function is load_nCov2019(). library('nCov2019') y <- load_nCov2019(lang = 'en') y # this will return update time of historical data ## nCov2019 historical data ## last update: 2020-02-29 For the historical data, currently, we maintain three historical data, one of which is collected and organized from GitHub repo, user will access it by default, or use load_nCov2019(source = 'github') to get it. The second one is obtained from an Chinese website Dingxiangyuan and user could access it by using load_nCov2019(source = 'dxy'). And the last one is obtained from the National Health Commission of Chinese, user could get it by using argument source = 'cnnhc'. The forms of these data are basically the same, but the default data source has more comprehensive global historical information and also contains older historical data. Users can compare and switch data from different sources. # compare the total confirmed cases in china between data sources library(nCov2019) library(ggplot2) y = load_nCov2019(lang = 'en', source = 'github') dxy = load_nCov2019(lang = 'en', source = 'dxy') nhc = load_nCov2019(lang = 'en', source = 'cnnhc') dxy_china <- aggregate(cum_confirm ~ + time, summary(dxy), sum) y_china <- aggregate(cum_confirm ~ + time, summary(y), sum) nhc_china <- aggregate(cum_confirm ~ + time, summary(nhc), sum) dxy_china$source = 'DXY data' y_china$source = 'GitHub data' nhc_china$source = 'NHC data' df = rbind(dxy_china, y_china, nhc_china) ggplot(subset(df, time >= '2020-01-11'), aes(time,cum_confirm, color = source)) + geom_line() + scale_x_date(date_labels = "%Y-%m-%d") + ylab('Confirmed Cases in China') + xlab('Time') + theme_bw() + theme(axis.text.x = element_text(hjust = 1)) + theme(legend.position = 'bottom') Then you can use summary(y) to get historical data at the provincial level in China: head(summary(y)) ## time province cum_confirm cum_heal cum_dead suspected ## 1 2019-12-01 Hubei 1 0 0 0 ## 2 2019-12-02 Hubei 1 0 0 0 ## 3 2019-12-03 Hubei 1 0 0 0 ## 4 2019-12-04 Hubei 1 0 0 0 ## 5 2019-12-05 Hubei 1 0 0 0 ## 6 2019-12-06 Hubei 1 0 0 0 To get historical data for all cities in China, you can use y[] as follow: head(y[]) ## time province city cum_confirm cum_heal cum_dead suspected ## 1 2019-12-01 Hubei Wuhan 1 0 0 0 ## 2 2019-12-02 Hubei Wuhan 1 0 0 0 ## 3 2019-12-03 Hubei Wuhan 1 0 0 0 ## 4 2019-12-04 Hubei Wuhan 1 0 0 0 ## 5 2019-12-05 Hubei Wuhan 1 0 0 0 ## 6 2019-12-06 Hubei Wuhan 1 0 0 0 You can also specify a province name to get the corresponding historical data, for example, extracting historical data from Anhui Province: head(y['Anhui']) ## time province city cum_confirm cum_heal cum_dead suspected ## 71 2020-01-21 Anhui Hefei 0 0 0 1 ## 108 2020-01-22 Anhui Hefei 1 0 0 3 ## 109 2020-01-22 Anhui Lu'an 0 0 0 1 ## 197 2020-01-23 Anhui Hefei 6 0 0 0 ## 198 2020-01-23 Anhui Bengbu 1 0 0 0 ## 199 2020-01-23 Anhui Anqing 1 0 0 0 Similarly, you can get global historical data by specifying the 'global' parameter. y <- load_nCov2019(lang = 'en', source='github') head(y['global']) ## time country cum_confirm cum_heal cum_dead ## 1 2019-12-01 China 1 0 0 ## 2 2019-12-02 China 1 0 0 ## 3 2019-12-03 China 1 0 0 ## 4 2019-12-04 China 1 0 0 ## 5 2019-12-05 China 1 0 0 ## 6 2019-12-06 China 1 0 0 NOTE: The global historical data is not available from source 'dxy'. Here are some visualization examples with the historical data. 1. Draw a curve reflecting the number of deaths, confirms, and cures in China. require('tidyr') require('ggrepel') require('ggplot2') y <- load_nCov2019(lang = 'en') d <- subset(y['global'], country == 'China') d <- gather(d, curve, count, -time, -country) ggplot(d, aes(time, count, color = curve)) + geom_point() + geom_line() + xlab(NULL) + ylab(NULL) + theme_bw() + theme(legend.position = "none") + geom_text_repel(aes(label = curve), data = d[d$time == time(y), ], hjust = 1) + theme(axis.text.x = element_text(angle = 15, hjust = 1)) + scale_x_date(date_labels = "%Y-%m-%d", limits = c(as.Date("2020-01-15"), as.Date("2020-03-01"))) + labs(title="Number of deaths, confirms, and cures in China") 2. Outbreak Trend Curves of Countries Around the World (except China). require('ggrepel') require('ggplot2') y <- load_nCov2019(lang = 'en') d <- subset(y['global'], country != "China") ggplot(d, aes(time, as.numeric(cum_confirm), group = country, color = country)) + geom_point() + geom_line() + geom_label_repel(aes(label = country), data = d[d$time == time(y), ], hjust = 1) + theme_bw() + theme(legend.position = 'none') + xlab(NULL) + ylab(NULL) + scale_x_date(date_labels = "%Y-%m-%d", limits = c(as.Date("2020-02-01"), as.Date("2020-03-01"))) + theme(axis.text.x = element_text(angle = 15, hjust = 1)) + labs(title = "Outbreak Trend Curves of Countries Around the World \n (except China)") Outbreak Trend Curves of Countries Around the World (except China) ● 3000 Republic of Korea ● 2000 ● Italy Norway United States USA ● Japan Croatia Bahrain Germany Oman 1000 ● ● ● ● United Kingdom ● ● ● Thailand● ● Singapore Russia Spain ● ● ● ● ● ● ● Kuwait Iran Greece Canada Malaysia ● ● ● ● ● ●France ●Netherlands Switz● erland Vietnam Australia ● ● ● ● ● ● ● ● Austria ● Israel SwedenUnited ●Arab Emirates● India ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Iraq Nepal RepubNigerialika SevNeerAfghanistanLithuaniawCambodianaBelgiumSr Zealand QatariMak PhilippinesAzLankaDenmarRomaniaLebanonPerbaijanGeorgiaBelarEstoniaFinlandIcelandedonijaakistanMeAlgerEgyptBrxicoazilusiak 03 10 17 24 02 −02− −02− −02− −02− −03− 2020 2020 2020 2020 2020 3.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us