R/plot_missing_data_detailed.R

Defines functions plot_missing_data_detailed

Documented in plot_missing_data_detailed

#' Plot the missing data, for specified test batteries and patients
#'
#' @param missing_data_percentages The tibble generated by calculate_missing_data.
#' @param id_var A quosure representing the column that contains the patient label.
#' @param this_patient The string representing the patient of interest (in the column defined by id_var)
#' @param this_visit The string representing the visit of interest
#'
#' @importFrom cowplot plot_grid
#' @importFrom dplyr pull
#' @importFrom dplyr filter
#' @importFrom dplyr mutate
#' @importFrom dplyr %>%
#' @importFrom ggplot2 facet_grid
#' @importFrom ggplot2 geom_text
#' @importFrom ggplot2 geom_tile
#' @importFrom ggplot2 geom_bar
#' @importFrom ggplot2 ggplot_build
#' @importFrom ggplot2 ggtitle
#' @importFrom ggplot2 ggplot_gtable
#' @importFrom ggplot2 theme
#' @importFrom ggplot2 element_text
#' @importFrom ggplot2 element_blank
#' @importFrom ggplot2 scale_alpha_manual
#' @importFrom ggplot2 scale_fill_distiller
#' @importFrom forcats fct_rev
#' @importFrom grid grid.draw
#' @importFrom grid unit
#' @importFrom RColorBrewer brewer.pal
#'
#' @export
plot_missing_data_detailed = function( missing_data_percentages,
                                       id_var,
                                       this_patient,
                                       this_visit ) {

  ### Patient level plot (over all missing data)

  patient_visit_testbattery_plot.d = missing_data_percentages %>%
    filter( !!id_var == this_patient ) %>%
    filter( .data$param_visit == this_visit ) %>%
    select( {{id_var}},
            .data$param_visit,
            .data$param_testbattery,
            .data$Label_Visit_testbattery_perc_missing ) %>%
    unique %>%
    mutate( perc_missing = .data$Label_Visit_testbattery_perc_missing ) %>%
    mutate( perc_present = 100-.data$perc_missing ) %>%
    select( -.data$Label_Visit_testbattery_perc_missing ) %>%
    pivot_longer( -c({{id_var}},
                     .data$param_visit,
                     .data$param_testbattery),
                  names_to = "variable",
                  values_to = "value" ) %>%
    filter( !is.na( .data$param_visit ) ) %>%
    # filter( .data$variable == "perc_missing" ) %>%
    mutate( param_visit = fct_rev(.data$param_visit) ) %>%
    mutate( battery_label = sprintf( "%s/%s", .data$param_visit, .data$param_testbattery ) ) #%>%
    #mutate( value = na_if( value, 0 ) )


  patient_visit_testbattery_plot = ggplot( patient_visit_testbattery_plot.d,
                         aes( x=.data$param_testbattery,
                              y=.data$value,
                              alpha=.data$variable ) ) +
    geom_bar( fill=brewer.pal(3,"Paired")[2],
              stat="identity" ) +
    ggtitle( sprintf( "%s: %s", this_patient, this_visit ) ) +
    xlab( "" ) +
    ylab( "% data present (overall)" ) +
    scale_alpha_manual( values=c( "perc_missing" = 0.3,
                                  "perc_present" = 1 ),
                        guide=FALSE ) +
    theme_bw( ) +
    theme( axis.text.x = element_text(angle=90,
                                      hjust=1) ) #+
    # theme( plot.margin = unit(c(0, 5.5, 0, 5.5), "pt")) # TOP RIGHT BOTTOM LEFT

  # patient_visit_plot =
  # ggplot( patient_visit_plot.d,
  #         aes( x=Label,
  #              y=value,
  #              alpha=variable,
  #              fill=Hospital ) ) +
  #   geom_bar( stat="identity" ) +
  #   facet_grid(param_visit~.) +
  #   scale_alpha_manual( values=c( perc_missing = 0.3,
  #                                 perc_present = 1 ) ) +
  #   theme( axis.text.x = element_text( angle=90 ) )



  plot(patient_visit_testbattery_plot)
}
LisaHopcroft/CTutils documentation built on Oct. 7, 2021, 11:08 p.m.