Skip to content

Inconsistent colours in filled contour plot #4268

Closed

Description

@dmurdoch

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.

Activity

  1. dmurdoch commented on Nov 17, 2020

    @dmurdoch
    ContributorAuthor

    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.

  2. clauswilke commented on Nov 17, 2020

    @clauswilke
    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.

  3. dmurdoch commented on Nov 17, 2020

    @dmurdoch
    ContributorAuthor

    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.

  4. clauswilke commented on Nov 17, 2020

    @clauswilke
    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.

  5. clauswilke commented on Nov 17, 2020

    @clauswilke
    Member

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

  6. clauswilke commented on Nov 17, 2020

    @clauswilke
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions