【问题标题】:How to exclude outliers from heat-map in ggplot2?如何从ggplot2的热图中排除异常值?
【发布时间】:2017-11-30 12:22:05
【问题描述】:

嗨, 我正在尝试在地图上绘制每平方的平均转化率。这很好用。有问题的是只有很少记录的正方形,这些记录经常达到接近 0 或 1 的极值。这使得图难以阅读。有没有办法排除未达到特定记录数的方块?或者设置颜色范围从例如 0.3 - 0.7?


代码:

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)

【问题讨论】:

  • 你们有SO用户的样本数据吗?如果可以,可以上传吗?

标签: r ggplot2 visualization heatmap ggmap


【解决方案1】:

首先,将数据集中的异常值更改为NA

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

然后,在scale_fill_gradient() 中添加na.value 以使NA 值成为中性色

scale_fill_gradient2(low = "red", mid = "yellow", high = "#007f00", midpoint=0.5, 
                     na.value = "grey50")

【讨论】:

  • 很好的解决方法@Jan Boyer。问题是,如果数据已经有 NA,那么异常值就会与缺失值混淆
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 2021-12-20
相关资源
最近更新 更多