-
When making boxplots for a grouping variable, is there some way to keep all group levels in the plot, even if some of the levels have 0 patients? The reason I want to do this is because I am using a for loop to make many plots, and sometimes n=0 for some of the group levels. But I dont want to drop the groups, since I want all of the plots to align the same way in a final multipanelplot. ExampleNotice the group level suppressPackageStartupMessages(library(BoutrosLab.plotting.general));
data(mtcars);
mtcars$am <- factor(
x = mtcars$am,
levels = 0:2,
labels = c('automatic', 'manual', 'other')
);
create.boxplot(
formula = mpg ~ am,
data = mtcars
); Created on 2024-05-28 with reprex v2.0.2 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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 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 |
Beta Was this translation helpful? Give feedback.
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:Created on 2024-05-28 with reprex v2.0.2
Related to: #111