Skip to content

Allow users to input their own colour schemes, fixes for problems with >9 groups.#189

Open
quantixed wants to merge 5 commits intoACCLAB:masterfrom
quantixed:master
Open

Allow users to input their own colour schemes, fixes for problems with >9 groups.#189
quantixed wants to merge 5 commits intoACCLAB:masterfrom
quantixed:master

Conversation

@quantixed
Copy link

This PR is an enhancement to {dabestr} and fixes a couple of issues with colouring dabest_plots.

I also merged the changes from marsiwiec which fixes the formatting of the print statements and adds an option to silence the greeting.

Issues (MWE)

Using the current version of {dabestr} the following code illustrates the issues:

library(dabestr)
set.seed(12345)
N <- 20

df <- tibble::tibble(
  ca = rnorm(N, mean = 3, sd = 0.4),
  cb = rnorm(N, mean = 3.5, sd = 0.75),
  cc = rnorm(N, mean = 3.25, sd = 0.4),
  cd = rnorm(N, mean = 3, sd = 0.4),
  ce = rnorm(N, mean = 3.5, sd = 0.75),
  cf = rnorm(N, mean = 3.25, sd = 0.4),
  cg = rnorm(N, mean = 3, sd = 0.4),
  ch = rnorm(N, mean = 3.5, sd = 0.75),
  ci = rnorm(N, mean = 3.25, sd = 0.4),
  cj = rnorm(N, mean = 3.5, sd = 0.75),
  ta = rnorm(N, mean = 3.5, sd = 0.5)
)

df <- df |>
  tidyr::gather(key = Group, value = Measurement)

sub9.mean_diff <- load(df, x = Group, y = Measurement,
                       idx = c(
                         "ca", "cb", "cc", "cd", "ce",
                         "cf", "cg", "ch", "ci"
                       )) |> mean_diff()

sub10.mean_diff <- load(df, x = Group, y = Measurement,
                        idx = c(
                          "ca", "cb", "cc", "cd", "ce",
                          "cf", "cg", "ch", "ci", "cj"
                        )) |> mean_diff()

sub11.mean_diff <- load(df, x = Group, y = Measurement,
                        idx = c(
                          "ca", "cb", "cc", "cd", "ce",
                          "cf", "cg", "ch", "ci", "cj", "ta"
                        )) |> mean_diff()

# jama has only 7 colours - requested plot fails with empty groups for 8 & 9
dabest_plot(sub9.mean_diff, custom_palette = "jama")

# output is ok using uscgb, which has enough colours. 9 groups works as intended
dabest_plot(sub9.mean_diff, custom_palette = "ucscgb")

# output is in incorrect with 10 groups
# - colours do not match between raw and delta plots
dabest_plot(sub10.mean_diff, custom_palette = "ucscgb")

# output is in incorrect with 11 groups
# - colours do not match between raw and delta plots
# - colour mismatch between swarm plot and swarm bars in raw plot
dabest_plot(sub11.mean_diff, custom_palette = "ucscgb")

With the proposed changes, the last four commands all work correctly.

The enhancement I am proposing is to add manual colouring to the graphics.

# ENHANCEMENT: Define a custom palette with enough colors
my_palette <- c("#e6194b", "#3cb44b", "#ffe119", "#4363d8", "#f58231",
                "#911eb4", "#46f0f0", "#f032e6", "#bcf60c", "#fabebe",
                "#008080", "#e6beff", "#9a6324", "#fffac8", "#800000",
                "#aaffc3", "#808000", "#ffd8b1", "#000075", "#808080",
                "#ffffff", "#000000")

dabest_plot(sub11.mean_diff,
            custom_palette = "manual",
            palette_values = my_palette)

Briefly, the issue with groups exceeding the maximum in the palette is solved by a check which uses default ggplot2 colours if the palette is exceeded.

The issue of more than 9 groups causing errors was fixed by factoring the x positions so that they are in numeric order (i.e. 10 follows 9 rather than 1). However this also required naming the palette so that it can be applied correctly to Cumming plots (as these have missing categories). This choice meant that apply_palette() had to be rewritten. The only alternative would've been to manually colour each bar and violin as is done for the text markers and this felt too hacky to be a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant