Skip to content

create.boxplot: How to keep all factor levels in plot, even when n=0 ? #182

Answered by jarbet
jarbet asked this question in Q&A
Discussion options

You must be logged in to vote

Ahh nvm, I think I found a solution:

You need to make a dummy sample with the missing factor level (and set all other variables to NA), then that group will appear in the plot:

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

data(mtcars);
mtcars$am <- factor(
    x = mtcars$am,
    levels = 0:2,
    labels = c('automatic', 'manual', 'other')
    );
dummy.sample <- data.frame(matrix(NA, nrow = 1, ncol = ncol(mtcars)));
colnames(dummy.sample) <- colnames(mtcars);
dummy.sample$am <- 'other';
mtcars <- rbind(mtcars, dummy.sample);

create.boxplot(
    formula = mpg ~ am,
    data = mtcars
    )

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

Related to: #111

Replies: 1 comment

Comment options

jarbet
May 28, 2024
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by jarbet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant