I am using raster function as shown on lines below. My last line produces some output. That output has a line that says dimensions : 240, 320, 76800 (nrow, ncol, ncell)
. I would like reprint that image but say only first 200 rows and first 300 columns. How can I do that? The second last line below plots the entire image
f <- "pictures/image1-1421787394.jpeg"
f
r <- raster(f)
plot(r);
r
=============================update1
I did png(filename = '~/x.png');par(mar=rep(0, 4), xpd = TRUE, oma=rep(0, 4),bty='n') ; plot(r,xlim=c(0,200),ylim=c(0,200),legend=FALSE,axes=FALSE); dev.off()
to save the cropped image. I was able to get rid of the legend and axes and the black box. But the problem is that the saved image contains much more than cropped part - for example white part around the image. I want to save only the cropped part of the original (keep image size 200*200 pixels). Please let me know how to do that?
Moreover, how could i add a red square that corresponds to the above cropped part to the original image? I mean I would like to get a red square (only edges) on the top of the original image and then save this (original image+square) as a new image.
How could i do that?
update2++++++++++++++++++++++++++++++++++++++++++++++++
adding repeatable example to show what i mean by white background
the last line below plots cropped image. I want that image to be 100*100 as my xlim and ylim are 100. But i see a white background as shown in the example below. (you cannot see the background. But if you run the code on your machine and open the image, you will see it)
library(raster)
r <- raster(nrow=240, ncol=320)
values(r) <- 1:ncell(r)
plot(r)
plot(r,xlim=c(0,100),ylim=c(0,100),legend=FALSE,axes=FALSE,frame.plot=F)
You can do it by setting xlim
and ylim
:
plot(r,xlim=c(0,299),ylim=c(0,199))
[UPDATE]
To get rid of the white background, you can try useRaster=F
parameter:
plot(r,xlim=c(0,100),ylim=c(0,100),legend=FALSE,axes=FALSE,frame.plot=F,useRaster=F)
If understand your question well, you have a RasterLayer r with dim(r) of c(240, 320, 1) and you want to crop that to the first 200 rows and 300 columns and then plot that without white space.
Always provide example data. In this case that is easy to do.
library(raster)
r <- raster(nrow=240, ncol=320)
values(r) <- 1:ncell(r)
There are different ways to crop by rows/columns. For example you can create an extent object and use that.
e <- extent(r, 1, 200, 1, 300)
rc <- crop(r, e)
Another way (for smaller sized rasters) would be to use indexing and drop=FALSE
rc <- r[1:200, 1:300, drop=FALSE]
To make a map you can use 'plot' or 'image'. Perhaps image is more of your liking (less white space, but no legend)
image(rc)
with plot you can set the size of the device before plotting.
dev.new(height=nrow(r), width=ncol(r))
plot(rc, legend=FALSE)
You can also plot to a file such as png to avoid whitespace; depending on how you set your 'par'ameters such as mai
png('test.png', width=450, height=275)
plot(rc)
dev.off()
Other ways to deal with this include using spplot, or levelplot in the rasterVis package
spplot(rc)
library(rasterVis)
levelplot(rc)
To get the red rectangle on the original image
plot(r)
plot(e, add=TRUE, col='red', lwd=2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With