|
1 | 1 | #' Make a SuperPlot |
2 | 2 | #' |
3 | 3 | #' @description |
4 | | -#' The function \code{superplot} creates a SuperPlot using \code{ggplot2}. |
| 4 | +#' The function \code{superplot()} creates a SuperPlot using \code{ggplot2}. |
5 | 5 | #' |
6 | 6 | #' @param df data frame with at least three columns: meas, cond, repl |
7 | 7 | #' @param meas character name of column with measurement (e.g. intensity) |
|
33 | 33 | #' ("para_unpaired", "para_paired", "nonpara_unpaired", or "nonpara_paired") |
34 | 34 | #' @param info logical for whether to print information about the plot (default |
35 | 35 | #' is FALSE) |
| 36 | +#' @param options optional named list for advanced layer customization. See |
| 37 | +#' `superplot_spec()` and `sp_modify()` for available options. |
36 | 38 | #' |
37 | 39 | #' @return ggplot object |
38 | 40 | #' @import ggplot2 |
@@ -64,179 +66,31 @@ superplot <- function(df, |
64 | 66 | gg = NULL, |
65 | 67 | stats = FALSE, |
66 | 68 | stats_test = "para_unpaired", |
67 | | - info = FALSE) { |
68 | | - ncond <- nrepl <- NULL |
69 | | - rep_mean <- rep_median <- NULL |
70 | | - |
71 | | - # validate args |
72 | | - validate_args(pal = pal, xlab = xlab, ylab = ylab, datadist = datadist, |
73 | | - size = size, alpha = alpha, bars = bars, linking = linking, |
74 | | - rep_summary = rep_summary, shapes = shapes, fsize = fsize, |
75 | | - gg = gg, stats = stats, stats_test = stats_test, info = info) |
76 | | - |
77 | | - # verify that the data frame to make sure that it is suitable for SuperPlot |
78 | | - if (verify_sp_columns(df, meas, cond, repl, facet) == FALSE) { |
79 | | - return(NULL) |
80 | | - } |
81 | | - |
82 | | - # if the cond column is not character, convert it |
83 | | - # but only if it is not already a factor |
84 | | - if (!is.factor(df[[cond]]) && !is.character(df[[cond]])) { |
85 | | - df[[cond]] <- as.character(df[[cond]]) |
86 | | - } |
87 | | - |
88 | | - # if the repl column is not character, convert it |
89 | | - # but only if it is not already a factor |
90 | | - if (!is.character(df[[repl]]) && !is.factor(df[[repl]])) { |
91 | | - df[[repl]] <- as.character(df[[repl]]) |
92 | | - } |
93 | | - |
94 | | - # if facet is not NULL, check that it is character and convert to factor if not already |
95 | | - if (!is.null(facet)) { |
96 | | - if (!is.character(df[[facet]]) && !is.factor(df[[facet]])) { |
97 | | - df[[facet]] <- as.character(df[[facet]]) |
98 | | - } |
99 | | - } |
100 | | - |
101 | | - # how many unique values in cond and repl? |
102 | | - ncond <- df %>% |
103 | | - pull(!!sym(cond)) %>% |
104 | | - unique() %>% |
105 | | - length() |
106 | | - nrepl <- df %>% |
107 | | - pull(!!sym(repl)) %>% |
108 | | - unique() %>% |
109 | | - length() |
110 | | - if (!is.null(facet)) { |
111 | | - nfacet <- df %>% |
112 | | - pull(!!sym(facet)) %>% |
113 | | - unique() %>% |
114 | | - length() |
115 | | - } |
116 | | - |
117 | | - # calculate summary statistics |
118 | | - summary_df <- get_sp_summary(df = df, |
119 | | - meas = meas, cond = cond, repl = repl, |
120 | | - facet = facet) |
121 | | - |
122 | | - # generate a warning if NROW of summary_df doesn't equal the product of |
123 | | - # unique values in cond and repl |
124 | | - if (!is.null(facet)) { |
125 | | - if (nrow(summary_df) != ncond * nrepl * nfacet & info == FALSE) { |
126 | | - warning("Summary statistics were not calculated for all combinations of |
127 | | - condition, replicate, and facet.\nCheck for missing data.\n |
128 | | - Call again with `info = TRUE` to see more details.") |
129 | | - } |
130 | | - } else { |
131 | | - if (nrow(summary_df) != ncond * nrepl & info == FALSE) { |
132 | | - warning("Summary statistics were not calculated for all combinations of |
133 | | - condition and replicate.\nCheck for missing data.\n |
134 | | - Call again with `info = TRUE` to see more details.") |
135 | | - } |
136 | | - } |
137 | | - # get colour values for the repl column |
138 | | - sp_colours <- get_sp_colours(nrepl, pal) |
139 | | - sp_shapes <- get_sp_shapes(nrepl, shapes) |
140 | | - |
141 | | - # if info is TRUE, print information about the plot |
142 | | - if (info == TRUE) { |
143 | | - get_sp_info(df = df, |
144 | | - meas = meas, cond = cond, repl = repl, facet = facet, |
145 | | - pal = pal, xlab = xlab, ylab = ylab, |
146 | | - datadist = datadist, size = size, alpha = alpha, |
147 | | - bars = bars, linking = linking, |
148 | | - rep_summary = rep_summary, shapes = shapes, |
149 | | - fsize = fsize, gg = gg, |
150 | | - stats = stats, stats_test = stats_test |
151 | | - ) |
152 | | - } |
153 | | - |
154 | | - # make superplot ---- |
155 | | - # we may have an existing ggplot object to add to |
156 | | - if (is.null(gg)) { |
157 | | - p <- ggplot() |
158 | | - } else { |
159 | | - p <- gg |
160 | | - } |
161 | | - |
162 | | - # data points get plotted here |
163 | | - if (datadist == "sina") { |
164 | | - p <- p + |
165 | | - geom_sina( |
166 | | - data = df, |
167 | | - aes(x = !!sym(cond), y = !!sym(meas), |
168 | | - colour = !!sym(repl), fill = !!sym(repl), shape = !!sym(repl)), |
169 | | - alpha = alpha[1], stroke = 0, jitter_y = FALSE, position = "auto", |
170 | | - size = size[1], maxwidth = 0.3 |
171 | | - ) |
172 | | - } else if (datadist == "jitter") { |
173 | | - p <- p + |
174 | | - geom_jitter( |
175 | | - data = df, |
176 | | - aes(x = !!sym(cond), y = !!sym(meas), |
177 | | - colour = !!sym(repl), fill = !!sym(repl), shape = !!sym(repl)), |
178 | | - alpha = alpha[1], stroke = 0, |
179 | | - size = size[1] |
180 | | - ) |
181 | | - } else if (datadist == "violin") { |
182 | | - p <- p + |
183 | | - geom_violin( |
184 | | - data = df, |
185 | | - aes(x = !!sym(cond), y = !!sym(meas), group = !!sym(cond)), |
186 | | - fill = "grey", width = 0.5, alpha = alpha[1] |
187 | | - ) |
188 | | - } else { |
189 | | - warning("datadist must be one of 'sina', 'jitter', or 'violin'") |
190 | | - } |
191 | | - if (linking == TRUE) { |
192 | | - p <- p + geom_path( |
193 | | - data = summary_df, |
194 | | - aes(x = !!sym(cond), y = !!sym(rep_summary), group = !!sym(repl)), |
195 | | - linetype = "dashed", alpha = 0.5 |
196 | | - ) |
197 | | - } |
198 | | - # add mean and error bars here if requested |
199 | | - if (bars != "") { |
200 | | - p <- add_sp_bars(p, bars, summary_df, cond, rep_summary) |
201 | | - } |
202 | | - # add summary points here |
203 | | - p <- p + geom_point( |
204 | | - data = summary_df, |
205 | | - aes(x = !!sym(cond), y = !!sym(rep_summary), |
206 | | - fill = !!sym(repl), shape = !!sym(repl)), |
207 | | - size = size[2], stroke = 0.5, alpha = alpha[2] |
| 69 | + info = FALSE, |
| 70 | + options = NULL) { |
| 71 | + spec <- superplot_spec( |
| 72 | + df = df, |
| 73 | + meas = meas, |
| 74 | + cond = cond, |
| 75 | + repl = repl, |
| 76 | + facet = facet, |
| 77 | + pal = pal, |
| 78 | + xlab = xlab, |
| 79 | + ylab = ylab, |
| 80 | + datadist = datadist, |
| 81 | + size = size, |
| 82 | + alpha = alpha, |
| 83 | + bars = bars, |
| 84 | + linking = linking, |
| 85 | + rep_summary = rep_summary, |
| 86 | + shapes = shapes, |
| 87 | + fsize = fsize, |
| 88 | + gg = gg, |
| 89 | + stats = stats, |
| 90 | + stats_test = stats_test, |
| 91 | + info = info, |
| 92 | + options = options |
208 | 93 | ) |
209 | | - # colours, shapes, and labels |
210 | | - p <- p + scale_color_manual(values = sp_colours) + |
211 | | - scale_shape_manual(values = sp_shapes) + |
212 | | - scale_fill_manual(values = sp_colours) + |
213 | | - labs(x = xlab, y = ylab) |
214 | | - # limits |
215 | | - if (min(df[[meas]], na.rm = TRUE) > 0) { |
216 | | - p <- p + lims(y = c(0, NA)) |
217 | | - } else { |
218 | | - # plot is scaled automatically |
219 | | - } |
220 | | - # facet if requested |
221 | | - if (!is.null(facet)) { |
222 | | - p <- p + facet_wrap(as.formula(paste("~", facet))) |
223 | | - } |
224 | | - # theme |
225 | | - p <- p + theme_cowplot(fsize) |
226 | | - # info is FALSE hide legend |
227 | | - if (info == TRUE) { |
228 | | - p <- p + theme(legend.position = "right") |
229 | | - } |
230 | | - p <- p + theme(legend.position = "none") |
231 | | - |
232 | | - # add stats if requested |
233 | | - if (stats == TRUE) { |
234 | | - if (!is.null(facet)) { |
235 | | - warning("Statistical tests are not currently implemented for faceted SuperPlots.") |
236 | | - } else { |
237 | | - get_sp_stats(as.data.frame(summary_df), rep_summary, cond, repl, ncond, nrepl, stats_test) |
238 | | - } |
239 | | - } |
240 | 94 |
|
241 | | - return(p) |
| 95 | + autoplot(spec) |
242 | 96 | } |
0 commit comments