R栅格在绘图时避免使用空白区域

编程入门 行业动态 更新时间:2024-10-26 17:25:05
本文介绍了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栅格在绘图时避免使用空白区域

本文发布于:2023-07-18 05:09:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栅格   空白   区域

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!