[英]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)))
You just need to set the limits
parameter of scale_fill_gradientn
:您只需要设置scale_fill_gradientn
的limits
参数:
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.