knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 7.5, fig.height = 4.5 )
This tutorial assumes you already imported the WHONET data with e.g. the readxl
package. In RStudio, this can be done using the menu button 'Import Dataset' in the tab 'Environment'. Choose the option 'From Excel' and select your exported file. Make sure date fields are imported correctly.
An example syntax could look like this:
library(readxl) data <- read_excel(path = "path/to/your/file.xlsx")
This package comes with an example data set WHONET
. We will use it for this analysis.
First, load the relevant packages if you did not yet did this. I use the tidyverse for all of my analyses. All of them. If you don't know it yet, I suggest you read about it on their website: https://www.tidyverse.org/.
library(dplyr) # part of tidyverse library(ggplot2) # part of tidyverse library(AMR) # this package library(cleaner) # to create frequency tables
We will have to transform some variables to simplify and automate the analysis:
mo
) using our Catalogue of Life reference data set, which contains all ~70,000 microorganisms from the taxonomic kingdoms Bacteria, Fungi and Protozoa. We do the tranformation with as.mo()
. This function also recognises almost all WHONET abbreviations of microorganisms."S"
, "I"
or "R"
. That is exactly where the as.sir()
function is for.# transform variables data <- WHONET %>% # get microbial ID based on given organism mutate(mo = as.mo(Organism)) %>% # transform everything from "AMP_ND10" to "CIP_EE" to the new `sir` class mutate_at(vars(AMP_ND10:CIP_EE), as.sir)
No errors or warnings, so all values are transformed succesfully.
We also created a package dedicated to data cleaning and checking, called the cleaner
package. Its freq()
function can be used to create frequency tables.
So let's check our data, with a couple of frequency tables:
# our newly created `mo` variable, put in the mo_name() function data %>% freq(mo_name(mo), nmax = 10)
# our transformed antibiotic columns # amoxicillin/clavulanic acid (J01CR02) as an example data %>% freq(AMC_ND2)
An easy ggplot
will already give a lot of information, using the included ggplot_sir()
function:
data %>% group_by(Country) %>% select(Country, AMP_ND2, AMC_ED20, CAZ_ED10, CIP_ED5) %>% ggplot_sir(translate_ab = "ab", facet = "Country", datalabels = FALSE)
# on very old and some new releases of R, this may lead to an error tryCatch( data %>% group_by(Country) %>% select(Country, AMP_ND2, AMC_ED20, CAZ_ED10, CIP_ED5) %>% ggplot_sir(translate_ab = "ab", facet = "Country", datalabels = FALSE) %>% print(), error = function(e) base::invisible() )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.