[英]Axes at minimum extent, no padding, in plots of raster* objects
有沒有辦法確保繪圖周圍的框與光柵范圍完全匹配? 在下面,柵格的上方和下方或左側和右側有一個間隙,具體取決於設備比例:
require(raster)
r = raster()
r[]= 1
plot(r, xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))
柵格對象問題的一個要素是asp=1
以確保正確顯示。 當asp=1
時,以下基本散點圖具有相同的問題:
plot(c(1:10), c(1:10), asp=1)
嘗試使用rasterVis包中的vectorplot(r)
來查看我希望軸看起來像什么。
編輯:
解決方案需要與SpatialPoints疊加層配合使用,而不是顯示指定柵格限制之外的點:
require(raster)
require(maptools)
# Raster
r = raster()
r[]= 1
# Spatial points
x = c(-100, 0, 100)
y = c(100, 0, 100)
points = SpatialPoints(data.frame(x,y))
plot(r, xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))
plot(points, add=T)
您可能最好使用其中一個基於lattice
的函數來繪制raster
和rasterVis
包提供的空間柵格對象。 您在vectorplot()
發現了其中一個,但在這種情況下, spplot()
或levelplot()
更符合您的需求。
( "RasterLayer"
對象的基於base graphics
的plot()
方法不允許任何簡單的方法來設置具有適當寬高比的軸。對於任何有興趣的人,我會詳細介紹為什么會出現這樣的情況。在帖子的底部。)
作為levelplot()
產生的那種情節的一個例子:
require(raster)
require(rasterVis)
## Create a raster and a SpatialPoints object.
r <- raster()
r[] <- 1:ncell(r)
SP <- spsample(Spatial(bbox=bbox(r)), 10, type="random")
## Then plot them
levelplot(r, col.regions = rev(terrain.colors(255)), cuts=254, margin=FALSE) +
layer(sp.points(SP, col = "red"))
## Or use this, which produces the same plot.
# spplot(r, scales = list(draw=TRUE),
# col.regions = rev(terrain.colors(255)), cuts=254) +
# layer(sp.points(SP, col = "red"))
這些方法中的任何一種仍然可以繪制符號的某些部分,該部分表示落在繪制的柵格之外的點。 如果您想避免這種可能性,您可以只對您的SpatialPoints
對象進行子集SpatialPoints
以刪除任何落在柵格之外的點。 這是一個簡單的功能,它將為您做到這一點:
## A function to test whether points fall within a raster's extent
inExtent <- function(SP_obj, r_obj) {
crds <- SP_obj@coord
ext <- extent(r_obj)
crds[,1] >= ext@xmin & crds[,1] <= ext@xmax &
crds[,2] >= ext@ymin & crds[,2] <= ext@ymax
}
## Remove any points in SP that don't fall within the extent of the raster 'r'
SP <- SP[inExtent(SP, r), ]
關於為什么難以制作plot(r)
額外雜草細節產生緊密貼合的軸
當plot
被稱為類型的對象上raster
,光柵數據是(最終),使用任一繪制rasterImage()
或image()
遵循哪條路徑取決於:(a)被繪制的設備類型; (b)原始plot()
調用中的useRaster
參數的值。
在任何一種情況下,繪圖區域的設置方式都會產生填充繪圖區域的軸,而不是以賦予它們適當寬高比的方式。
下面,我將展示在執行此步驟的過程中調用的函數鏈,以及最終設置繪圖區域的調用。 在這兩種情況下,似乎沒有簡單的方法來改變繪制的軸的范圍和縱橫比。
useRaster=TRUE
## Chain of functions dispatched by `plot(r, useRaster=TRUE)` getMethod("plot", c("RasterLayer", "missing")) raster:::.plotraster2 raster:::.rasterImagePlot ## Call within .rasterImagePlot() that sets up the plotting region plot(NA, NA, xlim = e[1:2], ylim = e[3:4], type = "n", , xaxs = "i", yaxs = "i", asp = asp, ...) ## Example showing why the above call produces the 'wrong' y-axis limits plot(c(-180,180), c(-90,90), xlim = c(-180,180), ylim = c(-90,90), pch = 16, asp = 1, main = "plot(r, useRaster=TRUE) -> \\nincorrect y-axis limits")
useRaster=FALSE
## Chain of functions dispatched by `plot(r, useRaster=FALSE)` getMethod("plot", c("RasterLayer", "missing")) raster:::.plotraster2 raster:::.imageplot image.default ## Call within image.default() that sets up the plotting region plot(NA, NA, xlim = xlim, ylim = ylim, type = "n", xaxs = xaxs, yaxs = yaxs, xlab = xlab, ylab = ylab, ...) ## Example showing that the above call produces the wrong aspect ratio plot(c(-180,180), c(-90,90), xlim = c(-180,180), ylim = c(-90,90), pch = 16, main = "plot(r,useRaster=FALSE) -> \\nincorrect aspect ratio")
這是我解決這個問題的方法
require(raster)
r = raster()
# default for raster is 180 row by 360 cols = 64800 cells
# fill with some values to make more interesting
r[]= runif(64800, 1, 1000)
# Set margin for text
par(mar=c(2, 6, 6, 2))
# Set some controls for the raster cell colours and legend
MyBrks<-c(0,1,4,16,64,256,1E20)
MyLbls<-c("<1","<4","<16","<64","<256","<Max")
MyClrs<-c("blue","cyan","yellow","pink","purple","red")
# Plot raster without axes or box or legend
# Note xlim and ylim don't seem do much unless you want to trim x and y
plot(r,
col=MyClrs,
axes=FALSE,
box=FALSE,
legend=FALSE
)
# Set up the ranges and intervals for axes - you can get the min max
# using xmin(r) and ymax(r) and so on if you like
MyXFrm <- -180
MyXTo <- 180
MyXStp <- 60
MyYFrm <- -90
MyYTo <- 90
MyYStp <- 30
# Plot the axes
axis(1,tick=TRUE,pos=ymin(r),las=1,at=seq(MyXFrm,MyXTo ,MyXStp ))
axis(2,tick=TRUE,pos=xmin(r),las=1,at=seq(MyYFrm ,MyYTo ,MyYStp ))
# Plot the legend use xpd to plot the legend outside the plot region
par(xpd=TRUE)
legend(MyXTo ,MyYTo ,
legend=MyLbls[1:6],
col= MyClrs,
fill=Clrs[1:6],
bg=rgb(0,0,0,0.85),
cex=0.9,
text.col="white",
text.font=2,
border=NA
)
# Add some axis labels and a title
text(-220,0,"Y",font=2)
text(0,-130,"X",font=2)
text(0,120,"My Raster",font=4,cex=1.5)
伙計,我很難過,最后只是把前景色調去了。 然后你可以利用柵格繪圖方法調用fields:::image.plot
,它允許你只繪制圖例(第二次,這次顯示墨水!)。 這是不優雅的,但在這種情況下工作:
par("fg" = NA)
plot(r, xlim = c(xmin(r), xmax(r)), ylim = c(ymin(r), ymax(r)), axes = FALSE)
par(new = TRUE,"fg" = "black")
plot(r, xlim = c(xmin(r), xmax(r)), ylim = c(ymin(r), ymax(r)), axes = FALSE, legend.only = TRUE)
axis(1, pos = -90, xpd = TRUE)
rect(-180,-90,180,90,xpd = TRUE)
ticks <- (ymin(r):ymax(r))[(ymin(r):ymax(r)) %% 20 == 0]
segments(xmin(r),ticks,xmin(r)-5,ticks, xpd = TRUE)
text(xmin(r),ticks,ticks,xpd=TRUE,pos=2)
title("sorry, this could probably be done in some more elegant way")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.