Skip to content

Commit

Permalink
Summary plot function
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerssam committed May 21, 2024
1 parent 2ce229c commit d86812c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions R/summary_graph.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' Summary Graph
#'
#' Produce a graphical summary of variables from a data frame. Variables are plotted
#' as box plots, grouped and, if enough variables are provided, coloured and facetted.
#'
#' @param data A data frame containing the variables to be plotted.
#' @param response The response variable to plot.
#' @param exp_var The explanatory (or grouping) variable to plot.
#' @param resp_units A string providing units to display on the response variable axis.
#'
#' @return A ggplot2 plot
#' @export
#'
#' @examples
#'
#' summary_graph(iris, "Petal.Length", "Species", "mm")
#' summary_graph(npk, "yield", c("N", "P", "K"), "lb/plot")
#'
summary_graph <- function(data, response, exp_var, resp_units, alpha = 0.3){
# What if data is not present?
# What if data is not a data frame?
# What if response is misspelt?
# What if response is an invalid type?
# What if >2 exp_var?
#

if(length(exp_var)==1){
gg <- ggplot2::ggplot(data = data, ggplot2::aes(x = .data[[exp_var]], y = .data[[response]])) +
ggplot2::geom_boxplot() +
ggplot2::geom_point(alpha = alpha) +
ggplot2::labs(y = paste(response, " (", resp_units, ")", sep = "")) +
ggplot2::theme_bw()
}
else if(length(exp_var) == 2){
gg <- ggplot2::ggplot(data = data, ggplot2::aes(x = .data[[exp_var[1]]], y = .data[[response]],
colour = .data[[exp_var[2]]], group = .data[[exp_var[2]]])) +
ggplot2::stat_summary(fun = mean, geom = "point") +
ggplot2::stat_summary(fun = mean, geom = "line") +
ggplot2::geom_point(alpha = alpha) +
ggplot2::labs(y = paste(response, " (", resp_units, ")", sep = "")) +
ggplot2::theme_bw()
}
else {
gg <- ggplot2::ggplot(data = data, ggplot2::aes(x = .data[[exp_var[1]]], y = .data[[response]],
colour = .data[[exp_var[2]]], group = .data[[exp_var[2]]])) +
ggplot2::stat_summary(fun = mean, geom = "point") +
ggplot2::stat_summary(fun = mean, geom = "line") +
ggplot2::facet_wrap(~ .data[[exp_var[3]]]) +
ggplot2::geom_point(alpha = alpha) +
ggplot2::labs(y = paste(response, " (", resp_units, ")", sep = "")) +
ggplot2::theme_bw()
}

return(gg)
}

0 comments on commit d86812c

Please sign in to comment.