ecodata icon indicating copy to clipboard operation
ecodata copied to clipboard

rework temperature scales to show both C and F

Open sgaichas opened this issue 1 year ago • 4 comments
trafficstars

scientists understand C, stakeholders understand F, we should show both on our absolute temperature and anomaly scales.

Kim Hyde has a nice example for absolute temperature Screen Shot 2024-02-01 at 11 38 05 AM

sgaichas avatar Feb 01 '24 16:02 sgaichas

Will need to look into how to do that in ggplot2. Do either of you know how?

andybeet avatar Feb 01 '24 19:02 andybeet

I don't. Happy to look into it after I unbury myself a bit, unless someone wants to take a stab sooner.

BBeltz1 avatar Feb 01 '24 19:02 BBeltz1

we need to do this for all spatial maps

  • [x] plot_bottom_temp_seasonal_gridded (this also needs a continuous scale)
  • [x] plot_seasonal_sst_anomaly_gridded
  • [x] plot_ches_bay_sst

So i dont know how to make this "dual" legend. As a compromise you can call any of these functions with a scale argument. The default is scale ="celsius" - so catalog will build using this. For presentations/managers you can use scale ="fahrenheit"

andybeet avatar Feb 08 '24 17:02 andybeet

I've seen two ways to possibly achieve this, neither really accomplish the neat color scale in the image.

One way is to hack the text labels on the legend. This won't achieve the same look as above (F on one side, C on the other), but it will put both units at the same breaks as a longer string of text ex. paste0(x, "°F / ", round(fahrenheit_to_celsius(x)), "°C").

library(ggplot2)
library(scales)

# Sample data
df <- data.frame(
  x = 1:100,
  y = rnorm(100),
  temperature_f = seq(32, 212, length.out = 100)  # Fahrenheit values
)

# Function to convert Fahrenheit to Celsius
fahrenheit_to_celsius <- function(f) {
  (f - 32) * 5/9
}

# Create the plot
ggplot(df, aes(x, y, color = temperature_f)) +
  geom_point(size = 3) +
  scale_color_gradientn(
    colors = c("blue", "green", "yellow", "red"),
    breaks = seq(32, 212, by = 20),  # Breaks in Fahrenheit
    labels = function(x) {
      # Add both Fahrenheit and Celsius labels
      paste0(x, "°F / ", round(fahrenheit_to_celsius(x)), "°C")
    }
  ) +
  labs(color = "Temperature") +
  theme_minimal() +
  theme(legend.position = "right")

The other (which I haven't tried) is to use {ggnewscale} and make a separate scale for the two units. I don't know if you'd be able to combine them this way.

See: http://gradientdescending.com/how-to-use-multiple-color-scales-in-ggplot-with-ggnewscale/ .

adamkemberling avatar Oct 17 '24 14:10 adamkemberling