yake84
yake84

Reputation: 3236

labels for custom diverging color gradient

I would like to plot some data where I use a custom transformation for a diverging color scale. As shown here, the labels come in funky or sometimes not at all. I would like for the labels to be rounded and to show the value the color represents and not the transformation.

enter image description here

I have been able to successfully apply it to a non-negative scale and I believe it has something do to with the negative numbers or the inverse call in trans_new which I don't quite understand from the documentation:

enter image description here

Here are some links that I explored:

R: custom ggplot2 color-transform gives error in labels

GGplot custom scale transformation with custom ticks

https://github.com/tidyverse/ggplot2/issues/980 looks helpful over my head

library(scales)
library(tidyverse)

log_both <- function(x){ifelse(x == 0, 0, log(abs(x)) * sign(x))}

log_both_trans <- 
  function(){
    trans_new(name = 'log_both', 
              transform = log_both,
              inverse = log_both) #not clear what `inverse` does
  }

df <-
  tibble(y = (-10:10),
         x = (y^4)*sign(y))

ggplot(df) +
  #no transformation
    geom_point(aes(factor(x), y = 1, fill = x), shape = 21, size = 10) +
    scale_fill_gradient2(low = "blue", mid = "white", high = "red") +
  #transformed
    geom_point(aes(factor(x), y = - 1, color = x), size  = 10) +
    scale_color_gradient2(low = "blue", mid = "white", high = "red", trans = "log_both") +
  ylim(-2, 2) +
  labs(colour = "transformed", fill = "default", x = "", y = "")

Upvotes: 3

Views: 2633

Answers (1)

Claus Wilke
Claus Wilke

Reputation: 17810

The argument inverse takes a function that is the mathematical inverse of your original transformation. If you log transform the inverse is exponentiation. The inverse is required to calculate the labels that correspond to the chosen breaks. (You log-transform to obtain your scaled data, and then you inverse-transform to obtain your labels. See also this SO post.)

library(scales)
library(tidyverse)

log_both <- function(x){ifelse(x == 0, 0, log(abs(x)) * sign(x))}
exp_both <- function(x){exp(abs(x)) * sign(x)} # this is the inverse of log_both

log_both_trans <- 
  function(){
    trans_new(name = 'log_both', 
              transform = log_both,
              inverse = exp_both)
  }

df <-
  tibble(y = (-10:10),
         x = (y^4)*sign(y))

ggplot(df) +
  #no transformation
  geom_point(aes(factor(x), y = 1, fill = x), shape = 21, size = 10) +
  scale_fill_gradient2(low = "blue", mid = "white", high = "red",
                       guide = guide_colorbar(order = 1)) +
  #transformed
  geom_point(aes(factor(x), y = - 1, color = x), size  = 10) +
  scale_color_gradient2(low = "blue", mid = "white", high = "red",
                        trans = "log_both",
                        breaks = c(-10000, -100, 0, 100, 10000), # desired breaks on transformed scale
                        guide = guide_colorbar(order = 2)) +
  ylim(-2, 2) +
  labs(colour = "transformed", fill = "default", x = "", y = "")

enter image description here

The lines guide = guide_colorbar(order = 1) and guide = guide_colorbar(order = 2) are there only to make sure the legends show in the right order. Otherwise they appear in random order.

Upvotes: 3

Related Questions