rggplot2colorbrewer

geom_contour_filled colour palette


I was trying to run the following code, but I can't seem to get to change the colour palette to the Spectral palette from ColorBrewer. Thoughts?

maya <- tibble(
  mass = seq(1, 10, length.out = 10),
  mois = seq(11, 20, length.out = 10)
) %>%
  expand(mass, mois) %>%
  mutate(
    diff = mois - mass * runif(1)
  ) %>%
  ggplot(aes(mass,mois,z = diff)) +
    geom_contour_filled() +
    scale_fill_distiller(palette = "Spectral")
maya

Solution

  • If I understand correctly, perhaps you're after something like the combination of geom_raster with interpolate = TRUE and scale_fill_distiller?

    library(tidyverse)
    
    tibble(
      mass = seq(1, 10, length.out = 10),
      mois = seq(11, 20, length.out = 10)
    ) %>%
      expand(mass, mois) %>%
      mutate(
        diff = mois - mass * runif(1)
      ) %>%
      ggplot(aes(mass,mois,fill = diff)) +
      geom_raster(interpolate = TRUE) +
      scale_fill_distiller(palette = "Spectral")
    

    Created on 2020-04-13 by the reprex package (v0.2.1)

    To expand, scale_fill_distiller can interpolate the colour scale to fit a continuous range of values, but it cannot actually interpolate your data, as it were. As far as I know, no such functionality is built into geom_contour_filled either. Therefore, I think you either need to do your interpolation by hand before plotting, or rely on something like the interpolation in geom_raster.