ggplot2标度填充梯度(带离散上限) [英] ggplot2 scale fill gradient with discrete cap

查看:177
本文介绍了ggplot2标度填充梯度(带离散上限)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找价值的热点图.我希望热图从低值(示例代码中为0)的蓝色变为高值(示例代码中为75)的绿色.但是,数据包含的值大于75.我希望任何大于75的值都用红色填充.

I am looking to have a heat map of values. I want the heat map to go from blue for low values (0 in my sample code) to green for high values (75 in my sample code). However, the data contains values greater than 75. I want any values greater than 75 to be filled in red.

因此,总而言之,我希望将填充从0顶部的75缩放为蓝色到绿色,并将大于75的任何值填充为红色.我现在使用的代码可以执行此操作,但是仍然可以将绿色值从76缩放为红色,而不是将它们全部设置为红色.

So to summarize, I want the fill to be scaled from 0 top 75 in blue to green with any value greater than 75 filled in red. The code I have now sort of does this, but still scales from green to red in values from 76-100 rather than have them all be red.

我在帖子中使用了Brian Diggs的答案( ggplot2热图与),但该答案并未涵盖如何填充超出梯度刻度上限值的所有值.

I have used the answer from Brian Diggs in the post (ggplot2 heatmap with colors for ranged values), but that answer does not cover what to do with filling all values beyond a cap value for a gradient scale.

帖子(带有颜色的ggplot geom_point()基于特定的离散值)似乎为geom_point回答了一个非常相似的问题,但是我很难将其调整为geom_tile.

The post (ggplot geom_point() with colors based on specific, discrete values) seems to answer a very similar question for geom_point, but I am having trouble adapting it to geom_tile.

下面是我的示例代码,感谢您的帮助!

My sample code is below and any help is appreciated!

#Check packages to use in library
{
  library('ggplot2')
  library('scales')
}

#Data

horizontal_position <- c(-2, -1, 0, 1, 2)
vertical_position <- c(-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)
data_val <- sample(0:100, 25)
all_data <-data.frame(horizontal_position, vertical_position, data_val)

     all_data %>%
      ggplot(aes(horizontal_position, vertical_position)) +
      geom_tile(aes(fill = data_val), colour = "transparent") + 
      geom_text(aes(label = data_val),colour="white")+
      scale_fill_gradientn(colours = c("blue", "lightblue", "lightgreen", "green", "red"),
                           breaks=c(0,25,50,75,Inf),
                           guide = "colorbar")

推荐答案

这是部分解决方案,它为图形着色,但使图例不完全正确:

Here's a partial solution, which colors the figure but leaves the legend not entirely correct:

all_data %>% 
  mutate(val2 = replace(data_val, data_val > 75, NA)) %>% 
  ggplot(aes(horizontal_position, vertical_position)) +
  geom_tile(aes(fill = val2), colour = "transparent") + 
  geom_text(aes(label = data_val),colour="white")+
  scale_fill_gradientn(colours = c("blue", "lightblue", "lightgreen", "green"),
                       breaks=c(0,25,50,75,Inf),
                       na.value = "red")

诀窍是将边界值设置为NA,这在大多数美学标度中都有一个特殊的可选值.当然,如果您的实际数据具有真实的NA,则这种情况会崩溃,并且正如我提到的,将其显示在颜色栏中是另一个难题.

The trick is setting your out-of-bounds values to NA, which has a special optional value in most of the aesthetic scales. Of course, this breaks down if your actual data have true NAs, and as I mentioned, getting it to show up in the colorbar is another quagmire.

编辑添加:快速搜索提出了一些解决方案: 添加一个连续映射的ggplot图例的NA值的框

Edited to add: a quick search brought up some solutions: Add a box for the NA values to the ggplot legend for a continous map

这篇关于ggplot2标度填充梯度(带离散上限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆