eliocamp / metR

Tools for Easier Analysis of Meteorological Fields
https://eliocamp.github.io/metR/
139 stars 22 forks source link

guide_colorsteps() doesn't work as expected #188

Closed pascaloettli closed 5 months ago

pascaloettli commented 5 months ago

Trying to reproduce the example of discretised_scale(), it fails to produce the correct result:

library(metR)
library(ggplot2)
library(data.table)
temperature <- copy(temperature)
temperature[, air.z := Anomaly(air), by = .(lat, lev)]

ggplot(temperature[lev == 300], aes(lon, lat, z = air.z)) +
  geom_contour_fill(aes(fill = after_stat(level)), breaks = c(-10, -8, -6, -2, -1, 0, 6, 8, 10)) +
  guides(fill = guide_colorsteps())

Rplot12

ggplot2_3.5.0 metR_0.15.0

pascaloettli commented 5 months ago

ggplot2 3.5.0 breaks the function. It works as expected with ggplot2 3.4.4

Rplot13

eliocamp commented 5 months ago

guide_colorsteps() is a ggplot2 function. The changes in the guide system seem to have messed it up somehow. Here's an pure ggplot2 example:

library(ggplot2)
levels <- pretty(range(volcano))

volcano2 <- reshape2::melt(volcano) |> 
  transform(value_discrete = cut(value, 
                                 breaks = levels,
                                 ordered_result = TRUE))

ggplot(volcano2, aes(Var1, Var2)) +
  geom_raster(aes(fill = value_discrete)) 

ggplot(volcano2,  aes(Var1, Var2)) +
  geom_raster(aes(fill = value_discrete)) +
  guides(fill = guide_colorsteps())

Created on 2024-03-20 with reprex v2.0.2

I opened an issue on their repo, which you can follow here: https://github.com/tidyverse/ggplot2/issues/5786

eliocamp commented 5 months ago

BTW, this has been fixed in the development version of ggplot2

pascaloettli commented 5 months ago

Sorry, I missed the fact is was a ggplot2 function and not a metR function. Sorry for the annoyance