eliocamp / ggnewscale

Multiple Fill, Color and Other Scales in `ggplot2`
https://eliocamp.github.io/ggnewscale/
GNU General Public License v3.0
405 stars 18 forks source link

new_scale_fill() changes colour to previous geom #17

Closed mchibok closed 5 years ago

mchibok commented 5 years ago

I'm trying to plot a spatial heatmap from various x y points that are separed in 2 categories. I wish to plot both categories with stat_density_2d in different colors (green and red).

However, whenever I add a new_scale_fill(), the background color of previously plotted geom change colors.

For exemple, regular plot:

p <- ggplot() +
  aes(long, lat, group = group) +
  geom_polygon(data = hydro.df, fill = "#d9e2ea", color = NA) +
  geom_polygon(data = iles.df, fill = "white", color = NA) +
  geom_path(data = routes.maj.df,
            #aes(long, lat, group = group),
            colour = rgb(0.9, 0.9, 0.9), size = 0.25) +
  geom_polygon(data = villes.df, fill = NA, color = "red", size = 0.2) +
  stat_density_2d(
    data = subset(av.desav, classement == "avantage"),
    aes(x = x, y = y, group = NA, 
      fill = ..level.., alpha = ..level..),
    geom = "polygon") + 
  scale_fill_gradient("< -5 min.",
    low = "white", high = rgb(10, 255, 10, maxColorValue = 255),
    labels = NULL) +
  guides(alpha = F) +
  coord_fixed(ratio = 1, xlim = x.lim, ylim = y.lim, expand = F) +
  theme_void()

plot(p)

gives this: image

But when I try to add a scale to plot another stat_density, this is what I get:

plot(p + new_scale_fill() #+
  # not plotted
  # stat_density_2d(data = subset(av.desav, classement == "desavantage"),
  #                 aes(x = x, y = y,
  #                     fill = ..level.., alpha = ..level..),
  #                 geom = "polygon") +
  # scale_fill_gradient("> 5 min.",
  #                     low = "white", high = rgb(255,10,10, maxColorValue = 255),
  #                     labels = NULL)
)

image

The various geom_polygon data come from shapefiles. I don't know if that could be the problem. Not sure if it's a bug or something I'm not doing right.

Thanks for your help!

eliocamp commented 5 years ago

Hi! I think this should be fixed with https://github.com/eliocamp/ggnewscale/issues/16, but I might be wrong on that. Are you using the latest development version? If not, try updating it and see if it works.

Otherwise, can you get a minimum reproducible example so I can investigate further? Since I don't have the data for your polygons I can't run those :).

mchibok commented 5 years ago

Hey, Just updated the version from 0.3.0 to 0.4.0.9 and it works! Thanks for your help and the package.