reconhub / incidence

☣:chart_with_upwards_trend::chart_with_downwards_trend:☣ Compute and visualise incidence
https://reconhub.github.io/incidence
Other
58 stars 13 forks source link

plot.incidence does not take column order into account #77

Closed zkamvar closed 5 years ago

zkamvar commented 5 years ago

Brought up on the RECON slack channel, there was a question about the default ordering of groups in plot.incidence(). It appears that the order of the columns is not respected.

library("incidence")
library("ggplot2")
g <- sample(LETTERS[1:5], 100, replace = TRUE)
g <- factor(g, levels = LETTERS[5:1])
i <- incidence(rpois(100, 10), groups = g)
i
#> <incidence object>
#> [100 cases from days 3 to 20]
#> [5 groups: A, B, C, D, E]
#> 
#> $counts: matrix with 18 rows and 5 columns
#> $n: 100 cases in total
#> $dates: 18 dates marking the left-side of bins
#> $interval: 1 day
#> $timespan: 18 days
#> $cumulative: FALSE
i$counts <- i$counts[, sample(5)]
i
#> <incidence object>
#> [100 cases from days 3 to 20]
#> [5 groups: B, E, A, C, D]
#> 
#> $counts: matrix with 18 rows and 5 columns
#> $n: 100 cases in total
#> $dates: 18 dates marking the left-side of bins
#> $interval: 1 day
#> $timespan: 18 days
#> $cumulative: FALSE
plot(i) 

It is possible to change the groups to the correct order with the following ggplot2 incantation, but ideally this should happen automatically.

plot(i) + aes(fill = factor(groups, unique(groups)))

Note that this also illustrates a problem where factor levels are not respected when creating the initial grouping table. Created on 2018-11-14 by the reprex package (v0.2.1)