graph_boxplot: Make boxplots or boxplots overlaid with individual points

View source: R/graph_boxplot.R

graph_boxplotR Documentation

Make boxplots or boxplots overlaid with individual points

Description

graph_boxplot takes as input a data.frame of numeric values and the categories those values are in and creates boxplots of those data. Options are included for type of graph to make and some aesthetics.

Usage

graph_boxplot(
  DF,
  category_column,
  value_column,
  facet1_column,
  facet2_column,
  graph_type = "boxplot",
  include_errorbars = FALSE,
  x_axis_label = NA,
  y_axis_label = NA,
  color_set = "default",
  save_graph = NA,
  fig_width = 6,
  fig_height = 4
)

Arguments

DF

the data.frame you want to graph, unquoted

category_column

the name of the column with categorical data, unquoted

value_column

the name of the column with value data, unquoted

facet1_column

(optional) the name of a column by which you might break up your graph into small multiples, unquoted. Please see the example if you're uncertain what this does.

facet2_column

(optional) the name of a second column by which you might break up the graph into small multiples, unquoted.

graph_type

the type of graph to plot. Options:

"boxplot"

(default) standard boxplots, aka box-and-whisker plots

"jittered points"

boxplots overlaid with points depicting each individual observation.

"jittered points, filled boxes"

boxplots with fill color according to category_column and open circles for the jittered points

include_errorbars

TRUE or FALSE (default) on whether to include horizontal error bars on the whiskers

x_axis_label

optionally supply a character vector or an expression to use for the x axis label

y_axis_label

optionally supply a character vector or an expression to use for the y axis label

color_set

the set of colors to use. Options:

"default"

a set of colors from Cynthia Brewer et al. from Penn State that are friendly to those with red-green colorblindness. The first three colors are green, orange, and purple. This can also be referred to as "Brewer set 2".

"Brewer set 1"

colors selected from the Brewer palette "set 1". The first three colors are red, blue, and green.

"ggplot2 default"

the default set of colors used in ggplot2 graphs (ggplot2 is an R package for graphing.)

"black"

black and white

"rainbow"

colors selected from a rainbow palette. The default palette is limited to something like 6 groups, so if you have more than that, that's when this palette is most useful.

"blue-green"

a set of blues fading into greens. This palette can be especially useful if you are comparing a systematic change in some continuous variable – for example, increasing dose or predicting how a change in intrinsic solubility will affect concentration-time profiles – because the direction of the trend will be clear.

"blues"

a set of blues fading light blue to dark blue. Like "blue-green", this palette can be especially useful if you are comparing a systematic change in some continuous variable.

"Tableau"

uses the standard Tableau palette; requires the "ggthemes" package

"viridis"

from the eponymous package by Simon Garnier and ranges colors from purple to blue to green to yellow in a manner that is "printer-friendly, perceptually uniform and easy to read by those with colorblindness", according to the package author

save_graph

optionally save the output graph by supplying a file name in quotes here, e.g., "My conc time graph.png". If you leave off ".png", it will be saved as a png file, but if you specify a different file extension, it will be saved as that file format. Acceptable extensions are "eps", "ps", "jpeg", "jpg", "tiff", "png", "bmp", or "svg". Do not include any slashes, dollar signs, or periods in the file name. Leaving this as NA means the file will not be automatically saved to disk.

fig_width

figure width in inches; default is 6

fig_height

figure height in inches; default is 4

Value

a ggplot2 graph

Examples

AUCs <- data.frame(AgeGroup = rep(LETTERS[1:5], each = 100),
                   AUC = c(rnorm(n = 100, mean = 10000, sd = 5000),
                           rnorm(n = 100, mean = 15000, sd = 5000),
                           rnorm(n = 100, mean = 20000, sd = 5000),
                           rnorm(n = 100, mean = 30000, sd = 5000),
                           rnorm(n = 100, mean = 33000, sd = 5000)))

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC)

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC,
               color_set = "rainbow")

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC,
               color_set = "blue-green", graph_type =  "jittered points")

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC,
              color_set = "Tableau", graph_type =  "jittered points, filled boxes")

# Adding a couple of example columns to use the "facet" options.
AUCs$Sex <- c("M", "F")
AUCs$Metabolizer <- sample(c("poor", "extensive"), 100, replace = TRUE)

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC,
               facet1_column = Sex,
               color_set = "blue-green", graph_type =  "jittered points")

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC,
               facet1_column = Sex, facet2_column = Metabolizer,
               color_set = "blue-green", graph_type =  "jittered points")

graph_boxplot(AUCs, category_column = AgeGroup, value_column = AUC,
              graph_type = "jittered points, filled boxes")

# Saving the output
graph_boxplot(AUCs %>% filter(AgeGroup %in% c("A", "B")),
              category_column = AgeGroup, value_column = AUC,
              graph_type = "jittered points, filled boxes",
              color_set = "Brewer set 2", include_errorbars = TRUE,
              save_graph = "test boxplot.png")



shirewoman2/Consultancy documentation built on Feb. 18, 2025, 10 p.m.