Skip to content

Colourbar does not have all colors of the scale. #2343

@has2k1

Description

@has2k1

For some data it is possible for the colourbar to cover a fraction of the colourspace smaller than that covered by the data. It can be confusing when the guide for a continuous scale does span the full mapped space of an aesthetic.

Actually, this bug is probably triggered every time there is a colourbar, but in most cases it is small and imperceptible. With colourmaps like viridis, the cases where it is obvious should only go up.

In the reprex below, I had to do some contriving (nbin=7) to reveal it.

library(ggplot2)

ggplot(faithful, aes(x = eruptions, y = waiting)) +
  geom_point()  +
  stat_density_2d(aes(fill=..level..), geom='polygon') +
  xlim(0.5, 6) +
  ylim(40, 110) +
  scale_fill_viridis_c() +
  guides(fill=guide_colorbar(nbin=7))

The root of the issue is pretty does not guarantee that the breaks it computes are close enough to the limits that the difference is not perceptible when mapped to a colour.

A simple but partial solution is to increase the default guide$nbin parameter. This would give pretty enough wiggle room to work with and reduce incidences of the bug.

A better solution is recognising that the "raster" colourbar does not need to be generated using 'pretty' breaks, a seq(.limits[1], .limits[2], length=guide$nbin) can do. For this solution the current value of nbin need not change except for the craziest of colourmaps.

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