0

Consider the following image:

R plot

Although the underlying dataset does not contain values below 0.1 degrees Celsius, I want to and still can force the legend to display the label and box for 'Below 0.1', but ggplot won't recognise the legend colour values I've chosen (instead of black, it's the default grey background colour), nor does it recognise the black bordering I specified for each legend colour box.

A snippet of the code used is below:

> colours
[1] "black"          "cornflowerblue" "lightblue"  
> legend_labels
[1] "Below 0.1°C"    "0.1°C to 3.0°C" "3.1°C to 5.0°C" "Missing"       
> legend_limits
[1] "(-Inf,0]" "(0,3]"    "(3,5]"    NA  

    ggplot(
      extreme_days,
      aes(
        .data$Day_number,
        .data$Seasons_ago,
        fill = .data$Temp_category,
        na.rm = FALSE
      )
    ) +
    geom_tile(na.rm = TRUE, width = 1) +
    scale_fill_manual(
      values = colours,
      name = paste(measure_label, 'temperature:'),
      labels = legend_labels,
      limits = legend_limits,
      na.value = na_colour
    ) +
    guides(fill = guide_legend(override.aes = list(col = "black")))

I tried using drop = FALSE in scale_fill_manual() to ensure all factor levels are used, but it does not work.

If I change colours to c('black', 'cornflowerblue', 'lightblue', 'white') to include white as well, that doesn't work either.

Also, removing na.rm = FALSE in aes() in ggplot() does not work either.

Importantly, I do not experience this issue with ggplot2 version 3.4.4. When I upgrade to version 3.5.0, I encounter this issue.

To reproduce the example above, see my code at: https://github.com/a-s-russo/austemp. The relevant functions (in particular, plot_temperatures()) are in the flat_minimal_package.Rmd file in the dev/ folder on the main branch. The following call to plot_temperatures() is used (using the default thresholds of c(0, 3, 5)):

> devtools::install_github("a-s-russo/austemp")
> library(austemp)
> data('Sydney')
> plot_temperatures(data = Sydney, season = 'winter')
1
  • 1
    This is most likely due to a change in ggplot2 3.5.0. To show the legend key add show.legend = TRUE to geom_tile. See e.g. my answer on this post.
    – stefan
    Commented Mar 25 at 7:51

0