Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create.violinplot: how to add extra points when using panels? #183

Open
jarbet opened this issue May 31, 2024 · 0 comments
Open

create.violinplot: how to add extra points when using panels? #183

jarbet opened this issue May 31, 2024 · 0 comments

Comments

@jarbet
Copy link
Contributor

jarbet commented May 31, 2024

I'm trying to add median points to a violin plot when having multiple panels: i.e. formula = outcome ~ first.group.var | second.group.var.

I can't figure out how to get the extra.points argument to work with panels, but I found a workaround using + layer(panel.points(..)):

suppressPackageStartupMessages(library(BoutrosLab.plotting.general));

set.seed(12345);
n <- 1090;
simple.data <- data.frame(
    outcome = c(
        rnorm(n = n/2, mean = 0),
        rnorm(n = n/2, mean = 3)
        ),
    first.group.var = as.factor(sample(LETTERS[1:2],n,TRUE))
    );
simple.data$second.group.var <- c(
    rep('group.1', n / 2),
    rep('group.2', n / 2)
    );

medians <- aggregate(
    x = outcome ~ first.group.var + second.group.var,
    data = simple.data,
    FUN = median
    );

Attempt 1: try passing vector to extra.points

create.violinplot(
    formula = outcome ~ first.group.var | second.group.var,
    resolution = 100,
    data = simple.data,
    extra.points = medians$outcome,
    extra.points.pch = 21,
    extra.points.col = c('blue'),
    extra.points.cex = 1.5,
    xlab.label = ''
    );

Attempt 2: try passing list to extra.points, one vector per panel

create.violinplot(
    formula = outcome ~ first.group.var | second.group.var,
    resolution = 100,
    data = simple.data,
    extra.points = list(
        medians$outcome[medians$second.group.var == 'group.1'],
        medians$outcome[medians$second.group.var == 'group.2']
        ),
    extra.points.pch = 21,
    extra.points.col = c('blue'),
    extra.points.cex = 1.5,
    xlab.label = ''
    );

Solution using + layer(panel.points(...))

violin <- create.violinplot(
    formula = outcome ~ first.group.var | second.group.var,
    resolution = 100,
    data = simple.data,
    );
extra.points.x <- list(1:2, 1:2);
extra.points.y <- list(
        medians$outcome[medians$second.group.var == 'group.1'],
        medians$outcome[medians$second.group.var == 'group.2']
        );
violin + layer(panel.points(
    x = extra.points.x[[panel.number()]],
    y = extra.points.y[[panel.number()]],
    pch = 21,
    col = 'blue',
    fill = 'blue',
    cex = 1.5
    ))

Created on 2024-05-31 with reprex v2.0.2

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

No branches or pull requests

1 participant