0

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?

1
  • could you share dput(dat)?
    – maarvd
    Jun 15, 2022 at 11:04

1 Answer 1

1

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))

If you have created an incidence object using the groups parameter, the recovery approach is similar, but you have to treat each of the groups separately (if there are lots of groups, I would suggest some kind of loop structure). Example, when groups=gender

# creation of grouped incidence object
i.sex <- incidence(ebola_sim$linelist$date_of_onset, groups = ebola_sim$linelist$gender)

# conversion to data.frame
i.sex_df = as.data.frame(i.sex)

# recovery back to an equivalent incidence object
incidence(
  c(rep(i.sex_df$dates, i.sex_df$f),rep(i.sex_df$dates, i.sex_df$m)),
  groups = c(rep("f",sum(i.sex_df$f)), rep("m",sum(i.sex_df$m)))
)


2
  • any idea how to recover the incidence object if you have set a "group"? e.g. ``` i.sex <- incidence(onset, interval = 7, group = ebola_sim_clean$linelist$gender) ```
    – krtbris
    Jun 15, 2022 at 15:13
  • see edit for group wise recovery.. note that I did not have access to your onset or your ebola_sim_clean objects
    – langtang
    Jun 15, 2022 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.