ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Color expansion of `geom_contour_filled`

Open wbvguo opened this issue 2 years ago • 1 comments

Dear ggplot2 developer,

I was browsing the official documentation of geom_contour_filled (https://ggplot2.tidyverse.org/reference/geom_contour.html), and wonder if it would be possible to add a expand parameter (similar to what we did in scale_x_continous) that can be used to expand the colored region outwards. This might be helpful in situation where the contour data only capture a part of the full spectrum (the rest part are with low frequency/level, likely not presented in observed data, but they are still important to display on the figure)

Specifically, in the documentation example image

Thanks, Wenbin

wbvguo avatar Dec 08 '23 21:12 wbvguo

Hi Wenbin,

If the input data is a grid of values, as is the case for geom_contour_filled(), I think the correct approach would be to ensure that the relevant values are present in the input data. If the input data are individual x/y observations, the geom_density_2d() function can compute density values outside the bounds of the input data. I don't think it makes sense to extrapolate in the case of geom_contour_filled(), but it is fine to compute density in geom_density_2d().

For geom_density_2d(), you can just use the x-scale limits to expand the region:

library(ggplot2)

ggplot(faithful, aes(waiting, eruptions)) +
  geom_density2d_filled(n = 75, h = c(10, 1)) +
  xlim(NA, 120)

Created on 2023-12-11 with reprex v2.0.2

teunbrand avatar Dec 11 '23 10:12 teunbrand

I'm closing this issue as there appears to be a reasonable path to the desired outcome without the need for changing anything in ggplot2. If I have misunderstood the issue, please clarify and we can reopen the issue.

teunbrand avatar Feb 22 '24 15:02 teunbrand