Home > Back-end >  Is it possible to turn an R incidence object to a dataframe and use this for plotting?
Is it possible to turn an R incidence object to a dataframe and use this for plotting?

Time:06-15

Is it possible to convert an incidence object into a dataframe and then somehow use this for plotting? The reason I need to do this is because I have to export a file in CSV format out of a secure server (incidence objects not allowed and raw data not allowed - but monthly/weekly counts fine) and generate the plot outside of the server. E.g:

library(outbreaks)
library(ggplot2)
library(incidence)

dat <- ebola_sim$linelist$date_of_onset
i <- incidence(dat)
plot(i)

i_df <- as.data.frame(i)

Is there a way to generate the same plot using the raw data in the "i_df" dataframe? Can it be turned back into an incidence object for example?

CodePudding user response:

The incidence:::incidence.plot() function is basically a wrapper around ggplot()/geom_col().. For your specific example dataset, you can basically do

ggplot(i_df, aes(dates,counts))   geom_col(width=1)

to replicate the basic content of the output of plot(i); for full replication, you can then update the labels, axis, etc.

As to your second question, you can recover the equivalent incidence object from i_df, by doing this:

incidence(rep(i_df$dates, i_df$counts))
  • Related