Cobin
Cobin

Reputation: 930

Why the box is so broad when plotting raster in R?

I want to plot the raster ,using code as follows:

library(raster)
tmp <- scan(what='') 
'D:\temp\m2001EOS.tif'

ra<-raster(tmp)
plot(ra)

ra attribution as follows:

class       : RasterLayer 
dimensions  : 1941, 1832, 3555912  (nrow, ncol, ncell)
resolution  : 981.8572, 981.8572  (x, y)
extent      : 4723620, 6522382, 4203136, 6108921  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=aea +lat_1=25 +lat_2=47 +lat_0=0 +lon_0=105 +x_0=4000000 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : D:\temp\m2001EOS.tif 
names       : m2001EOS 
values      : -32768, 32767  (min, max)

enter image description here

The plot is so wide that is not match xmin and xmax, but the range of height seem reasonable to ymin and ymax. Which parameter causes different patterns? And how to adjust the box outline? The example file is m2001EOS.tif

Upvotes: 1

Views: 1117

Answers (1)

dww
dww

Reputation: 31452

Using the volcano data set, we can get a simple reproducible example which does not require downloading data from a remote link:

library(raster)
plot(raster(volcano))

enter image description here

When plotting rasters (using raster::plot):

  1. The shape of the bounding box is determined by the aspect ratio (shape) of the device window in which it is plotted.
  2. The scale size is forced to be the same in x and y directions, as this is standard practice for spatial data (because we don't want to distort the shape of spatial data by stretchng it out to fit the box). We can see this in the example plot above by the fact that the raster is exactly square.

Given these two constraints, the need to fill the bounding box with blank areas follows as a direct consequence.

So how to avoid this? One way is to simply change the height to width ratio of the plot window or output file.

pdf(height = 4, width = 4)
plot(raster(volcano))
dev.off()

enter image description here

Even better is to use rasterVis::levelplot for nicer plotting:

library(rasterVis)
levelplot(raster(volcano), margin = F)

enter image description here

Upvotes: 3

Related Questions