Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude outliers from heat-map in ggplot2?

Hi, I am trying to plot average conversion per square on the map. That works great. Problematic are the squares that have only few records that often reach extreme values close to 0 or 1. That makes the plot hard to read. Is there a way how to exclude squares that do not reach specific record count? Or set up colors range from e.g 0.3 - 0.7?


CODE:

library(ggplot2)
library(ggmap)
manila_map <- get_map("Manila,Philippines", zoom=11)

map <- ggmap(manila_map)
map + stat_summary_2d(
   geom = "tile", 
   data = data,
   fun = "mean",
   binwidth = 0.02,
   aes(x = lon, y = lat, z = requested),
   alpha = 0.4
) +
scale_fill_gradient2(low = "red", mid = "yellow", high = "#007f00", midpoint=0.5)

enter image description here

like image 925
David Lexa Avatar asked Jan 30 '26 21:01

David Lexa


1 Answers

First, change the outlier values in your dataset to NA

data$requested <- ifelse(data$requested <= 0.7 & data$requested >= 0.3, 
                         data$requested, NA)

Then, add na.value within scale_fill_gradient() to make NA values a neutral color

scale_fill_gradient2(low = "red", mid = "yellow", high = "#007f00", midpoint=0.5, 
                     na.value = "grey50")
like image 94
Jan Boyer Avatar answered Feb 02 '26 14:02

Jan Boyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!