Home > database >  How to create a gap of values in a gradient scale in R
How to create a gap of values in a gradient scale in R

Time:03-15

I am currently creating a map of the U.S. with a gradient of counts of a variable ('totalvisits') within each state.

There is one state that has really high count (i.e., MA - over 8000) while every other state are <500. Would it be possible for me to create a gap in the gradient scale such that values from 500 - 8000 are skipped. I'd also like to have something where the differences between states of lower values can be better visualized, because right now the heat map makes it look like they all have the same value (picture below)

This is my current code:

statebins_US <- 
  statebins(state_data = patient_origin_bystate, state_col = "region", value_col = "totalvisits", dark_label = "black", light_label = "white", font_size = 3)   
  theme_statebins(legend_position="right")   
  scale_fill_gradientn(colours = rev(brewer.pal(10, "Spectral")), na.value = "gray78") 
 

Current Heat Map - Where States of Lower Values all have Same Color

CodePudding user response:

I am not going to answer your question, but make a suggestion in another direction. If your values are all strictly positive, I'm going to recommend that you use a log-transformation instead. (You could use a pseudo-log transform if your values include negative values or 0)

Here is a brief mock-up of your problem, where you have very few outlier values.

library(ggplot2)

set.seed(0)
df <- expand.grid(LETTERS[1:6], LETTERS[1:6])
df$value <- rlnorm(nrow(df), meanlog = 6)

p <- ggplot(df, aes(Var1, Var2))  
  geom_tile(aes(fill = value))

p   scale_fill_distiller(palette = "Spectral")

My recommendation is to use a log transformed colour palette, instead of a discontinuous palette. In such cases it should use a larger variety of colours of the palette.

p   scale_fill_distiller(palette = "Spectral", trans = "log10")

Created on 2022-03-10 by the reprex package (v2.0.1)

  • Related