Skip to content

awkward behavior from scale_fill_viridis_c(trans = "log") #4185

Closed
@twest820

Description

@twest820

Three interrelated issues:

  1. Log transformation of zero values for a continuous fill produces a warning about infinite values in a discrete y axis. This warning could be more clearly worded since the fill is neither discrete nor a y axis. There also appears to be differentiation of NaN from -Inf, leading to a cryptic NaN warning from negative fill values.
  2. There doesn't appear to be any way to tell ggplot these fill values are expected in order to suppress the warnings. The closest I can see is something like scale_fill_viridis_c(na.value = "transparent", trans = "log"), which plots fine since apparently -Inf, NaN, and NA are treated interchangeably but is orthogonal to warning generation.
  3. The resulting labels on the fill colorbar include 8+ decimal places, which is a curiously verbose default, though format_format(digits = n) provides a simple mitigation.
library(ggplot2)
library(magrittr)
library(scales)
library(tidyverse)

data = crossing(x = seq(0, 1, length.out = 100), y = seq(0, 1, length.out = 100))
data %<>% mutate(value = x - y)
ggplot(data) + geom_raster(aes(x = x, y = y, fill = value)) + scale_fill_viridis_c(trans = "log")

Warning messages:
1: In self$trans$transform(x) : NaNs produced
2: Transformation introduced infinite values in discrete y-axis 

# change for more concise labeling
ggplot(data) + geom_raster(aes(x = x, y = y, fill = value)) + scale_fill_viridis_c(labels = format_format(digits = 2), trans = "log")

Observed on R 4.0.2 with tidyverse 1.3.0 (ggplot 3.3.2). I suspect this might also occur with other scale_fill functions.
ggplot scale_fill_viridis_c

Metadata

Metadata

Assignees

No one assigned

    Labels

    messagesrequests for improvements to error, warning, or feedback messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions