Skip to content

Commit 5eba43f

Browse files
committed
Fix: colour usage for flatplot and pieplot
1 parent 7c61bd5 commit 5eba43f

11 files changed

Lines changed: 207 additions & 59 deletions

File tree

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export(add_sp_bars)
44
export(flatplot)
55
export(get_fp_colour)
6+
export(get_fp_colours)
67
export(get_sp_colours)
78
export(get_sp_shapes)
89
export(get_sp_stats)
@@ -14,6 +15,7 @@ import(cowplot)
1415
import(dplyr)
1516
import(ggforce)
1617
import(ggplot2)
18+
importFrom(grDevices,colors)
1719
importFrom(rlang,":=")
1820
importFrom(stats,TukeyHSD)
1921
importFrom(stats,kruskal.test)

R/flatplot.R

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ flatplot <- function(df,
7171
df[[cond]] <- as.character(df[[cond]])
7272
}
7373

74-
fp_colour <- get_fp_colour(colour)
74+
fp_colour <- get_fp_colours(colour)
7575

7676
# how many unique values in cond?
7777
ncond <- df %>%
7878
pull(!!sym(cond)) %>%
7979
unique() %>%
8080
length()
8181

82-
# make superplot ----
82+
# make flatplot ----
8383
# we may have an existing ggplot object to add to
8484
if (is.null(gg)) {
8585
p <- ggplot()
@@ -89,23 +89,49 @@ flatplot <- function(df,
8989

9090
# data points get plotted here
9191
if (datadist == "sina") {
92-
p <- p +
93-
geom_sina(
94-
data = df,
95-
aes(x = !!sym(cond), y = !!sym(meas)),
96-
colour = fp_colour,
97-
alpha = alpha, shape = 16, jitter_y = FALSE,
98-
size = size, maxwidth = 0.8
99-
)
92+
if (length(fp_colour) == 1) {
93+
# Single color - use as fixed aesthetic
94+
p <- p +
95+
geom_sina(
96+
data = df,
97+
aes(x = !!sym(cond), y = !!sym(meas)),
98+
colour = fp_colour,
99+
alpha = alpha, shape = 16, jitter_y = FALSE,
100+
size = size, maxwidth = 0.8
101+
)
102+
} else {
103+
# Multiple colors - map to condition levels
104+
p <- p +
105+
geom_sina(
106+
data = df,
107+
aes(x = !!sym(cond), y = !!sym(meas), colour = !!sym(cond)),
108+
alpha = alpha, shape = 16, jitter_y = FALSE,
109+
size = size, maxwidth = 0.8
110+
) +
111+
scale_colour_manual(values = fp_colour)
112+
}
100113
} else if (datadist == "jitter") {
101-
p <- p +
102-
geom_jitter(
103-
data = df,
104-
aes(x = !!sym(cond), y = !!sym(meas)),
105-
colour = fp_colour,
106-
alpha = alpha, shape = 16,
107-
size = size
108-
)
114+
if (length(fp_colour) == 1) {
115+
# Single color - use as fixed aesthetic
116+
p <- p +
117+
geom_jitter(
118+
data = df,
119+
aes(x = !!sym(cond), y = !!sym(meas)),
120+
colour = fp_colour,
121+
alpha = alpha, shape = 16,
122+
size = size
123+
)
124+
} else {
125+
# Multiple colors - map to condition levels
126+
p <- p +
127+
geom_jitter(
128+
data = df,
129+
aes(x = !!sym(cond), y = !!sym(meas), colour = !!sym(cond)),
130+
alpha = alpha, shape = 16,
131+
size = size
132+
) +
133+
scale_colour_manual(values = fp_colour)
134+
}
109135
} else {
110136
warning("datadist must be one of 'sina' or 'jitter'")
111137
}

R/get_fp_colour.R

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
#' FlatPlot Colour Selection
1+
#' FlatPlot Colour Selection (Multiple Colors)
2+
#'
3+
#' @param keys character vector of color names/codes, can be "rl_green", "rl_red", "rl_blue",
4+
#' "rl_yellow", "rl_purple", "rl_orange", "rl_magenta", hex colours, or valid R color names
5+
#'
6+
#' @return character vector of hex colours
7+
#' @export
8+
#'
9+
#' @examples
10+
#' get_fp_colours(c("rl_green", "#FF0000", "blue"))
11+
#' get_fp_colours("rl_green")
12+
get_fp_colours <- function(keys) {
13+
# Apply get_fp_colour to each element in the vector
14+
sapply(keys, get_fp_colour, USE.NAMES = FALSE)
15+
}
16+
17+
#' FlatPlot Colour Selection (Single Color)
218
#'
319
#' @param key character name, can be "rl_green", "rl_red", "rl_blue",
4-
#' "rl_purple", "rl_orange", "rl_magenta", or a hex colour
20+
#' "rl_yellow", "rl_purple", "rl_orange", "rl_magenta", or a hex colour
521
#'
622
#' @return character of hex colour
23+
#' @importFrom grDevices colors
724
#' @export
825
#'
926
#' @examples
@@ -15,10 +32,16 @@ get_fp_colour <- function(key) {
1532
if (grepl("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", key)) {
1633
return(key)
1734
}
35+
valid_r_colours <- colors()
36+
# if the key is a valid R colour, return it
37+
if (key %in% valid_r_colours) {
38+
return(key)
39+
}
1840
colour <- switch(key,
1941
"rl_green" = "#00a651",
2042
"rl_red" = "#ed1c24",
2143
"rl_blue" = "#2276b9",
44+
"rl_yellow" = "#ffc620",
2245
"rl_purple" = "#64318e",
2346
"rl_orange" = "#f59331",
2447
"rl_magenta" = "#da70d6")

R/pieplot.R

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#' Make a pie chart using ggplot2
22
#'
33
#' @description
4-
#' The function \code{pie_maker} creates a pie chart using
5-
#' \code{ggplot2}. It takes two vectors of values and optionally a
4+
#' The function \code{pie_maker} creates a pie chart using \code{ggplot2}.
5+
#' It takes two vectors of values and optionally a
66
#' second vector of values to create a pie chart with two layers.
77
#' @param x1 A numeric vector containing the values for the first
88
#' layer of the pie chart.
@@ -13,12 +13,11 @@
1313
#' @param label An optional character string to be used as the title
1414
#' @param ... Currently not used.
1515
#' @examples
16-
#' pieplot(x1 = c(143, 459),
16+
#' pieplot(x1 = c(123, 456),
1717
#' cols = c("#44aa99", "#117733"))
1818
#'
19-
#' pieplot(x1 = c(61 - 9, 9, 593, 3595 - 593),
20-
#' cols = c("#bbbbbb", "#44aa99", "#117733", "#dddddd"),
21-
#' x2 = c(61, 3595))
19+
#' pieplot(x1 = c(50 - 20, 20, 80, 180 - 80),
20+
#' cols = c("#bbbbbb", "#44aa99", "#117733", "#dddddd"), x2 = c(100,130))
2221
#'
2322
#' @return ggplot object
2423
#' @import ggplot2
@@ -28,21 +27,51 @@
2827

2928
pieplot <- function(x1, cols, x2 = NULL, label = NULL, ...) {
3029
pcolours <- ymin <- ymax <- NULL
30+
# validate args
31+
if (!is.numeric(x1)) {
32+
stop("x1 must be a numeric vector")
33+
}
34+
if (!is.null(x2) && !is.numeric(x2)) {
35+
stop("x2 must be a numeric vector or NULL")
36+
}
37+
# check that x1 and optionally x2 are non-negative
38+
if (any(x1 < 0)) {
39+
stop("x1 must be a non-negative numeric vector")
40+
}
41+
if (!is.null(x2) && any(x2 < 0)) {
42+
stop("x2 must be a non-negative numeric vector or NULL")
43+
}
44+
if (length(x1) == 0) {
45+
stop("x1 must be a non-empty numeric vector")
46+
}
47+
if (!is.null(x2) && length(x2) == 0) {
48+
stop("x2 must be a non-empty numeric vector or NULL")
49+
}
50+
# colour check
51+
check_colour(cols)
52+
if (!is.character(cols) || length(cols) != length(x1)) {
53+
stop("cols must be a character vector of the same length as x1")
54+
}
55+
cols <- get_fp_colours(cols)
56+
3157
df <- data.frame(
3258
pvalues = x1,
3359
pcolours = as.factor(seq_along(cols))
3460
)
35-
cs <- cumsum(x1)
36-
df$ymin <- c(0, cs[seq_along(cs) - 1])
37-
df$ymax <- cs
61+
cs1 <- cumsum(x1)
62+
df$ymin <- c(0, cs1[seq_along(cs1) - 1])
63+
df$ymax <- cs1
3864
if (is.null(x2)) {
65+
# if x2 is NULL, we only have one layer
66+
# so df2 makes the outline over the pie chart
3967
df2 <- df
4068
} else {
41-
cs <- cumsum(x2)
69+
x2 <- x2 / sum(x2) * sum(x1) # scale x2 to match x1
70+
cs2 <- cumsum(x2)
4271
df2 <- data.frame(
4372
pvalues = x2,
44-
ymin = c(0, cs[seq_along(cs) - 1]),
45-
ymax = cs
73+
ymin = c(0, cs2[seq_along(cs2) - 1]),
74+
ymax = cs2
4675
)
4776
}
4877

R/validate.R

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,34 @@ check_pal <- function(arg) {
6969
#'
7070
#' @param arg argument passed as colour
7171
#' @returns none
72+
#' @importFrom grDevices colors
7273
#' @keywords internal
7374
check_colour <- function(arg) {
74-
# colour should be character, one of the following:
75-
# "rl_green", "rl_red", "rl_blue", "rl_purple", "rl_orange", "rl_magenta", or
76-
# a hex colour
77-
# first test if it is a character vector of length 1
78-
if (length(arg) > 1) {
79-
stop("'colour' must be a hex colour or one of rl_green, rl_red, rl_blue,
80-
rl_purple, rl_orange, or rl_magenta", call. = FALSE)
81-
} else if (!arg %in% c("rl_green", "rl_red", "rl_blue", "rl_purple",
82-
"rl_orange", "rl_magenta")) {
83-
if (!grepl("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", arg)) {
84-
stop("'colour' must be a hex colour or one of rl_green, rl_red, rl_blue,
85-
rl_purple, rl_orange, or rl_magenta", call. = FALSE)
75+
# colour should be character vector, each element one of the following:
76+
# "rl_green", "rl_red", "rl_blue", "rl_yellow", "rl_purple", "rl_orange",
77+
# "rl_magenta", or a hex colour
78+
79+
# Check if arg is a character vector
80+
if (!is.character(arg)) {
81+
stop("'colour' must be a character vector", call. = FALSE)
82+
}
83+
84+
# Define valid named colours
85+
valid_colours <- c(colors(), "rl_green", "rl_red", "rl_blue", "rl_yellow",
86+
"rl_purple", "rl_orange", "rl_magenta")
87+
88+
# Check each element in the vector
89+
for (i in seq_along(arg)) {
90+
colour <- arg[i]
91+
92+
# Check if it's a valid named colour
93+
if (!colour %in% valid_colours) {
94+
# If not a named colour, check if it's a valid hex colour
95+
if (!grepl("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", colour)) {
96+
stop("'colour[", i, "]' must be a hex colour, a named colour or one of
97+
rl_green, rl_red, rl_blue, rl_yellow, rl_purple, rl_orange,
98+
or rl_magenta", call. = FALSE)
99+
}
86100
}
87101
}
88102
}

_pkgdown.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
url: https://quantixed.github.io/SuperPlotR/
2+
23
template:
34
bootstrap: 5
45
bootswatch: flatly
6+
7+
articles:
8+
- title: How to...
9+
navbar: ~
10+
contents:
11+
- SuperPlotR
12+
- advanced
13+
- limitations
14+
- flatplot
15+
- pieplot

man/get_fp_colour.Rd

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

man/get_fp_colours.Rd

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

man/pieplot.Rd

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

vignettes/flatplot.Rmd

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,27 @@ flatplot(lord_jcb, "Speed", "Treatment", ylab = "Speed (um/min)",
3737
colour = "rl_green")
3838
```
3939

40-
The control of colour is by a single colour, which can be a hex code or one of
41-
our lab's publication colour palette.
40+
The control of colour is usually by a single colour, which can be a hex code or
41+
one of our lab's publication colour palette.
4242

4343
```{r flatplot3}
4444
flatplot(lord_jcb, "Speed", "Treatment", ylab = "Speed (um/min)",
4545
colour = "rl_red", stats = TRUE)
4646
```
4747

48+
It is also possible to specify a vector of colours, which will be used for each
49+
condition. Any extra colours are ignored, and valid inputs are R colors, hex
50+
codes or our lab's colour palette.
51+
52+
```{r flatplot4}
53+
flatplot(lord_jcb, "Speed", "Treatment", ylab = "Speed (um/min)",
54+
colour = c("rl_red", "rl_blue", "rl_green"))
55+
```
56+
4857
We can request statistical testing as for SuperPlots, but the p-values will be
4958
calculated for the whole dataset, not for each replicate.
5059

51-
```{r flatplot4}
60+
```{r flatplot5}
5261
flatplot(lord_jcb, "Speed", "Treatment", ylab = "Speed (um/min)",
5362
colour = "rl_red", size = 4, alpha = 0.25,
5463
bars = "mean_ci")

0 commit comments

Comments
 (0)