Search code examples
rggplot2axis-labels

Several colors for the same tick/label


My data :

dat <- data.frame(x = c(1,2,3,4,5,6), y = c(2,3,4,6,2,3))

Breaks and labels of my plot :

breaks <- c(3,5)
labels <- c(paste(3,"(0.3)"), paste(5,"(0.5)"))

And my plot :

library(ggplot2)
ggplot() + 
  geom_point(data = dat, aes(x = x, y = y)) + 
  scale_y_continuous(breaks = breaks, labels = labels)

I wish to colour the same labels differently. For instance, I wish to colour the "3" with a different colour than the one of "(0.3)".


Solution

  • Here's a way to stick 2 plots together with patchwork, which is a package similar to cowplot but with a little more flexibility. I split the labels into 2 vectors, one with the integers and one with the decimals in parentheses. Then make 2 plots, one for the outer labels with no other markings, and one for the main plot.

    After doing one round of trying to build this, I started adjusting the margins in each theme, realizing I needed to set the top and bottom margins the same, but making no margin on the right side of the left plot and the left side of the right plot, so there's very little space between them. There's definitely still ways to tweak this, but I'd start with some of the spacing.

    library(tidyverse)
    library(patchwork)
    
    lbl_int <- str_extract(labels, "^\\d+")
    lbl_frac <- str_extract(labels, "\\(.+\\)")
    

    The main plot is fairly straightforward, just removing elements from the left side in the theme.

    (main_plot <- ggplot(dat, aes(x = x, y = y)) + 
      geom_point() + 
      scale_y_continuous(breaks = breaks, labels = lbl_frac) +
      theme(axis.text.y.left = element_text(color = "gray"), 
            axis.title.y.left = element_blank(),
            plot.margin = margin(1, 1, 1, 0, "mm")))
    

    The plot for the outer labels has most theme elements removed, but has the y-axis title and labels.

    (int_plot <- ggplot(dat, aes(x = 0, y = y)) + 
      scale_y_continuous(breaks = breaks, labels = lbl_int) +
      theme(axis.text.y.left = element_text(color = "black"), 
            axis.title.y.left = element_text(color = "black"),
            axis.title.x = element_blank(),
            panel.grid = element_blank(),
            axis.ticks = element_blank(),
            axis.text.x = element_blank(),
            panel.background = element_blank(),
            plot.margin = margin(1, 0, 1, 1, "mm")))
    

    Then patchwork makes it easy to just add plots together—literally with +—and then set the widths. Again, here's something you can adjust as you need, but I made the left plot very very skinny compared to the right one.

    int_plot + main_plot +
      plot_layout(ncol = 2, widths = c(1e-3, 1))
    

    Created on 2018-12-21 by the reprex package (v0.2.1)