R/dataviz.R

Defines functions make_spaghetti_plots make_density make_histogram make_bar_plot

# Data visualization ------------------------------------------------------
#' Bar charts for categorical variables
#' 
#' Make bar charts categorical variables in a data.frame
#' 
#' @param data A data.frame
#' @param var A character vector with the names of the categorical variables in data.frame.
#' @param by A character value giving the name of the variable to group by (e.g. event status)
#' 
#' @import ggplot2
#' @import gridExtra
#' 
#' @export
#' @examples 
#' make_bar_plot(data = cars, var = mpg, by = cil)
#' 
make_bar_plot <- function(data, var, by = NULL) {
  if (is.null(by)) {
    bar_plot <- ggplot(data = data) +
      geom_bar(aes(x = get(var), fill = get(var))) +
      theme_minimal() +
      labs(x = "", y = "") +
      guides(fill = guide_legend(title = paste(var)))
    bar_plot
  } else {
    stopifnot("by should be a character or NULL"=is.character(by))
    bar_plot <- ggplot(data = data) +
      geom_bar(aes(x = get(var), fill = get(var))) +
      theme_minimal() +
      labs(x = "", y = "") +
      guides(fill = guide_legend(title = paste(var))) +
      facet_grid(cols = vars(get(by)))
    bar_plot
  }
}

#' Histograms for continuous variables
#' 
#' Make histograms for continuous variable in your data.frame
#' 
#' @param data A data.frame
#' @param var a character vector with the names of the numeric variables in data.frame.
#' 
#' @import ggplot2
#' @import gridExtra
#' 
#' @export
make_histogram <- function(data, var) {
  plot <- ggplot() +
    geom_histogram(data = data, aes(x = get(var)), bins = 20) +
    theme_classic() +
    labs(x = paste("histogram of", var))
  plot
}

#' Density plots for continuous variables
#' 
#' Make density plots for continuous variable in your data.frame with option
#' to show data by outcome
#' 
#' @param data A data.frame
#' @param var a character vector with the names of the numeric variables in data.frame.
#' @param by A character value giving the name of the variable to group by (e.g. event status)
#' 
#' @import ggplot2
#' @import gridExtra
#' 
#' @export
make_density <- function(data, var, by = NULL) {
  if (is.null(by)) {
    plot <- ggplot() +
      ggplot() +
      geom_density(data = data, aes(x = var), alpha = 0.5) +
      theme_classic() +
      labs(x = paste("density plot of", var))
  } else {
    stopifnot("by should be a character or NULL"=is.character(by))
    plot <- ggplot() +
      geom_density(data = data, aes(x = get(var), fill = get(by)), alpha = 0.5) +
      theme_classic() +
      labs(x = paste("density plot of", var))
  }
}


#' Spaghetti plots for longitudinal variables
#' 
#' Make spaghetti plots for longintudinal variables in a data.frame.
#' 
#' @param data A data.frame in long format.
#' @param time A character value giving the name of the time variable.
#' @param var A character value giving the name of the longitudinal variable.
#' @param id A character value giving the name of the variable with the subject identifier.
#' @param by A character value giving the name of the variable to group the trajectories by (e.g. event status)
#' @param breaks A numeric value with the major tick mark for the x-axis.
#' 
#' @import dplyr
#' @import ggplot2
#' @import gridExtra
make_spaghetti_plots <- function(data, ftime, var, id, by = NULL, breaks = 1) {
  if (is.null(by) ) {
    plot <- ggplot(data = data, 
                   aes(x = get(ftime), y = get(var), group = get(id))) +
      geom_jitter(size = 0.2, alpha = 0.3, width = 0.01, height = 0.01) +
      geom_line(size = 0.1, alpha = 0.2) +
      geom_smooth(data = data, aes(x = get(ftime), y = get(var)), method = 'loess') +
      scale_x_continuous(breaks = scales::breaks_width(breaks)) +
      theme_classic() +
      labs(x = "Follow-up time", y = paste(var)) 
  } else {
    plot <- ggplot(data = data, 
                   aes(x = get(ftime), y = get(var), group = get(id), color = as.factor(get(by)))) +
      geom_jitter(size = 0.2, alpha = 0.3, width = 0.01, height = 0.01) +
      geom_line(size = 0.1, alpha = 0.2) +
      geom_smooth(data = data, 
                  aes(x = get(ftime), y = get(var), color = as.factor(get(by))), method = 'loess') +
      scale_x_continuous(breaks = scales::breaks_width(breaks)) +
      theme_classic() +
      labs(x = "Follow-up time", 
           y = paste(var),
           color = by) + 
      theme(legend.position="none")
  }
  plot
}
JanvandenBrand/highdimjm documentation built on Dec. 18, 2021, 12:32 a.m.