labels for custom diverging color gradient in ggplot

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.

在这里输入图像描述

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:

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

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

在这里输入图像描述

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.

链接地址: http://www.djcxy.com/p/30906.html

上一篇: 在ggplot2中创建散点图的矩阵(对()等价)

下一篇: ggplot中自定义渐变颜色渐变的标签