New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function colour_ramp() should respect transparency by default #108

Closed
clauswilke opened this Issue Dec 20, 2017 · 4 comments

Comments

Projects
None yet
3 participants
@clauswilke
Collaborator

clauswilke commented Dec 20, 2017

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

@lukasjonkers

This comment has been minimized.

lukasjonkers commented Oct 19, 2018

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?

@clauswilke

This comment has been minimized.

Collaborator

clauswilke commented Oct 20, 2018

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)

@lukasjonkers

This comment has been minimized.

lukasjonkers commented Oct 20, 2018

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

@clauswilke

This comment has been minimized.

Collaborator

clauswilke commented Oct 20, 2018

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