11

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)

enter image description here

user2543622
  • 5,760
  • 25
  • 91
  • 159

2 Answers2

5

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)
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
  • is there any way to save the new image (cropped)? – user2543622 Jan 20 '15 at 22:10
  • Sure -- do `png(filename = '~/x.png'); plot(r,xlim=c(0,200),ylim=c(0,200)); dev.off()`. You can also other commands like `pdf()` or `jpeg()` to produce image in different formats. – Marat Talipov Jan 20 '15 at 22:22
  • one more question: i am planning to get values using getValues() function. getValues(r) works fine. But i want to get values only for cropped image. How can i use getValues function in conjuction with xlim and ylim parameters? – user2543622 Jan 21 '15 at 21:19
  • I can always save a cropped image and then apply getValues function on it . But i feel that there must be an easier method – user2543622 Jan 21 '15 at 21:33
  • There is getValuesBlock. And you do no have to 'save' a cropped image, as you can do getValues(crop(x, e)) – Robert Hijmans Mar 02 '15 at 20:23
4

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)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • When i am using png command what parameters should i set to avoid any background? in your example cropped image is going to be 200*300 (as per extent command) then in png command why do you have width=450, height=275? – user2543622 Apr 23 '15 at 04:10
  • also when i plot my original image bottom left hand corner is 0,0. But when i draw extend why does it appear as a horizontal bar at top part of the image? shouldn't it be vertical bar situate in leftmost part of the image? – user2543622 Apr 23 '15 at 04:13
  • I do not know what you mean by "avoid any background". I change the width/height to accommodate the legend. I do not know what you mean by horizontal and vertical bar (I do not see either). – Robert Hijmans Apr 23 '15 at 04:38