绘图问题-图例栏比例,中断,图例,小数 [英] Plot issues - legend bar scale, breaks, legend, decimals

查看:76
本文介绍了绘图问题-图例栏比例,中断,图例,小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一堆栅格,我创建了一个代码来调整每个栅格的中断,并通过for循环对其进行绘图.但是我遇到了一个有问题的颜色比例尺,而我的努力并没有有效地解决这个问题.示例:

I want to plot a bunch of rasters and I created a code to adjust breaks for each one and plot them trough a for loop. But i'm getting a problematic color scale bar, and my efforts haven't being effective to solve that. Example:

我的降水量在0到11.000之间...但是大部分数据在0到5.000之间...很少有11.000以下的数据.因此,我需要更改休息时间以捕获这种变化...在我拥有更多数据的地方,更多的休息时间.

I have precipitation ranging from 0 to 11.000...but most part of the data is between 0 and 5.000... and very few up to 11.000. So I need to change the breaks to capture this variation... more breaks where I have more data.

然后我为此创建了一个breaks对象.
但是当我绘制栅格时,比例尺的颜色栏变得非常糟糕,非常混乱...

Then I created a breaks object for that.
But when I plot the raster, the scale color bar gets awful, very messy...

#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()
}

该图是循环中所有栅格的i = 12

This figure is the i=12 from all rasters in the loop

然后,我编写了不同的代码以对颜色条设置不同的中断

Then I wrote a different code to set different breaks to the color bar

#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的颜色...这是不同的.

But that doesn't work, because the colors don't really match the numbers in the map... Look at the color for 6.000 in each map... It's different.

有关如何进行此操作的任何提示? 我是R的新手,所以我很难为达到目标而奋斗... 另外,我在数字中得到了很多小数位...如何将其更改为2个小数位?

Any tips on how to proceed on that? I'm new to R so I struggle a lot to reach my goals... Also, I'm getting a lot of decimal places in the numbers... how to change that for 2 decimal places?

@jbaums教我使用日志...我喜欢,但现在还不是我想要的

@jbaums taught me to use log... I liked but it's not yet what I seek

levelplot(predictors[[12]]+1, col.regions=colorRampPalette(brewer.pal(9, 'Blues')), zscaleLog=TRUE, at=seq(1, 4, len=100), margin=FALSE)

推荐答案

您可以使用classInt包中的classIntervals()函数避免日志缩放(某些用户说过).

You can avoid log scale (as some users said you) using classIntervals() function from classInt package.

使用levelplot()(我认为结果优于raster::plot()函数):

Using levelplot() (in my opinion the result is better than raster::plot() function):

# Normal breaks
break1 <- classIntervals(predictors[[12]][!is.na(predictors[[12]])], n = 50, style = "equal")

levelplot(predictors[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')), at=break1$brks, margin=FALSE,main =predic_legends[12])

# Using quantiles
break1 <- classIntervals(predictors[[12]][!is.na(predictors[[12]])], n = 50, style = "quantile")

levelplot(predictors[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')), at=break1$brks, margin=FALSE,main =predic_legends[12])

此外,您还有更多选择可供选择,例如sdprettykmeanshclust等.

Also, you have more options to choose, such like sd, pretty, kmeans, hclust and others.

首先,我将上面的图保存到p,对于该示例,该行太长了:

First, I'll save the plot above to p, the line is too long for this example:

p <- levelplot(predictors[[12]], col.regions=colorRampPalette(brewer.pal(9, 'Blues')), at=break1$brks, margin=FALSE,main =predic_legends[12])

我将使用与wrld_simpl数据相同的数据作为要添加到图中的多边形,并且我还将创建要添加到图中的点.

I'll use the same data than your, wrld_simpl data, as polygons to add into the plot and I'll create points to be added to the plot also.

library(maptools)
library(rgeos)

data(wrld_simpl)
pts <- gCentroid(wrld_simpl, byid = T)

要添加线,多边形,点甚至文本,可以使用layer()函数和panel.spplot对象:

To add lines, polygons, points or even text, you can use layer() function and a panel.spplot object:

p + layer(sp.polygons(wrld_simpl)) + layer(sp.points(pts))

最后,您还可以更改颜色,填充,符号体系等:

Finally, you can also change color, fill, symbology, and so on:

p + layer(sp.polygons(wrld_simpl,col='firebrick')) + layer(sp.points(pts,pch = 12,col='red'))

检查?panel.spplot了解更多信息.

这篇关于绘图问题-图例栏比例,中断,图例,小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆