Skip to content
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

Inconsistent colours in filled contour plot #4268

Closed
dmurdoch opened this issue Nov 17, 2020 · 6 comments
Closed

Inconsistent colours in filled contour plot #4268

dmurdoch opened this issue Nov 17, 2020 · 6 comments

Comments

@dmurdoch
Copy link
Contributor

This bug was reported in https://stackoverflow.com/q/64873086/2554330 where the user couldn't get colours consistent across two graphs. Code here is basically what was posted there.

library(ggplot2)
library(gridExtra)

v <- list()
v[[1]] <- ggplot(faithfuld, aes(waiting, eruptions, z = density)) + stat_contour_filled(breaks=(0:10)*0.005)
v[[2]] <- ggplot(faithfuld, aes(waiting, eruptions, z = 1.1*density)) + stat_contour_filled(breaks=(0:10)*0.005)

do.call(grid.arrange,  v)

Screen Shot 2020-11-17 at 12 28 57 PM

Notice that neither graph lists all bins between the specified breaks, and they are inconsistent about assigning colours to corresponding bins, most obviously at the top end where the first graph leaves out 2 bins and the bottom graph leaves out one.

What I would expect is that if the breaks are specified explicitly, the colour would depend on the specified bins, not just the non-empty ones.

@dmurdoch
Copy link
Contributor Author

A workaround for this is to specify a scale with drop = FALSE, e.g.

ggplot(faithfuld, aes(waiting, eruptions, z = density)) +
          geom_contour_filled(breaks=(0:10)*0.01)+
          scale_fill_viridis_d(drop = FALSE)

So I suppose a fix would be to default to drop = FALSE in geom_contour_filled.

@clauswilke
Copy link
Member

Honestly, I don't think this is a bug. Discrete scales drop empty factor levels by default, and you can set drop = FALSE to prevent this from happening. So your workaround is in fact the correct solution to this problem.

@dmurdoch
Copy link
Contributor Author

I think it's a workaround, not a solution, unless there's a way to specify drop = FALSE without saying anything else about the fill scale. It doesn't make sense for a user to need to know the current colour scheme just to avoid dropping ranges in the contour plot.

@clauswilke
Copy link
Member

It doesn't make sense for a user to need to know the current colour scheme just to avoid dropping ranges in the contour plot.

I agree with this statement, but it's a more general problem than the one encountered here. You also encounter it when you want to, for example, change the labels that go with certain breaks in a scale. You can only do that by explicitly calling a scale function, and to do that you need to know which scale function to call.

This has been bugging me for a while, and I'd like to fix it in a general way, but it's non-trivial.

@clauswilke
Copy link
Member

See e.g. here: #3973 (comment)

@clauswilke
Copy link
Member

I'm closing this in favor of #4269, which describes the general problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants