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

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

Closed
has2k1 opened this issue Nov 18, 2017 · 3 comments · Fixed by #2346
Closed

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

has2k1 opened this issue Nov 18, 2017 · 3 comments · Fixed by #2346

Comments

@has2k1
Copy link
Contributor

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.

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.

@hadley
Copy link
Member

hadley commented Nov 20, 2017

Would you mind pointing me towards the code involved?

@has2k1
Copy link
Contributor Author

has2k1 commented Nov 20, 2017

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

@hadley
Copy link
Member

hadley commented Nov 20, 2017

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

@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.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants