Mark
Mark

Reputation: 1769

How merge two different scale color gradient with ggplot

By using R, is it possible to place 2 ggplot together (i.e., on the same plot) but with different bar of color gradient? My code, e.g.,

library(ggplot2)
ggplot(df1, aes(duration, slopes, col = color)) +
 geom_point(size = 3) +
 scale_color_gradient(low = "black", high = "red")
ggplot(df2, aes(duration, slopes, col = color)) +
 geom_point(size = 3) +
 scale_color_gradient(low = "blue", high = "green")

produces the following two pictures

enter image description here

enter image description here

I wish instead to be able to integrate them together in one plot with a bar for red and black and another bar for blue and green.

Upvotes: 1

Views: 2363

Answers (1)

teunbrand
teunbrand

Reputation: 37933

Yes you could if you use the ggnewscale package:

a <- sample(nrow(iris), 75)

df1 <- iris[a,]
df2 <- iris[-a,]

library(ggnewscale)

ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
  geom_point(data = df1, aes(colour = Petal.Length)) +
  scale_colour_gradientn(colours = c("red", "black")) +
  # Important: define a colour/fill scale before calling a new_scale_* function
  new_scale_colour() +
  geom_point(data = df2, aes(colour = Petal.Width)) +
  scale_colour_gradientn(colours = c("blue", "white"))

enter image description here

Alternatives are the relayer package, or the scale_colour_multi/scale_listed from ggh4x (full disclaimer: I wrote ggh4x).

EDIT: Here are the alternatives:

library(ggh4x)

# ggh4x scale_colour_multi (for gradientn-like scales)
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
  geom_point(data = df1, aes(length = Petal.Length)) +
  geom_point(data = df2, aes(width = Petal.Width)) +
  scale_colour_multi(colours = list(c("red", "black"), c("blue", "white")),
                     aesthetics = c("length", "width"))

# ggh4x scale_listed (for any non-position scale (in theory))
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
  geom_point(data = df1, aes(length = Petal.Length)) +
  geom_point(data = df2, aes(width = Petal.Width)) +
  scale_listed(list(
    scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length"),
    scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width")
  ), replaces = c("colour", "colour"))


library(relayer)

# relayer
ggplot(mapping = aes(Sepal.Width, Sepal.Length)) +
  rename_geom_aes(geom_point(data = df1, aes(length = Petal.Length)), 
                  new_aes = c("colour" = "length")) +
  rename_geom_aes(geom_point(data = df2, aes(width = Petal.Width)),
                  new_aes = c("colour" = "width")) +
  scale_colour_gradientn(colours = c("red", "black"), aesthetics = "length", 
                         guide = guide_colourbar(available_aes = "length")) +
  scale_colour_gradientn(colours = c("blue", "white"), aesthetics = "width", 
                         guide = guide_colourbar(available_aes = "width"))

All the alternatives give warnings about unknown aesthetics, but this doesn't matter for the resulting plots. It is just a line of code in ggplot's layer() function that produces this warning and you can't go around this without either re-coding every geom wrapper or, as ggnewscale does, renaming the old aesthetic instead of providing a new aesthetic. The plots all look near-identical, so I figured I wouldn't have to post them again.

Upvotes: 9

Related Questions