88# ' @param cond character name of column with condition (e.g. Control, WT)
99# ' @param repl character name of column with replicate (e.g. unique experiment
1010# ' identifiers)
11+ # ' @param facet character name of column to facet by (default is NULL)
1112# ' @param pal name of colour palette to use (default is "tol_bright")
1213# ' @param xlab string for x label (default is empty)
1314# ' @param ylab string for y label (default is "Measurement")
3839# ' @import dplyr
3940# ' @import ggforce
4041# ' @import cowplot
41- # ' @importFrom stats sd median
42+ # ' @importFrom stats sd median as.formula
4243# '
4344# ' @export
4445# '
4950# '
5051superplot <- function (df ,
5152 meas , cond , repl ,
53+ facet = NULL ,
5254 pal = " tol_bright" ,
5355 xlab = " " , ylab = " Measurement" ,
5456 datadist = " sina" ,
@@ -73,7 +75,7 @@ superplot <- function(df,
7375 gg = gg , stats = stats , stats_test = stats_test , info = info )
7476
7577 # verify that the data frame to make sure that it is suitable for SuperPlot
76- if (verify_sp_columns(df , meas , cond , repl ) == FALSE ) {
78+ if (verify_sp_columns(df , meas , cond , repl , facet ) == FALSE ) {
7779 return (NULL )
7880 }
7981
@@ -89,6 +91,13 @@ superplot <- function(df,
8991 df [[repl ]] <- as.character(df [[repl ]])
9092 }
9193
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+
92101 # how many unique values in cond and repl?
93102 ncond <- df %> %
94103 pull(!! sym(cond )) %> %
@@ -98,17 +107,32 @@ superplot <- function(df,
98107 pull(!! sym(repl )) %> %
99108 unique() %> %
100109 length()
110+ if (! is.null(facet )) {
111+ nfacet <- df %> %
112+ pull(!! sym(facet )) %> %
113+ unique() %> %
114+ length()
115+ }
101116
102117 # calculate summary statistics
103118 summary_df <- get_sp_summary(df = df ,
104- meas = meas , cond = cond , repl = repl )
119+ meas = meas , cond = cond , repl = repl ,
120+ facet = facet )
105121
106122 # generate a warning if NROW of summary_df doesn't equal the product of
107123 # unique values in cond and repl
108- if (nrow(summary_df ) != ncond * nrepl && info == FALSE ) {
109- warning(" Summary statistics were not calculated for all combinations of
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.\n Check 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
110133 condition and replicate.\n Check for missing data.\n
111134 Call again with `info = TRUE` to see more details." )
135+ }
112136 }
113137 # get colour values for the repl column
114138 sp_colours <- get_sp_colours(nrepl , pal )
@@ -117,7 +141,7 @@ superplot <- function(df,
117141 # if info is TRUE, print information about the plot
118142 if (info == TRUE ) {
119143 get_sp_info(df = df ,
120- meas = meas , cond = cond , repl = repl ,
144+ meas = meas , cond = cond , repl = repl , facet = facet ,
121145 pal = pal , xlab = xlab , ylab = ylab ,
122146 datadist = datadist , size = size , alpha = alpha ,
123147 bars = bars , linking = linking ,
@@ -193,6 +217,10 @@ superplot <- function(df,
193217 } else {
194218 # plot is scaled automatically
195219 }
220+ # facet if requested
221+ if (! is.null(facet )) {
222+ p <- p + facet_wrap(as.formula(paste(" ~" , facet )))
223+ }
196224 # theme
197225 p <- p + theme_cowplot(fsize )
198226 # info is FALSE hide legend
@@ -203,7 +231,11 @@ superplot <- function(df,
203231
204232 # add stats if requested
205233 if (stats == TRUE ) {
206- get_sp_stats(as.data.frame(summary_df ), rep_summary , cond , repl , ncond , nrepl , stats_test )
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+ }
207239 }
208240
209241 return (p )
0 commit comments