Skip to content

Commit 4f3ca9e

Browse files
committed
v.0.0.5 addition of FlatPlots
1 parent 16710fb commit 4f3ca9e

18 files changed

Lines changed: 443 additions & 26 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
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.4
3+
Version: 0.0.5
44
Authors@R:
55
person(given = "Stephen J",
66
family = "Royle",

NAMESPACE

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

33
export(add_sp_bars)
4+
export(flatplot)
5+
export(get_fp_colour)
46
export(get_sp_colours)
57
export(get_sp_shapes)
68
export(get_sp_stats)

NEWS.md

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

3+
## SuperPlotR 0.0.5
4+
5+
* Added `plainplot` function for simple plots.
6+
37
## SuperPlotR 0.0.4
48

59
* Added simple statistical testing for the `superplot` function.

R/flatplot.R

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#' Make a FlatPlot
2+
#'
3+
#' @param df data frame with at least three columns: meas, cond, repl
4+
#' @param meas character name of column with measurement (e.g. intensity)
5+
#' @param cond character name of column with condition (e.g. Control, WT)
6+
#' @param colour string for colour palette to use, select ("rl_green", "rl_red",
7+
#' "rl_blue", "rl_purple", "rl_orange", "rl_magenta", or a hex colour, default
8+
#' is black)
9+
#' @param xlab string for x label (default is empty)
10+
#' @param ylab string for y label (default is "Measurement")
11+
#' @param datadist string for data distribution to use, select ("sina" default,
12+
#' or "jitter")
13+
#' @param size numeric size of data points (default is 2)
14+
#' @param alpha numeric vector of alpha range data and summary points (default
15+
#' is c(0.5, 0.7))
16+
#' @param bars string for type of error bars to add, select ("none",
17+
#' "mean_sd" (default), "mean_sem", or "mean_ci")
18+
#' @param fsize numeric font size for text (default is 12)
19+
#' @param gg ggplot object to add to (default is NULL)
20+
#' @param stats logical for whether to add statistical tests (default is FALSE)
21+
#' @param stats_test string for statistical test to use, select
22+
#' ("para_unpaired", "para_paired", "nonpara_unpaired", or "nonpara_paired")
23+
#'
24+
#' @return ggplot object
25+
#' @import ggplot2
26+
#' @import dplyr
27+
#' @import ggforce
28+
#' @import cowplot
29+
#' @importFrom stats sd median
30+
#'
31+
#' @export
32+
#'
33+
#' @examples
34+
#' flatplot(lord_jcb, "Speed", "Treatment", ylab = "Speed (um/min)")
35+
#'
36+
flatplot <- function(df,
37+
meas, cond,
38+
colour = "#000000",
39+
xlab = "", ylab = "Measurement",
40+
datadist = "sina",
41+
size = 2,
42+
alpha = 0.5,
43+
bars = "mean_sd",
44+
fsize = 12,
45+
gg = NULL,
46+
stats = FALSE,
47+
stats_test = "para_unpaired") {
48+
ncond <- nrepl <- NULL
49+
rep_mean <- rep_median <- NULL
50+
51+
# validate args
52+
validate_args(colour = colour, xlab = xlab, ylab = ylab, datadist = datadist,
53+
bars = bars, fsize = fsize,
54+
gg = gg, stats = stats, stats_test = stats_test)
55+
# size and alpha should be a single numeric value
56+
if (!is.numeric(size) || length(size) != 1) {
57+
stop("size must be a single numeric value")
58+
}
59+
if (!is.numeric(alpha) || length(alpha) != 1) {
60+
stop("alpha must be a single numeric value")
61+
}
62+
63+
# verify that the data frame to make sure that it is suitable for SuperPlot
64+
if (verify_fp_columns(df, meas, cond) == FALSE) {
65+
return(NULL)
66+
}
67+
68+
# if the cond column is not character, convert it
69+
if (!is.character(df[[cond]])) {
70+
df[[cond]] <- as.character(df[[cond]])
71+
}
72+
73+
fp_colour <- get_fp_colour(colour)
74+
75+
# how many unique values in cond?
76+
ncond <- df %>%
77+
pull(!!sym(cond)) %>%
78+
unique() %>%
79+
length()
80+
81+
# make superplot ----
82+
# we may have an existing ggplot object to add to
83+
if (is.null(gg)) {
84+
p <- ggplot()
85+
} else {
86+
p <- gg
87+
}
88+
89+
# data points get plotted here
90+
if (datadist == "sina") {
91+
p <- p +
92+
geom_sina(
93+
data = df,
94+
aes(x = !!sym(cond), y = !!sym(meas)),
95+
colour = fp_colour,
96+
alpha = alpha, shape = 16, jitter_y = FALSE,
97+
size = size, maxwidth = 0.8
98+
)
99+
} else if (datadist == "jitter") {
100+
p <- p +
101+
geom_jitter(
102+
data = df,
103+
aes(x = !!sym(cond), y = !!sym(meas)),
104+
colour = fp_colour,
105+
alpha = alpha, shape = 16,
106+
size = size
107+
)
108+
} else {
109+
warning("datadist must be one of 'sina' or 'jitter'")
110+
}
111+
# add mean and error bars here if requested
112+
if (bars != "") {
113+
p <- add_sp_bars(p, bars, df, cond, meas)
114+
}
115+
# colours, shapes, and labels
116+
p <- p + labs(x = xlab, y = ylab)
117+
# limits
118+
if (min(df[[meas]], na.rm = TRUE) > 0) {
119+
p <- p + lims(y = c(0, NA))
120+
} else {
121+
# plot is scaled automatically
122+
}
123+
# theme
124+
p <- p + theme_cowplot(fsize) +
125+
theme(legend.position = "none")
126+
# add stats if requested
127+
if (stats == TRUE) {
128+
nrepl <- df %>%
129+
group_by(!!sym(cond)) %>%
130+
summarise(n = n())
131+
nrepl <- min(nrepl$n, na.rm = TRUE)
132+
get_sp_stats(as.data.frame(df), meas, cond, repl = NULL,
133+
ncond, nrepl, stats_test)
134+
}
135+
136+
return(p)
137+
}

R/get_fp_colour.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' FlatPlot Colour Selection
2+
#'
3+
#' @param key character name, can be "rl_green", "rl_red", "rl_blue",
4+
#' "rl_purple", "rl_orange", "rl_magenta", or a hex colour
5+
#'
6+
#' @return character of hex colour
7+
#' @export
8+
#'
9+
#' @examples
10+
#' get_fp_colour("rl_green")
11+
get_fp_colour <- function(key) {
12+
# key has already been validated to be either a single hex value or one of
13+
# the allowed strings
14+
# so, if it is a hex code, return it
15+
if (grepl("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", key)) {
16+
return(key)
17+
}
18+
colour <- switch(key,
19+
"rl_green" = "#00a651",
20+
"rl_red" = "#ed1c24",
21+
"rl_blue" = "#2276b9",
22+
"rl_purple" = "#64318e",
23+
"rl_orange" = "#f59331",
24+
"rl_magenta" = "#da70d6")
25+
return(colour)
26+
}

R/get_sp_stats.R

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#' @returns nothing, prints results to console
1313
#' @importFrom stats t.test wilcox.test kruskal.test TukeyHSD
1414
#' @export
15-
get_sp_stats <- function(df, rep_summary, cond, repl, ncond, nrepl,
15+
get_sp_stats <- function(df, rep_summary, cond, repl, ncond, nrepl,
1616
stats_test) {
1717
# if ncond is 1, then we can't do any tests
1818
if (ncond == 1) {
@@ -66,15 +66,20 @@ get_sp_stats <- function(df, rep_summary, cond, repl, ncond, nrepl,
6666
cat("ANOVA not significant, no Tukey's HSD test performed\n")
6767
}
6868
} else {
69-
cat("Performing repeated measures ANOVA\n")
70-
aov <- aov(df[[rep_summary]] ~ df[[cond]] + Error(df[[repl]]))
71-
print(aov)
72-
print(summary(aov))
73-
# if Pr is < 0.05, then we do Tukey's HSD test
74-
if (summary(aov)[[2]][[1]]["Pr(>F)"][[1]][1] < 0.05) {
75-
cat("Pr < 0.05, perform multiple comparisons manually\n")
69+
if (!is.null(repl)) {
70+
cat("Performing repeated measures ANOVA\n")
71+
aov <- aov(df[[rep_summary]] ~ df[[cond]] + Error(df[[repl]]))
72+
print(aov)
73+
print(summary(aov))
74+
# if Pr is < 0.05, then we do Tukey's HSD test
75+
if (summary(aov)[[2]][[1]]["Pr(>F)"][[1]][1] < 0.05) {
76+
cat("Pr < 0.05, perform multiple comparisons manually\n")
77+
} else {
78+
cat("ANOVA not significant\n")
79+
}
7680
} else {
77-
cat("ANOVA not significant\n")
81+
cat("Selected para_paired, and there are more than 2 groups. Please
82+
consider performing a repeated measures ANOVA manually\n")
7883
}
7984
}
8085
}
@@ -92,7 +97,7 @@ get_sp_stats <- function(df, rep_summary, cond, repl, ncond, nrepl,
9297
cat("Kruskal-Wallis test not significant\n")
9398
}
9499
} else {
95-
cat("Selected nonpara-paired, and there are more than 2 groups. Please
100+
cat("Selected nonpara_paired, and there are more than 2 groups. Please
96101
consider performing a Friedman test manually\n")
97102
}
98103
}

R/superplot.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
#' @param datadist string for data distribution to use, select ("sina" default,
1212
#' "jitter", or "violin")
1313
#' @param size numeric vector of size range data and summary points (default is
14-
#' c(0.8, 1.5))
14+
#' c(2, 3))
1515
#' @param alpha numeric vector of alpha range data and summary points (default
1616
#' is c(0.5, 0.7))
1717
#' @param bars string for type of error bars to add, select ("none" default,
1818
#' "mean_sd", "mean_sem", or "mean_ci")
1919
#' @param linking logical for whether to link summary points between conditions
2020
#' (default is FALSE)
21-
#' @param fsize numeric font size for text (default is 9)
21+
#' @param fsize numeric font size for text (default is 12)
2222
#' @param shapes logical for whether to use different shapes for replicates
2323
#' @param rep_summary string for summary statistic to use for replicates, select
2424
#' ("rep_mean" default, or "rep_median")
@@ -66,7 +66,7 @@ superplot <- function(df,
6666
gg = gg, stats = stats, stats_test = stats_test)
6767

6868
# verify that the data frame to make sure that it is suitable for SuperPlot
69-
if (verify_columns(df, meas, cond, repl) == FALSE) {
69+
if (verify_sp_columns(df, meas, cond, repl) == FALSE) {
7070
return(NULL)
7171
}
7272

R/validate.R

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#' Validate SuperPlot Arguments
22
#'
33
#' @param pal argument passed to pal
4+
#' @param colour argument passed to colour
45
#' @param xlab argument passed to xlab
56
#' @param ylab argument passed to ylab
67
#' @param datadist argument passed to datadist
@@ -17,12 +18,13 @@
1718
#'
1819
#' @returns none
1920
#' @keywords internal
20-
validate_args <- function(pal = NULL, xlab = NULL, ylab = NULL, datadist = NULL,
21-
size = NULL, alpha = NULL, bars = NULL,
22-
linking = NULL, rep_summary = NULL, shapes = NULL,
23-
fsize = NULL, gg = NULL, stats = NULL,
21+
validate_args <- function(pal = NULL, colour = NULL, xlab = NULL, ylab = NULL,
22+
datadist = NULL, size = NULL, alpha = NULL,
23+
bars = NULL, linking = NULL, rep_summary = NULL,
24+
shapes = NULL, fsize = NULL, gg = NULL, stats = NULL,
2425
stats_test = NULL) {
2526
if (!is.null(pal)) check_pal(pal)
27+
if (!is.null(colour)) check_colour(colour)
2628
if (!is.null(xlab)) check_xlab(xlab)
2729
if (!is.null(ylab)) check_ylab(ylab)
2830
if (!is.null(datadist)) check_datadist(datadist)
@@ -62,6 +64,28 @@ check_pal <- function(arg) {
6264
}
6365
}
6466

67+
#' Check colour argument
68+
#'
69+
#' @param arg argument passed as colour
70+
#' @returns none
71+
#' @keywords internal
72+
check_colour <- function(arg) {
73+
# colour should be character, one of the following:
74+
# "rl_green", "rl_red", "rl_blue", "rl_purple", "rl_orange", "rl_magenta", or
75+
# a hex colour
76+
# first test if it is a character vector of length 1
77+
if (length(arg) > 1) {
78+
stop("'colour' must be a hex colour or one of rl_green, rl_red, rl_blue,
79+
rl_purple, rl_orange, or rl_magenta", call. = FALSE)
80+
} else if (!arg %in% c("rl_green", "rl_red", "rl_blue", "rl_purple",
81+
"rl_orange", "rl_magenta")) {
82+
if (!grepl("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", arg)) {
83+
stop("'colour' must be a hex colour or one of rl_green, rl_red, rl_blue,
84+
rl_purple, rl_orange, or rl_magenta", call. = FALSE)
85+
}
86+
}
87+
}
88+
6589
#' Check xlab argument
6690
#'
6791
#' @param arg argument passed as xlab

R/verify.R

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#'
99
#' @return logical to allow plot to go ahead
1010
#' @keywords internal
11-
verify_columns <- function(df, meas, cond, repl) {
11+
verify_sp_columns <- function(df, meas, cond, repl) {
1212
# check that meas, cond and repl are character
1313
if (!is.character(meas) | !is.character(cond) | !is.character(repl)) {
1414
message("meas, cond and repl must be character")
@@ -26,3 +26,30 @@ verify_columns <- function(df, meas, cond, repl) {
2626
}
2727
return(TRUE)
2828
}
29+
30+
#' Verify the data frame used for FlatPlot
31+
#'
32+
#' @param df data frame with at least three columns: meas, cond, repl
33+
#' @param meas character name of column with measurement (e.g. intensity)
34+
#' @param cond character name of column with condition (e.g. Control, WT)
35+
#'
36+
#' @return logical to allow plot to go ahead
37+
#' @keywords internal
38+
verify_fp_columns <- function(df, meas, cond) {
39+
# check that meas, cond and repl are character
40+
if (!is.character(meas) | !is.character(cond)) {
41+
message("meas and cond must be character")
42+
return(FALSE)
43+
}
44+
# verify the data frame - check if the required columns are present
45+
if (!cond %in% colnames(df) | !meas %in% colnames(df)) {
46+
message("The data frame does not contain the required columns")
47+
return(FALSE)
48+
}
49+
# check that column meas is numeric
50+
if (!is.numeric(df[[meas]])) {
51+
message("The column ", meas, " is not numeric")
52+
return(FALSE)
53+
}
54+
return(TRUE)
55+
}

man/check_colour.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)