Several colors for the same tick/label

2020-03-26 09:02发布

问题:

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)".

回答1:

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)



回答2:

This is something to get you going (credit to this answer which I adapted).

We use annotate to plot your labels, on two different x-axis coords, this will function as our labels (so we need to shut off the actual labelling in the theme).

First we create two vectors of the exact labels that we want in different colors.

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

breaks <- c(3,5)
labels_new1 <- c(NA, NA, 3, NA, 5, NA) # NA in order to skip that annotation
labels_new2 <- c(NA, NA, "(0.3)", NA, "(0.5)", NA)

Important parts:

  • coord_cartesian(xlim = c(0, 6), expand = FALSE) + # this will cut our plot properly

  • plot.margin = unit(c(1, 1, 1, 5), "lines") # this will give us some space on the left

Note that in coord_cartesian defined like that we are actually cutting off the two annotations (notice that the two x values you see in the next part (-1, -0.5) are outside the xlim range).

Plot object:

g1 <- ggplot() + 
  geom_point(data = dat, aes(x = x, y = y)) + 
  annotate(geom = "text", y = seq_len(nrow(dat)), x = -1, label = labels_new1, size = 4) +
#first the number add color = "blue" for example
  annotate(geom = "text", y = seq_len(nrow(dat)), x = -0.5, label = labels_new2, size = 4, color = "red") +
#second the parenthesis (colored in red)
  coord_cartesian(xlim = c(0, 6), expand = FALSE) +
  scale_y_continuous(breaks = breaks) +
#now lets shut off the labels and give us some left space in the plot 
  theme(plot.margin = unit(c(1, 1, 1, 5), "lines"),
        axis.title.y = element_blank(),
        axis.text.y = element_blank())

Finally:

g2 <- ggplot_gtable(ggplot_build(g1)) # convert to grob
g2$layout$clip[g2$layout$name == "panel"] <- "off" # clipping of the axes
# this will show the two annotations that we left off before
grid::grid.draw(g2)

Remarks:

You can play around with x=-1 and x=-0.5 to move the two annotations, and with the last value in c(1, 1, 1, 5) to give you more space on the left side.

labels_new1 and labels_new2 are very important, they are doing all the heavy work of what and where you want to show something.