简体   繁体   English

具有固定截止值的连续缩放填充渐变 R

[英]Continuous scale fill gradientn with fixed cutoffs R

I can't figure out how to define fixed cutoffs in a gradient fill.我不知道如何在渐变填充中定义固定截止。 This is an example on the iris data:这是 iris 数据的一个例子:

ggplot(iris, aes(x=Sepal.Length,
                 y=Sepal.Width,
                 fill=Petal.Length))+
  geom_point(shape=21)+
  scale_fill_gradientn(colors=c("white","black","darkred","red"))

In my actual data, I have a heatmap with many values 0, a lot between 1-10, a few outliers in the hundreds and thousands.在我的实际数据中,我有一个热图,其中包含许多值 0、1-10 之间的很多值,以及成百上千的一些异常值。

I would like the gradient to be:我希望渐变为:

  • 0 = white 0 = 白色
  • 1-10 = from gray to black 1-10 = 从灰色到黑色
  • 11-100 = from black to darkred 11-100 = 从黑色到深红色
  • 101-1000 = from darkred to red 101-1000 = 从深红色到红色
  • over 1000 = purple超过 1000 = 紫色

I don't want to transform it in a factor with bins, because I do want to maintain the continuous gradient especially at the low parts of the scale.我不想用 bin 将它转换为一个因子,因为我确实想保持连续梯度,尤其是在比例的低部分。

Thank you for you help谢谢你的帮助

You will have to generate you own color pal with colorspace for the white to red, but if you're okay with the default blues or viridis, you can have that out of the box with scale_fill_binned() :您必须为白色到红色生成具有颜色colorspace的自己的颜色伙伴,但如果您对默认的蓝色或绿色没有问题,您可以使用scale_fill_binned()开箱即用:

ggplot(iris, aes(x=Sepal.Length,
                 y=Sepal.Width,
                 fill=Petal.Length))+
  geom_point(shape = 21, size = 4) +
  scale_fill_binned(breaks = c(2, 5), type = "viridis")

在此处输入图像描述

https://ggplot2.tidyverse.org/reference/guide_coloursteps.html https://ggplot2.tidyverse.org/reference/guide_coloursteps.html

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

相关问题 R:使用带有geom_tile()和scale_fill_gradientn()的颜色划分热图 - R: Dividing heat map using colors with geom_tile() and scale_fill_gradientn() 为负值和正值定义颜色渐变 scale_fill_gradientn() - define color gradient for negative and positive values scale_fill_gradientn() 用ggplot2的scale_fill_gradientn表示,恒定渐变 - Representation with, scale_fill_gradientn ggplot2, constant gradient 在 scale_fill_gradientn 中使用绝对值而不是相对值 - Use absolute values not relative values in scale_fill_gradientn 在 R 中使用 scale_color_gradientn() 手动缩放颜色渐变 - Manual scaling of color gradient with scale_color_gradientn() in R R: ggplot2 为 scale_fill_continuous 指定数据集 - R: ggplot2 specify dataset for scale_fill_continuous na.value 不能在 r 中的 scale_fill_continuous() 中工作 - na.value can not work in scale_fill_continuous() in r R Stargazer:如何在音符中将动态截止值与固定字符向量组合起来 - R Stargazer: How to combine dynamic cutoffs with fixed character vector in notes 如何获得多个具有相同比例的 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()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM