【发布时间】:2018-01-13 23:34:47
【问题描述】:
我想绘制一堆栅格,我创建了一个代码来调整每个栅格的中断并通过 for 循环绘制它们。但是我得到了一个有问题的色标尺,我的努力并没有有效地解决这个问题。示例:
我的降水量范围从 0 到 11.000...但大部分数据在 0 和 5.000 之间...很少有高达 11.000。所以我需要改变休息时间来捕捉这种变化......更多的休息时间我有更多的数据。
然后我为此创建了一个中断对象。
但是当我绘制栅格时,比例颜色条变得非常糟糕,非常混乱......
#get predictors (These are a way lighter version of mine)
predictors_full<-getData('worldclim', var='bio', res=10)
predic_legends<-c(
"Annual Mean Temperature [°C*10]",
"Mean Diurnal Range [°C]",
"Isothermality",
"Temperature Seasonality [standard deviation]",
"Max Temperature of Warmest Month [°C*10]",
"Min Temperature of Coldest Month [°C*10]",
"Temperature Annual Range [°C*10]",
"Mean Temperature of Wettest Quarter [°C*10]",
"Mean Temperature of Driest Quarter [°C*10]",
"Mean Temperature of Warmest Quarter [°C*10]",
"Mean Temperature of Coldest Quarter [°C*10]",
"Annual Precipitation [mm/year]",
"Precipitation of Wettest Month [mm/month]",
"Precipitation of Driest Month [mm/month]",
"Precipitation Seasonality [coefficient of variation]",
"Precipitation of Wettest Quarter [mm/quarter]",
"Precipitation of Driest Quarter [mm/quarter]",
"Precipitation of Warmest Quarter [mm/quarter]",
"Precipitation of Coldest Quarter [mm/quarter]",
)
# Crop rasters and rename
xmin=-120; xmax=-35; ymin=-60; ymax=35
limits <- c(xmin, xmax, ymin, ymax)
predictors <- crop(predictors_full,limits)
predictor_names<-c("mT_annual","mT_dayn_rg","Isotherm","T_season",
"maxT_warm_M","minT_cold_M","rT_annual","mT_wet_Q","mT_dry_Q",
"mT_warm_Q","mT_cold_Q","P_annual","P_wet_M","P_dry_M","P_season",
"P_wet_Q","P_dry_Q","P_warm_Q","P_cold_Q")
names(predictors)<-predictor_names
#Set a palette
Blues_up<-c('#fff7fb','#ece7f2','#d0d1e6','#a6bddb','#74a9cf','#3690c0','#0570b0','#045a8d','#023858','#233159')
colfunc_blues<-colorRampPalette(Blues_up)
#Create a loop to plot all my Predictor rasters
for (i in 1:19) {
#save a figure
png(file=paste0(predictor_names[[i]],".png"),units="in", width=12, height=8.5, res=300)
#Define a plot area
par(mar = c(2,2, 3, 3), mfrow = c(1,1))
#extract values from rasters
vmax<- maxValue(predictors[[i]])
vmin<-minValue(predictors[[i]])
vmedn=(maxValue(predictors[[i]])-minValue(predictors[[i]]))/2
#breaks
break1<-c((seq(from=vmin,to= vmedn, length.out = 40)),(seq(from=(vmedn+(vmedn/5)),to=vmax,length.out = 5)))
#plot without the legend because the legend would come out with really messy, with too many marks and uneven spaces
plot(predictors[[i]], col =colfunc_blues(45) , breaks=break1, margin=FALSE,
main =predic_legends[i],legend.shrink=1)
dev.off()
}
然后我写了一个不同的代码来为颜色条设置不同的中断
#Plot the raster with no color scale bar
plot(predictors[[i]], col =colfunc_blues(45) , breaks=break1, margin=FALSE,
main =predic_legends[i],legend=FALSE)
#breaks for the color scale
def_breaks = seq(vmax,vmin,length.out=(10))
#plot only the legend
image.plot(predictors_full[[i]], zlim = c(vmin,vmax),
legend.only = TRUE, col = colfunc_greys(30),
axis.args = list(at = def_breaks, labels =def_breaks,cex.axis=0.5))
但这不起作用,因为颜色与地图中的数字并不完全匹配……看看每张地图中 6.000 的颜色……它是不同的。
关于如何进行此操作的任何提示? 我是 R 的新手,所以为了实现我的目标我付出了很多努力...... 另外,我的数字中有很多小数位...如何将其更改为小数点后 2 位?
编辑:@jbaums 教我使用日志...我喜欢,但它还不是我所寻求的
levelplot(predictors[[12]]+1, col.regions=colorRampPalette(brewer.pal(9, 'Blues')), zscaleLog=TRUE, at=seq(1, 4, len=100), margin=FALSE)
【问题讨论】:
-
您真正希望它是什么样子?
-
我不希望颜色沿数字均匀分布,因为我的高低数字数据很少,而中间的数据太多......如果它们均匀分布,我会得到一张几乎没有的地图浅蓝色和深蓝色,一切都是“中蓝色”,蓝色变化很少。我想要一个极端的浅蓝色阴影,另一个极端的深蓝色阴影,以及数据中确实存在数据的其余蓝色。我发布的代码是我尝试这样做...但上个月我被介绍到编程和 R...我缺乏基础,虽然我已经阅读了很多
-
@jbaums... 你明白这个解释吗?请让我知道如果我不够清楚,我会努力做得更好!提前感谢您的关注!
-
你可以试试对数刻度。例如:
library(rasterVis); library(RColorBrewer); levelplot(predictors[[12]]+1, col.regions=colorRampPalette(brewer.pal(9, 'Blues')), zscaleLog=TRUE, at=seq(1, 4, len=100), margin=FALSE) -
谢谢@jbaums...我真的很喜欢学习这种可能性!但我会继续尝试其他选项,因为使用日志我会丢失规模中降水量的简单信息,尽管我可以看到哪里下雨更多..
标签: r plot raster legend-properties