在光栅*对象的图中,轴最小程度,无填充

时间:2012-02-17 20:12:46

标签: r

有没有办法确保绘图周围的框与光栅范围完全匹配?在下文中,栅格的上方和下方或左侧和右侧之间存在间隙,具体取决于设备比例:

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)

4 个答案:

答案 0 :(得分:12)

您可能最好使用其中一个基于lattice的函数来绘制rasterrasterVis包提供的空间栅格对象。您在vectorplot()中发现了其中一个,但在这种情况下spplot()levelplot()更符合您的需求。

(基于base graphics的{​​{1}} plot()方法只是不允许您使用适当的宽高比设置轴。对于任何有兴趣的人,我去更详细地说明为什么在帖子底部的一节中就是这样。)

作为"RasterLayer"产生的那种情节的一个例子:

levelplot()

enter image description here

这些方法中的任何一个都可能仍然会绘制符号的某些部分,这些部分表示落在绘制的栅格之外的点。如果您想避免那种的可能性,您可以只对您的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

关于为什么难以让## 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时,(最终)使用rasterrasterImage()绘制栅格数据。遵循哪条路径取决于:(a)被绘制的设备类型; (b)原始image()调用中useRaster参数的值。

在任何一种情况下,绘图区域的设置方式都会产生填充绘图区域的轴,而不是以适合宽高比的方式设置。

下面,我将展示在执行此步骤的过程中调用的函数链,以及最终设置绘图区域的调用。在这两种情况下,似乎没有简单的方法来改变绘制的轴的范围和纵横比。

  • plot()

    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

答案 1 :(得分:1)

男人,我很难过,最后只是把前景色调去了情节。然后你可以利用栅格绘图方法调用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")

enter image description here

答案 2 :(得分:1)

这是解决这个问题的方法

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) 

What the plot looks like

答案 3 :(得分:0)

我认为最好(或最简单)的解决方案是使用image()

library(raster)

# Raster
r = raster()
r[]= rnorm(ncell(r))

# Spatial points
x = c(-100, 0, 100)
y = c(100, 0, 100)
points = SpatialPoints(data.frame(x,y))

# plot
image(r)
plot(points, add=T, pch=16, cex=2)

raster plot with the image function