R bitmap avoids white space when plotting

I have an image, and I want to build only 100 * 100 square with the bottom left corner at 0.0. when i use below command. Why am I getting a space around my cropped image? how can i avoid this and make sure i get the exact 100 * 100 images?

If you want to repeat my example, you can use any image in line 1 (provided that the image is larger 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) 

enter image description here

+4
source share
3 answers

Here is my best shot:

 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, a resizing device will spoil the constructed map format.)

enter image description here

+1
source

Aspect ratios are usually supported for maps. You can use the width / height when printing to a file. You can resize the standard device manually, but you can also do this:

 library(raster) r <- raster(nrow=240, ncol=320) values(r) <- 1:ncell(r) dev.new(height=0.91*nrow(r)/50, width=1.09*ncol(r)/50) plot(r, legend=FALSE) 
0
source

Change aspect ratio:

 plot(r, asp=1) 
0
source

Source: https://habr.com/ru/post/1397079/


All Articles