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

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

has2k1 opened this Issue Nov 18, 2017 · 3 comments


None yet
2 participants

has2k1 commented Nov 18, 2017

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.


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() +

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.


This comment has been minimized.


hadley commented Nov 20, 2017

Would you mind pointing me towards the code involved?


This comment has been minimized.


has2k1 commented Nov 20, 2017

.bar <- discard(pretty(.limits, n = guide$nbin), scale$get_limits())


This comment has been minimized.


hadley commented Nov 20, 2017

Yeah, there doesn't seem to be any need for pretty() there. Do you want to do a PR?

@hadley hadley closed this in #2346 Nov 21, 2017

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018

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