Skip to content

Commit 3735d80

Browse files
committed
v0.2.0
1 parent 241f820 commit 3735d80

16 files changed

Lines changed: 787 additions & 205 deletions

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
S3method(autoplot,superplot_spec)
34
export(add_sp_bars)
45
export(flatplot)
56
export(get_fp_color)
@@ -14,7 +15,9 @@ export(get_sp_summary)
1415
export(pieplot)
1516
export(representative)
1617
export(setup_rproj)
18+
export(sp_modify)
1719
export(superplot)
20+
export(superplot_spec)
1821
import(cowplot)
1922
import(dplyr)
2023
import(ggforce)

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
# SuperPlotR 0.2.0
2+
3+
* SuperPlotting is now done using `superplot_spec()` behind the scenes, which
4+
allows for greater flexibility in the future.
5+
* Better handling of colour and shape aesthetics, including the ability to specify
6+
them in the `superplot()` function.
7+
18
# SuperPlotR 0.1.2
9+
210
* Ability to facet superplots
311
* Added text description of colours used in information output
412
* Documentation improvements

R/add_sp_bars.R

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,53 @@
55
#' @param df data frame of summary data (supplied by superplot())
66
#' @param cond character string of the condition column
77
#' @param rep_summary character string of the replicate summary column
8+
#' @param bar_params named list of additional parameters passed to the
9+
#' errorbar `stat_summary` layer
10+
#' @param crossbar_params named list of additional parameters passed to the
11+
#' crossbar `stat_summary` layer
812
#'
913
#' @return ggplot object
1014
#' @export
11-
add_sp_bars <- function(p, bars, df, cond, rep_summary) {
15+
add_sp_bars <- function(p, bars, df, cond, rep_summary,
16+
bar_params = list(),
17+
crossbar_params = list()) {
18+
base_bar <- list(
19+
data = df,
20+
mapping = aes(x = !!sym(cond), y = !!sym(rep_summary)),
21+
geom = "errorbar",
22+
linewidth = 0.2,
23+
width = 0
24+
)
25+
1226
if (bars == "mean_sd") {
13-
p <- p + stat_summary(
14-
data = df,
15-
aes(x = !!sym(cond), y = !!sym(rep_summary)),
16-
fun.data = trio_sd,
17-
geom = "errorbar", linewidth = 0.2, width = 0)
27+
p <- p + do.call(stat_summary, utils::modifyList(
28+
c(base_bar, list(fun.data = trio_sd)),
29+
bar_params
30+
))
1831
} else if (bars == "mean_sem") {
19-
p <- p + stat_summary(
20-
data = df,
21-
aes(x = !!sym(cond), y = !!sym(rep_summary)),
22-
fun.data = trio_sem,
23-
geom = "errorbar", linewidth = 0.2, width = 0)
32+
p <- p + do.call(stat_summary, utils::modifyList(
33+
c(base_bar, list(fun.data = trio_sem)),
34+
bar_params
35+
))
2436
} else if (bars == "mean_ci") {
25-
p <- p + stat_summary(
26-
data = df,
27-
aes(x = !!sym(cond), y = !!sym(rep_summary)),
28-
fun.data = trio_ci,
29-
geom = "errorbar", linewidth = 0.2, width = 0)
37+
p <- p + do.call(stat_summary, utils::modifyList(
38+
c(base_bar, list(fun.data = trio_ci)),
39+
bar_params
40+
))
3041
}
42+
3143
# whatever the user enters, we also want to show the crossbars
32-
p <- p + stat_summary(
33-
data = df,
34-
aes(x = !!sym(cond), y = !!sym(rep_summary)),
35-
fun.data = trio_mean,
36-
geom = "crossbar", linewidth = 0.4, width = 0.8)
44+
p <- p + do.call(stat_summary, utils::modifyList(
45+
list(
46+
data = df,
47+
mapping = aes(x = !!sym(cond), y = !!sym(rep_summary)),
48+
fun.data = trio_mean,
49+
geom = "crossbar",
50+
linewidth = 0.4,
51+
width = 0.8
52+
),
53+
crossbar_params
54+
))
3755

3856
return(p)
3957
}

R/superplot.R

Lines changed: 28 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#' Make a SuperPlot
22
#'
33
#' @description
4-
#' The function \code{superplot} creates a SuperPlot using \code{ggplot2}.
4+
#' The function \code{superplot()} creates a SuperPlot using \code{ggplot2}.
55
#'
66
#' @param df data frame with at least three columns: meas, cond, repl
77
#' @param meas character name of column with measurement (e.g. intensity)
@@ -33,6 +33,8 @@
3333
#' ("para_unpaired", "para_paired", "nonpara_unpaired", or "nonpara_paired")
3434
#' @param info logical for whether to print information about the plot (default
3535
#' is FALSE)
36+
#' @param options optional named list for advanced layer customization. See
37+
#' `superplot_spec()` and `sp_modify()` for available options.
3638
#'
3739
#' @return ggplot object
3840
#' @import ggplot2
@@ -64,179 +66,31 @@ superplot <- function(df,
6466
gg = NULL,
6567
stats = FALSE,
6668
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
20893
)
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-
}
24094

241-
return(p)
95+
autoplot(spec)
24296
}

0 commit comments

Comments
 (0)