Skip to content

Function colour_ramp() should respect transparency by default #108

Closed

Description

@clauswilke

The function colour_ramp() has the option to ramp RGBA colors instead of RGB colors. However, by default, this option is switched off, and the function removes any alpha values that may be attached to the colors it is given.

This seems to be an unusual default setting. If somebody goes to the effort of adding alpha values to colors, I would assume they want those respected. Also, this creates inconsistency between discrete and continuous color scales. Discrete scales respect alpha value, continuous ones do not.

Reproducible example: First a case using a discrete color scale.

library(ggplot2)

ggplot(iris, aes(Sepal.Length, fill = Species)) + 
  geom_density() +
  scale_fill_manual(values = c("#00FF00FF", "#008000B0", "#0000FF20"))

screen shot 2017-12-19 at 6 59 33 pm

The transparency values of the colors are respected. Now the same plot using a continuous color scale:

ggplot(iris, aes(Sepal.Length, fill = as.numeric(Species), group = Species)) + 
  geom_density() +
  scale_fill_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20"))

screen shot 2017-12-19 at 6 59 45 pm

Transparency is gone.

However, if we switch the alpha option to TRUE in function colour_ramp(), things behave as expected:

# change the default setting of scales::colour_ramp:
assignInNamespace("colour_ramp", function(colors, na.color = NA, alpha = TRUE){
  if (length(colors) == 0) {
    stop("Must provide at least one color to create a color ramp")
  }
  colorMatrix <- grDevices::col2rgb(colors, alpha = alpha)
  structure(function(x) {
    scales:::doColorRamp(colorMatrix, x, alpha, ifelse(is.na(na.color), 
                                                       "", na.color))
  }, safe_palette_func = TRUE)
}, "scales")

ggplot(iris, aes(Sepal.Length, fill = as.numeric(Species), group = Species)) + 
  geom_density() +
  scale_fill_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20"))

screen shot 2017-12-19 at 7 00 09 pm

Activity

  1. added
    featurea feature request or enhancement
    wipwork in progress
    on Jun 4, 2018
  2. added a commit that references this issue on Jun 9, 2018
    59197eb
  3. lukasjonkers commented on Oct 19, 2018

    @lukasjonkers

    I am trying to use this to change the transparency of colour and fill of geom_polygon, but noticed that the solution above affects colour and fill differently. The difference is only small, but leads to very ugly graphs with tightly spaced polygons (e.g. maps)

    ggplot(iris, aes(Sepal.Length, fill = as.numeric(Species), colour = as.numeric(Species), group = Species)) + 
      geom_density() +
      scale_fill_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20")) +
      scale_colour_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20"))
    

    image

    Any clue why this might be and how to fix it?

  4. clauswilke commented on Oct 20, 2018

    @clauswilke
    CollaboratorAuthor

    It is not the case that color and fill are affected differently. Instead, the lines are drawn on top of the fill area and therefore those regions appear darker. We can exaggerate this effect by making the lines thicker. And we can avoid it by removing the lines altogether.

    library(ggplot2)
    
    # make line thicker to highlight the problem
    ggplot(iris, aes(Sepal.Length, fill = as.numeric(Species), colour = as.numeric(Species), group = Species)) + 
      geom_density(size = 5) +
      scale_fill_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20")) +
      scale_colour_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20"))

    # remove lines altogether
    ggplot(iris, aes(Sepal.Length, fill = as.numeric(Species), group = Species)) + 
      geom_density(color = NA) +
      scale_fill_gradientn(colors = c("#00FF00FF", "#008000B0", "#0000FF20"))

    Created on 2018-10-19 by the reprex package (v0.2.1)

  5. lukasjonkers commented on Oct 20, 2018

    @lukasjonkers

    I see, should have thought about that myself. That makes it hard to use transparency and get rid of the blank/white lines when plotting polygons.

    image

    Because I seem to need the lines for that purpose.

    Code from here

  6. clauswilke commented on Oct 20, 2018

    @clauswilke
    CollaboratorAuthor

    Yes, that's a fundamental limitation of the current R graphics architecture, and it's the reason why my gradient geoms from the ggridges package don't allow transparency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementwipwork in progress

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions