Reputation: 11
I am new to R and trying to find a simple way to change the labels of the legend for combined boxplots created with ggplot and patchwork.
I am comparing the proportions of 5 different types of cells in 2 groups (controls and asthmatic). I created boxplot for each cell type, and combined them with patchwork.
plot_mac <- ggplot(asthma_desc, aes(x=control_case, y=BAL_mac_LP, color=control_case)) +
geom_boxplot(width=0.5,lwd=0.5) +
geom_jitter(width=0.15) +
labs(y = "Macrophages %") +
stat_compare_means(label = "p.signif", label.x.npc = "center", label.y = 80, hide.ns = T)
then I do the same for 4 different types of cells.
patchwork <- plot_mac + plot_lym + plot_neu + plot_mast + plot_eos + plot_layout(guides = 'collect')
patchwork & theme_minimal() & scale_color_manual(values=c("black", "red")) &
theme(axis.title.x = element_blank(), axis.ticks.x=element_blank(), axis.text.x=element_blank(), text=element_text(size=7)) &
ylim(0,100)
I get the following plot
I would like to change the legend "control_case" to "Group", "1" to "control, "2" to "case". I could not make it work with labs(), scale_x_discrete() nor with theme().
Upvotes: 1
Views: 1062
Reputation: 79194
Another option is to add a new column to your dataframe named Group
.
Here is an example with mock mtcars
dataset:
library(tidyverse)
library(ggpubr)
library(patchwork)
mtcars1 <- mtcars %>%
mutate(Group = case_when( am==0 ~"control",
am==1~"case",
TRUE ~ NA_character_))
p1 <- ggplot(mtcars1, aes(x=Group, y=mpg, color=Group)) +
geom_boxplot(width=0.5,lwd=0.5) +
geom_jitter(width=0.15) +
labs(y = "Macrophages %") +
stat_compare_means(label = "p.signif", label.x.npc = "center", label.y = 80, hide.ns = T)
p2 <- ggplot(mtcars1, aes(x=Group, y=wt, color=Group)) +
geom_boxplot(width=0.5,lwd=0.5) +
geom_jitter(width=0.15) +
labs(y = "Macrophages %") +
stat_compare_means(label = "p.signif", label.x.npc = "center", label.y = 80, hide.ns = T)
p3 <- ggplot(mtcars1, aes(x=Group, y=hp, color=Group)) +
geom_boxplot(width=0.5,lwd=0.5) +
geom_jitter(width=0.15) +
labs(y = "Macrophages %") +
stat_compare_means(label = "p.signif", label.x.npc = "center", label.y = 80, hide.ns = T)
patchwork <- p1 + p2 + p3 + plot_layout(guides = 'collect')
patchwork & theme_minimal() & scale_color_manual(values=c("black", "red")) &
theme(axis.title.x = element_blank(), axis.ticks.x=element_blank(), axis.text.x=element_blank(), text=element_text(size=7)) &
ylim(0,100)
Upvotes: 0