gpt4 book ai didi

r - R中不同绘图具有相同色标的色带

转载 作者:行者123 更新时间:2023-12-05 01:19:57 26 4
gpt4 key购买 nike

我需要创建一些光栅图形,但要保持颜色渐变比例相同,但我还希望颜色渐变是平滑的渐变。是否可以保留大量颜色(约 100 种以具有平滑的色带),但同时具有合理数量的中断,以便可读?

library(raster)
library(colorRamps)

r1<- raster(ncol=56, nrow=26)
r1[] <- runif(n=56*26,min=-20,max=15)

r2<- raster(ncol=56, nrow=26)
r2[] <- runif(n=56*26,min=-14,max=68)


brk=seq(-50,70,length.out=100)
col=matlab.like(100)

plot(r1, breaks=brk, col=col)
plot(r2, breaks=brk, col=col)

在这种情况下,我有一个我想要的颜色渐变,但你无法阅读中断标签 enter image description here

当我减少中断的次数时,色带变成了一种颜色

brk=seq(-50,70,length.out=6)

enter image description here

最佳答案

您可能会发现使用 ggplot2 会更容易.在下面的代码中,对于每个图,关键是为 low 设置相同的颜色值。 , mid , 和 high同样limitsscale_fill_gradient2 .这保证了相同的数据值被映射到每个图中的相同颜色。例如:

library(rasterVis)
library(ggplot2)

# Reproducible rasters
set.seed(4598)
r1<- raster(ncol=56, nrow=26)
r1[] <- runif(n=56*26,min=-20,max=15)

r2<- raster(ncol=56, nrow=26)
r2[] <- runif(n=56*26,min=-14,max=68)

# Get range of data values across the two rasters
rng = range(c(getValues(r1), getValues(r2)))

gplot(r1) +
geom_tile(aes(fill=value)) +
ggtitle("r1") +
scale_fill_gradient2(low="red", mid="green", high="blue",
midpoint=mean(rng), # Value that gets the middle color (default is zero)
breaks=seq(-100,100,10), # Set whatever breaks you want
limits=c(floor(rng[1]), ceiling(rng[2]))) # Set the same limits for each plot

gplot(r2) +
geom_tile(aes(fill=value)) +
ggtitle("r2") +
scale_fill_gradient2(low="red", mid="green", high="blue",
midpoint=mean(rng), # Value that gets the middle color (default is zero)
breaks=seq(-100,100,10), # Set whatever breaks you want
limits=c(floor(rng[1]), ceiling(rng[2]))) # Set the same limits for each plot

enter image description here

关于r - R中不同绘图具有相同色标的色带,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com