Formatting ggplot2 legend text in rows with "empty" slots
If it's fine for your to get rid of the legend key background then one option would be
- to set
ncols=3
inguide_legend
- to add two additional categories to your factor to add two additional slots or keys to the legend
- set the labels for these categories to an empty string and the color equal to the plot background color
- additionally we have to set
drop=FALSE
toscale_color_manual
so that these unused categories don't get dropped
library(ggplot2)
library(dplyr)
df <- df %>%
mutate(supplement = factor(supplement,
levels = c("Control", "foo", "bar",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI")))
ggplot(df, aes(x = axis1, y = axis2)) +
geom_point(aes(color = supplement), size = 4) +
labs(y = "Axis 2", x = "Axis 1") +
scale_colour_manual(name = "",
breaks = c("Control", "foo", "bar",
"0.5 mM NA", "5 mM NA", "10 mM NA",
"0.5 mM NI", "5 mM NAI", "10 mM NI"),
labels = c("No Treatment", "", "",
"0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
"0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite"),
values = c("#003F5C", "white", "white",
"#824ED4", "#31B3F7", "#FFA600",
"#ABF13D", "#DC267F", "#16F5E3"),
drop = FALSE) +
guides(color = guide_legend(order = 1, ncol = 3, byrow = TRUE)) +
theme(legend.title = element_blank(),
legend.text = element_text(size = unit(14, "pt")),
legend.position = "bottom",
legend.box = "vertical",
legend.key = element_rect(fill = NA),
axis.title = element_text(size = unit(16, "pt")),
axis.text = element_text(size = unit(14, "pt")),
legend.key.size = unit(25, "pt"),
legend.key.width = unit(0, 'mm'))