简体   繁体   English

如何获得多个具有相同比例的 ggplot2 scale_fill_gradientn?

[英]How to get multiple ggplot2 scale_fill_gradientn with same scale?

library(ggplot2)
library(cumplyr)
library(scales)
library(RColorBrewer)


myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))

x   = 1:5
y   = 1:5
pts = cartesian_product(c('x','y'))
d1 = cbind(pts, runif(nrow(pts),min=0,max=1), 1)
d2 = cbind(pts, runif(nrow(pts),min=0,max=4), 2)
colnames(d1) = colnames(d2) = c("x","y","val","k")
d  = rbind(d1,d2)

p1 <- ggplot(d1)
p1 <- p1 + geom_tile(aes(x = x, y = y, fill = val))
p1 <- p1 + scale_fill_gradientn(colours = myPalette(4))
p1

p2 <- ggplot(d2, aes(x = x, y = y, fill = val))
p2 <- p2 + geom_tile(aes(x = x, y = y, fill = val)) 
p2 <- p2 + scale_fill_gradientn(colours = myPalette(4))
p2

And this leads to the two plots below.这导致了下面的两个图。 My question is, using this same type of color scheme, how do I get both plots to use the same scale for value?我的问题是,使用这种相同类型的配色方案,如何让两个图使用相同的值比例? Eg p1 should be much more uniform than p2.例如,p1 应该比 p2 更均匀。

p1: p1:p1

p2: p2:在此处输入图片说明

Use limit in scale_gradientn :scale_gradientn使用limit

p1 <- ggplot(as.data.frame(d1))
p1 <- p1 + geom_tile(aes(x = x, y = y, fill = val))

p2 <- ggplot(as.data.frame(d2), aes(x = x, y = y, fill = val))
p2 <- p2 + geom_tile(aes(x = x, y = y, fill = val)) 
library(gridExtra)

p1 <- p1 + scale_fill_gradientn(colours = myPalette(4), limits=c(0,4))
p2 <- p2 + scale_fill_gradientn(colours = myPalette(4), limits=c(0,4))
grid.arrange(p1, p2)

在此处输入图片说明

The grid.arrange stuff is just to avoid me having to copy/paste two pictures. grid.arrange东西只是为了避免我不得不复制/粘贴两张图片。

In scale_fill_gradientn set the limits to be the same, so for example:在 scale_fill_gradientn 中将限制设置为相同,例如:

scale_fill_gradientn(colours = myPalette(4),limits=c(0,4))

And this is p1 and p2:这是 p1 和 p2:

p1

p2

暂无
暂无

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

相关问题 用ggplot2的scale_fill_gradientn表示,恒定渐变 - Representation with, scale_fill_gradientn ggplot2, constant gradient 在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 中使用绝对值而不是相对值 - Use absolute values not relative values in scale_fill_gradientn 为负值和正值定义颜色渐变 scale_fill_gradientn() - define color gradient for negative and positive values scale_fill_gradientn() 如何在ggplot2的scale_colour_gradientn中指定颜色编号 - How to specify the colour number in scale_colour_gradientn of ggplot2 使用 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 使用转换后的颜色/填充变量 ggplot2 为 scale_*_gradientn 指定手动值 - Specify manual values for scale_*_gradientn with transformed color/fill variable ggplot2 在热图中为比例设置 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM