Home > Blockchain >  Formatting ggplot2 legend text in rows with "empty" slots
Formatting ggplot2 legend text in rows with "empty" slots

Time:01-24

I am trying to format the legend in my the plot below. This is some dummy data and my code:

df <- data.frame(supplement = c("Control", 
                            "0.5 mM NA", "5 mM NA", "10 mM NA",
                            "0.5 mM NI", "5 mM NAI", "10 mM NI"),
             axis1 = c(1, 2, 3, 4, 5, 6, 7),
             axis2 = c(7, 6, 5, 4, 3, 2, 1))

df %>%
  mutate(supplement = factor(supplement,
                             levels = c("Control", 
                                        "0.5 mM NA", "5 mM NA", "10 mM NA",
                                        "0.5 mM NI", "5 mM NAI", "10 mM NI"))) %>%
  ggplot(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", 
                                 "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",
                                  "#824ED4", "#31B3F7", "#FFA600",
                                  "#ABF13D", "#DC267F", "#16F5E3")) 
  guides(color = guide_legend(order = 1, nrow = 2, byrow = TRUE))  
  theme(legend.title = element_blank(),
        legend.text = element_text(size = unit(14, "pt")),
        legend.position = "bottom",
        legend.box = "vertical",
        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'))

What I get is this.

ggplot I get

But I would really like them to be like this. However I dont know if this is even possible in ggplot as I have not found anything that would do this.

ggplot I want

CodePudding user response:

I don't know of any (non-complicated) method to pull out one particular legend key on a separate line compared to another, but in this case since you have 7 items in the legend, you can separate out the "No Treatment" key by constraining the guide to 3 columns and reordering in scale_color_manual() to place the control last.

In that case the part of your plot code is changed to this:

# ... original plot code...
scale_colour_manual(name = "",
                      breaks = c( 
                                 "0.5 mM NA", "5 mM NA", "10 mM NA",
                                 "0.5 mM NI", "5 mM NAI", "10 mM NI", "Control"),
                      labels = c(
                                 "0.5 mM Nitrate", "5 mM Nitrate", "10 mM Nitrate",
                                 "0.5 mM Nitrite", "5 mM Nitrite", "10 mM Nitrite", "No Treatment"),
                      values =  c(
                                  "#824ED4", "#31B3F7", "#FFA600",
                                  "#ABF13D", "#DC267F", "#16F5E3", "#003F5C")) 
  guides(color = guide_legend(order = 1, ncol = 3, byrow = TRUE))  

# ... remaining plot code

enter image description here

The alternative is to use your original code and just have the legend positioned at the right or left side of the plot. To do that, remove the guides(...) line and legend.position= theme element, since the default is to place on the right.

enter image description here

CodePudding user response:

If it's fine for your to get rid of the legend key background then one option would be

  1. to set ncols=3 in guide_legend
  2. to add two additional categories to your factor to add two additional slots or keys to the legend
  3. set the labels for these categories to an empty string and the color equal to the plot background color
  4. additionally we have to set drop=FALSE to scale_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'))

  •  Tags:  
  • Related