CTU-Bern/kpitools

plot function that shows all levels of the hierarchy

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
Useful to have all info in one plot

grafik

Describe the solution you'd like

Describe alternatives you've considered
manual

Additional context

Something like this...

Probably needs an lapply rather than the first three subsets. And some way to define what level to use...

bubblekpiplot <- function(kpis = kpis, kpi, title, lkup){
  bubbledat <- kpis %>%
    `[[`(., kpi) %>%
    `[[`(., "lowest_level") %>%
    `[[`(., "calc") %>%
    left_join(lkup)
  rowdat<- kpis %>%
    `[[`(., kpi) %>%
    `[[`(., "upper_level") %>%
    `[[`(., "calc")
  overall <- kpis %>%
    `[[`(., kpi) %>%
    `[[`(., "overall") %>%
    `[[`(., "calc")
  bubbledat %>%
    ggplot(aes(x = stat, y = upper_level, size = N, pch = "Site")) +
    geom_vline(aes(xintercept = stat), data = overall, lty = 2, col = "grey") +
    geom_point(aes(x = stat, y = upper_level,
               pch = "Level"),
               data = rowdat,
               inherit.aes = FALSE,
               # size = 2
               ) +
    geom_point(col = unibeRed(.5)) +
    myggtheme() +
    # xlim(0,100) +
    scale_y_discrete(limits=rev) +
    ggtitle(title) +
    guides(size = guide_legend(title = "N participants in site",
                               fill = NA,
                               order = 2,
                               nrow = 1),
           shape = guide_legend(order = 1, col = "black")) +
    theme(plot.title.position = "plot",
          axis.title.x = element_blank()) +
    scale_shape_manual(values = c(Site = 1, Country = 3),
                       name = "",
                       guide = guide_legend(override.aes = list(col = c("black", unibeRed(.5)),
                                                                size = 2))) +
    scale_size_continuous(guide = guide_legend(nrow = 1,
                                               override.aes = list(fill = NA),
                                               fill = NA)) +
    labs(caption = "The dashed vertical line represents the trial as a whole")
}