|
| 1 | +#' Information about a SuperPlot |
| 2 | +#' |
| 3 | +#' This function prints information about a SuperPlot, it runs internally from |
| 4 | +#' superplot() |
| 5 | +#' |
| 6 | +#' @param df data frame with at least three columns: meas, cond, repl |
| 7 | +#' @param meas character name of column with measurement (e.g. intensity) |
| 8 | +#' @param cond character name of column with condition (e.g. Control, WT) |
| 9 | +#' @param repl character name of column with replicate (e.g. unique experiment |
| 10 | +#' identifiers) |
| 11 | +#' @param pal argument passed to pal |
| 12 | +#' @param xlab argument passed to xlab |
| 13 | +#' @param ylab argument passed to ylab |
| 14 | +#' @param datadist argument passed to datadist |
| 15 | +#' @param size argument passed to size |
| 16 | +#' @param alpha argument passed to alpha |
| 17 | +#' @param bars argument passed to bars |
| 18 | +#' @param linking argument passed to linking |
| 19 | +#' @param rep_summary argument passed to rep_summary |
| 20 | +#' @param shapes argument passed to shapes |
| 21 | +#' @param fsize argument passed to fsize |
| 22 | +#' @param gg argument passed to gg |
| 23 | +#' @param stats argument passed to stats |
| 24 | +#' @param stats_test argument passed to stats_test |
| 25 | +#' @param ... additional arguments |
| 26 | +#' |
| 27 | +#' @import dplyr |
| 28 | +#' @importFrom stats sd median |
| 29 | +#' @importFrom rlang := |
| 30 | +#' |
| 31 | +#' @returns none |
| 32 | +#' @keywords internal |
| 33 | + |
| 34 | +# this function will take all the arguments sent from superplot |
| 35 | +get_sp_info <- function(df, |
| 36 | + meas, cond, repl, |
| 37 | + pal, xlab, ylab, datadist, size, alpha, bars, |
| 38 | + linking, rep_summary, shapes, fsize, gg, |
| 39 | + stats, stats_test, |
| 40 | + ...) { |
| 41 | + ncond <- nrepl <- NULL |
| 42 | + rep_mean <- rep_median <- NULL |
| 43 | + |
| 44 | + # args are already validated |
| 45 | + |
| 46 | + # how many unique values in cond and repl? |
| 47 | + ncond <- df %>% |
| 48 | + pull(!!sym(cond)) %>% |
| 49 | + unique() %>% |
| 50 | + length() |
| 51 | + nrepl <- df %>% |
| 52 | + pull(!!sym(repl)) %>% |
| 53 | + unique() %>% |
| 54 | + length() |
| 55 | + |
| 56 | + # calculate summary statistics |
| 57 | + summary_df <- get_sp_summary(df = df, |
| 58 | + meas = meas, cond = cond, repl = repl) |
| 59 | + |
| 60 | + # get colour values for the repl column |
| 61 | + sp_colours <- get_sp_colours(nrepl, pal) |
| 62 | + sp_shapes <- get_sp_shapes(nrepl, shapes) |
| 63 | + |
| 64 | + # add repl from summary_df to sp_colour and sp_shapes |
| 65 | + # repl is converted to factor so that the colours and shapes |
| 66 | + # are assigned in the order of the factor levels |
| 67 | + # but only if repl is not already a factor |
| 68 | + if (!is.factor(df[[repl]])) { |
| 69 | + summary_df <- summary_df %>% |
| 70 | + mutate(!!sym(repl) := factor(!!sym(repl), levels = unique(df[[repl]]))) |
| 71 | + } |
| 72 | + summary_df <- summary_df %>% |
| 73 | + mutate( |
| 74 | + sp_colour = factor(!!sym(repl), levels = unique(df[[repl]]), |
| 75 | + labels = sp_colours), |
| 76 | + sp_shape = factor(!!sym(repl), levels = unique(df[[repl]]), |
| 77 | + labels = sp_shapes) |
| 78 | + ) |
| 79 | + |
| 80 | + ## print information about the plot |
| 81 | + message("SuperPlot information") |
| 82 | + message("=====================") |
| 83 | + message("Number of conditions: ", ncond) |
| 84 | + message("Number of replicates: ", nrepl) |
| 85 | + message("Number of data points: ", nrow(df)) |
| 86 | + message("Number of summary points: ", nrow(summary_df)) |
| 87 | + message("=====================") |
| 88 | + message("Colour palette: ", pal) |
| 89 | + message("Data distribution: ", datadist) |
| 90 | + message("Summary statistic: ", rep_summary) |
| 91 | + if( bars != "") { |
| 92 | + message("Bars: ", bars) |
| 93 | + } else { |
| 94 | + message("No bars") |
| 95 | + } |
| 96 | + message("X-axis label: ", xlab) |
| 97 | + message("Y-axis label: ", ylab) |
| 98 | + if (linking == TRUE) { |
| 99 | + message("Linking: ", linking) |
| 100 | + } |
| 101 | + if (shapes == TRUE) { |
| 102 | + message("Shapes: ", shapes) |
| 103 | + } |
| 104 | + message("Point sizes: ", size[1], " (individual), ", |
| 105 | + size[2], " (summary)") |
| 106 | + message("Alpha for points: ", alpha[1], " (individual), ", |
| 107 | + alpha[2], " (summary)") |
| 108 | + message("Font size: ", fsize) |
| 109 | + if (stats== TRUE) { |
| 110 | + message("Statistics: ", stats_test) |
| 111 | + } else { |
| 112 | + message("No statistics") |
| 113 | + } |
| 114 | + message("=====================") |
| 115 | + message("Colours for replicates: ", paste(sp_colours, collapse = ", ")) |
| 116 | + message("Shapes for replicates: ", paste(sp_shapes, collapse = ", ")) |
| 117 | + |
| 118 | + if (nrow(summary_df) != ncond * nrepl) { |
| 119 | + s_summary_df <- summary_df %>% |
| 120 | + group_by(!!sym(cond)) %>% |
| 121 | + summarise( |
| 122 | + n_replicates = n() |
| 123 | + ) |
| 124 | + message("Unequal number of replicates per condition:") |
| 125 | + |
| 126 | + print(s_summary_df) |
| 127 | + } |
| 128 | + |
| 129 | + message("=====================") |
| 130 | + message("Summary statistics:") |
| 131 | + print(summary_df) |
| 132 | +} |
0 commit comments