Skip to content

Commit 9825448

Browse files
committed
v.0.0.7 get information from SuperPlot
1 parent 1b3a9ba commit 9825448

15 files changed

Lines changed: 682 additions & 18 deletions

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: SuperPlotR
22
Title: Making SuperPlots in R
3-
Version: 0.0.6
3+
Version: 0.0.7
44
Authors@R:
55
person(given = "Stephen J",
66
family = "Royle",
@@ -25,7 +25,8 @@ Imports:
2525
ggplot2,
2626
dplyr,
2727
ggforce,
28-
cowplot
28+
cowplot,
29+
rlang
2930
URL: https://github.com/quantixed/SuperPlotR, https://quantixed.github.io/SuperPlotR/
3031
BugReports: https://github.com/quantixed/SuperPlotR/issues
3132
Suggests:

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ export(get_fp_colour)
66
export(get_sp_colours)
77
export(get_sp_shapes)
88
export(get_sp_stats)
9+
export(get_sp_summary)
10+
export(representative)
911
export(superplot)
1012
import(cowplot)
1113
import(dplyr)
1214
import(ggforce)
1315
import(ggplot2)
16+
importFrom(rlang,":=")
1417
importFrom(stats,TukeyHSD)
1518
importFrom(stats,kruskal.test)
1619
importFrom(stats,median)

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# SuperPlotR (development version)
22

3+
## SuperPlotR 0.0.7
4+
5+
* Added ability to find representative datapoints with `representative()`
6+
* Added ability to retrieve information about superplot using the option
7+
`info = TRUE`. User is prompted to do this if there are unequal numbers of
8+
replicates in each condition
9+
* Added `get_sp_summary()` to retrieve the data frame that is overlaid
10+
on the superplot
11+
312
## SuperPlotR 0.0.6
413

514
* Fixed factoring bug.

R/get_sp_info.R

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

R/get_sp_summary.R

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#' Get summary statistics for SuperPlot data
2+
#'
3+
#' This function calculates summary statistics (mean and median) for each
4+
#' combination of condition and replicate in a SuperPlot dataset.
5+
#'
6+
#' It is useful to get teh summary data and computing further stats or making
7+
#' plots. The function only needs 4 parameters, anything else is ignored. This
8+
#' means you can simply exchange get_sp_summary() for your superplot() call and
9+
#' get the correct data frame to work with.
10+
#'
11+
#' @param df A data frame containing the experimental data
12+
#' @param meas Character string specifying the name of the column containing
13+
#' the measurements/values to summarize (e.g., "intensity", "speed")
14+
#' @param cond Character string specifying the name of the column containing
15+
#' the experimental conditions (e.g., "Treatment", "Genotype")
16+
#' @param repl Character string specifying the name of the column containing
17+
#' the replicate identifiers (e.g., "Replicate", "Experiment")
18+
#' @param ... Additional arguments (ignored)
19+
#'
20+
#' @return A data frame with columns for condition, replicate, rep_mean
21+
#' (mean of measurements), and rep_median (median of measurements)
22+
#'
23+
#' @import dplyr
24+
#' @importFrom stats median
25+
#'
26+
#' @export
27+
#'
28+
#' @examples
29+
#' # Using the built-in dataset
30+
#' get_sp_summary(lord_jcb, "Speed", "Treatment", "Replicate")
31+
#'
32+
get_sp_summary <- function(df, meas, cond, repl, ...) {
33+
# Validate inputs
34+
if (!is.data.frame(df)) {
35+
stop("df must be a data frame")
36+
}
37+
38+
if (!meas %in% names(df)) {
39+
stop("meas column '", meas, "' not found in data frame")
40+
}
41+
42+
if (!cond %in% names(df)) {
43+
stop("cond column '", cond, "' not found in data frame")
44+
}
45+
46+
if (!repl %in% names(df)) {
47+
stop("repl column '", repl, "' not found in data frame")
48+
}
49+
50+
# Calculate summary statistics grouped by condition and replicate
51+
sdf <- df %>%
52+
group_by(!!sym(cond), !!sym(repl)) %>%
53+
summarise(
54+
rep_mean = mean(!!sym(meas), na.rm = TRUE),
55+
rep_median = median(!!sym(meas), na.rm = TRUE),
56+
.groups = "drop"
57+
)
58+
59+
return(sdf)
60+
}

0 commit comments

Comments
 (0)