简体   繁体   English

在 scale_fill_gradientn 中使用绝对值而不是相对值

[英]Use absolute values not relative values in scale_fill_gradientn

I would like a smooth gradient starting from red at 0 to white at 0.5 to green at 1.我想要一个平滑的渐变,从 0 处的红色到 0.5 处的白色到 1 处的绿色。

My data doesn't cover this full range, and it seems scale_fill_gradientn takes the values argument to be relative to the data, rather than their actual values.我的数据没有涵盖这个完整范围,而且scale_fill_gradientn似乎将values参数与数据相关,而不是它们的实际值。

The plot produced below shows white above 0.5 rather than at 0.5.下面生成的图显示白色高于 0.5 而不是 0.5。 How can I correct this and set the gradient limits to the specific values I write, rather than relative to the data?我该如何纠正这个问题并将梯度限制设置为我写的特定值,而不是相对于数据?

library(tidyverse)
set.seed(1)
expand_grid(x=1:10, y=1:10) %>%
  mutate(val = rnorm(100, mean = 0.55, sd = 0.05)) %>%
  ggplot(aes(x, y)) +
  geom_tile(aes(fill = val)) +
  geom_text(aes(label = round(val, 3))) + 
  scale_fill_gradientn(colours = c("red","white","green"),
                       values = scales::rescale(c(0.0, 0.5, 1.0)))

heatmap_example

You just need to set the limits parameter of scale_fill_gradientn :您只需要设置scale_fill_gradientnlimits参数:

library(tidyverse)
set.seed(1)
expand_grid(x=1:10, y=1:10) %>%
  mutate(val = rnorm(100, mean = 0.55, sd = 0.05)) %>%
  ggplot(aes(x, y)) +
  geom_tile(aes(fill = val)) +
  geom_text(aes(label = round(val, 3))) + 
  scale_fill_gradientn(colours = c("red","white","green"),
                      limits = c(0.0, 1.0))

Created on 2020-02-18 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 2 月 18 日创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为负值和正值定义颜色渐变 scale_fill_gradientn() - define color gradient for negative and positive values scale_fill_gradientn() 在ggplot中与geom_bin2d结合获得相对计数,并正确使用scale_fill_gradientn - Get relative count in ggplot combined with geom_bin2d, and proper use of scale_fill_gradientn 在热图中为比例设置 scale_fill_gradientn 的限制(从 0 到 1 的值带有蓝色刻度,0 是白色,大于 1 的值带有红色刻度) - Set limits for scale_fill_gradientn for ratios (values from 0 to 1 with blue scale, 0 is white and values over 1 with red scale) in heatmap 用ggplot2的scale_fill_gradientn表示,恒定渐变 - Representation with, scale_fill_gradientn ggplot2, constant gradient 如何获得多个具有相同比例的 ggplot2 scale_fill_gradientn? - How to get multiple ggplot2 scale_fill_gradientn with same scale? 使用 scale_fill_gradientn() 将颜色比例转换为概率转换颜色分布 - Transform color scale to probability-transformed color distribution with scale_fill_gradientn() R:使用带有geom_tile()和scale_fill_gradientn()的颜色划分热图 - R: Dividing heat map using colors with geom_tile() and scale_fill_gradientn() 使用 scale_fill_gradientn 将特定颜色分配给条形 plot 中的确定值 - Assign specific color to definite value in bar plot using scale_fill_gradientn scale_fill_gradientn() 成功填充 map 但未能在图例中显示所有 colors - The scale_fill_gradientn() successfully fills map but fails showing all colors in the legend 使用转换后的颜色/填充变量 ggplot2 为 scale_*_gradientn 指定手动值 - Specify manual values for scale_*_gradientn with transformed color/fill variable ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM