rggplot2ggthemes

Setting midpoint for ggthemese continuous scales


In ggplot2, we can use the midpoint argument in scale_fill_gradient2() to set the midpoint of divergence, however this does not exist when I try to use color themes from ggthemes such as scale_fill_gradient2_tableau()

# A tibble: 6 x 2
  Year     Value
  <chr>    <dbl>
1 2002   0.0109 
2 2001  -0.0273 
3 2000   0.0113 
4 1999   0.0148 
5 1998   0.00841
6 1997   0.0361 

ggplot(df, aes(x = Year, y = Value)) +
  geom_col(aes(fill = Value)) +
  scale_fill_gradient2_tableau()

I would like the midpoint to be at 0 but now it is at 0.75.

enter image description here


Solution

  • You can centre the colorbar at zero by specifying limits that are equally distant from zero, e.g.

    library(tidyverse)
    #install.packages("ggthemes")
    library(ggthemes)
    
    Year <- list("2002", "2001", "2000", "1999", "1998", "1997")
    Value <- list(0.0109, -0.0273, 0.0113, 0.0148, 0.00841, 0.0361)
    df <- do.call(rbind, Map(data.frame, Year=Year, Value=Value))
    
    ggplot(df, aes(x = Year, y = Value)) +
      geom_col(aes(fill = Value)) +
      scale_fill_gradient2_tableau(limits = c(-0.04, 0.04))
    

    example plot with centred midpoint