R栅格在绘图时避免使用空白区域 [英] R raster avoid white space when plotting

查看:474
本文介绍了R栅格在绘图时避免使用空白区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片,我想在0,0左右底角只绘制100 * 100平方。当我使用下面的命令时
。为什么我的裁剪图像周围会出现白色空间?
如何避免它并确保我得到准确的100 * 100图像?

I have an image and i want to plot only 100*100 square with left hand bottom corner at 0,0. when i use below commands. Why do i get a white space around my cropped image? how can i avoid it and ensure that I get exact 100*100 image?

如果你想重复我的例子,你可以在线使用任何图像1(假设图像大于100 * 100像素)

If you want to repeat my example, you can use any image on line 1 (provided that the image is bigger than 100*100 pixels)

r <- raster("C:/Users/nnnn/Desktop/geo.jpg")
vector= getValues(r)
plot(r)
r


par(mar=c(0,0,0,0))
par(oma=c(0,0,0,0))
par(mai=c(0,0,0,0))
par(omi=c(0,0,0,0))
plot(r,xlim=c(0,100),ylim=c(0,100),legend=FALSE,axes=FALSE)

推荐答案

这是我的最佳镜头:

library(raster)

## An example raster
logo <- raster(system.file("external/rlogo.grd", package="raster")) 

## Clip out the lower-left 50*100 pixel rectangle as a new raster 'r'
cropWithRowCol <- function(r, rows, cols) {
    cc <- cellFromRowColCombine(r, rownr=rows, colnr=cols)
    crop(r, rasterFromCells(r, cc, values=FALSE))
}
r <- cropWithRowCol(logo, nrow(logo) - 49:0, 1:100)


## Extract multipliers used to appropriately size the selected device
w <- ncol(r)/max(dim(r))
h <- nrow(r)/max(dim(r))

## Set up appropriately sized device with no borders and required coordinate system    
## png("eg.png", width=480*w, height=480*h)
dev.new(width=5*w, height=5*h)
plot.new()
par(mar=c(0,0,0,0), oma=c(0,0,0,0))
plot.window(xlim=extent(r)[1:2], ylim=extent(r)[3:4], xaxs="i",yaxs="i")

## Finally, plot the (sub-)raster to it
plot(r, axes=FALSE, legend=FALSE, add=TRUE)
## dev.off()

(请记住,在交互式可调整大小的设备中,更改设备的大小会使绘制的地图的宽高比变得混乱。)

(Please remember that, in an interactively resizable device, changing the device's size will mess up the plotted map's aspect ratio.)

这篇关于R栅格在绘图时避免使用空白区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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