Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:08:55+00:00 2026-05-30T05:08:55+00:00

Is there a way to ensure that the box around a plot matches the

  • 0

Is there a way to ensure that the box around a plot matches the raster extents exactly? In the following there is a gap above and below or to the left and right of the raster depending on the device proportions:

require(raster)
r = raster()
r[]= 1
plot(r, xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))

One element of the problem with raster objects is that asp=1 to ensure proper display. The following basic scatterplot has the same issue when asp=1:

plot(c(1:10), c(1:10), asp=1)

Try vectorplot(r) from the rasterVis package to see what I want the axes to look like.

EDIT:

Solutions need to play nice with SpatialPoints overlays, not showing points outside the specified raster limits:

require(raster)
require(maptools)

# Raster
r = raster()
r[]= 1

# Spatial points
x = c(-100, 0, 100)
y = c(100, 0, 100)
points = SpatialPoints(data.frame(x,y))

plot(r, xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))
plot(points, add=T)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:08:56+00:00Added an answer on May 30, 2026 at 5:08 am

    You’d probably do best to go with one of the lattice-based functions for plotting spatial raster objects provided by the raster and rasterVis packages. You discovered one of them in vectorplot(), but spplot() or levelplot() better match your needs in this case.

    (The base graphics-based plot() method for "RasterLayer" objects just doesn’t allow any easy way for you to set axes with the appropriate aspect ratio. For anyone interested, I go into more detail about why that’s so in a section at the bottom of the post.)

    As an example of the kind of plot that levelplot() produces:

    require(raster)
    require(rasterVis)
    
    ## Create a raster and a SpatialPoints object.
    r <- raster()
    r[] <- 1:ncell(r)
    SP <- spsample(Spatial(bbox=bbox(r)), 10, type="random")
    
    ## Then plot them    
    levelplot(r, col.regions = rev(terrain.colors(255)), cuts=254, margin=FALSE) +
    layer(sp.points(SP, col = "red"))
    
    ## Or use this, which produces the same plot.
    # spplot(r, scales = list(draw=TRUE), 
    #        col.regions = rev(terrain.colors(255)), cuts=254) +
    # layer(sp.points(SP, col = "red"))
    

    enter image description here

    Either of these methods may still plot some portion of the symbol representing points that fall just outside of the plotted raster. If you want to avoid that possibility, you can just subset your SpatialPoints object to remove any points falling outside of the raster. Here’s a simple function that’ll do that for you:

    ## A function to test whether points fall within a raster's extent
    inExtent <- function(SP_obj, r_obj) {
        crds <- SP_obj@coord
        ext  <- extent(r_obj)
        crds[,1] >= ext@xmin  &  crds[,1] <= ext@xmax &
        crds[,2] >= ext@ymin  &  crds[,2] <= ext@ymax
    }
    ## Remove any points in SP that don't fall within the extent of the raster 'r'
    SP <- SP[inExtent(SP, r), ]
    

    Additional weedy detail about why it’s hard to make plot(r) produce snugly fitting axes

    When plot is called on an object of type raster, the raster data is (ultimately) plotted using either rasterImage() or image(). Which path is followed depends on: (a) the type of device being plotted to; and (b) the value of the useRaster argument in the original plot() call.

    In either case, the plotting region is set up in a way which produces axes that fill the plotting region, rather than in a way that gives them the appropriate aspect ratio.

    Below, I show the chain of functions that’s called on the way to this step, as well as the call that ultimately sets up the plotting region. In both cases, there appears to be no simple way to alter both the extent and the aspect ratio of the axes that are plotted.

    • useRaster=TRUE

      ## Chain of functions dispatched by `plot(r, useRaster=TRUE)`
      getMethod("plot", c("RasterLayer", "missing"))
      raster:::.plotraster2
      raster:::.rasterImagePlot
      
      ## Call within .rasterImagePlot() that sets up the plotting region
      plot(NA, NA, xlim = e[1:2], ylim = e[3:4], type = "n",
                 , xaxs = "i", yaxs = "i", asp = asp, ...)
      
      ## Example showing why the above call produces the 'wrong' y-axis limits
      plot(c(-180,180), c(-90,90), 
           xlim = c(-180,180), ylim = c(-90,90), pch = 16,
           asp = 1,
           main = "plot(r, useRaster=TRUE) -> \nincorrect y-axis limits")
      
    • useRaster=FALSE

      ## Chain of functions dispatched by `plot(r, useRaster=FALSE)`
      getMethod("plot", c("RasterLayer", "missing"))
      raster:::.plotraster2
      raster:::.imageplot
      image.default
      
      ## Call within image.default() that sets up the plotting region
      plot(NA, NA, xlim = xlim, ylim = ylim, type = "n", xaxs = xaxs, 
           yaxs = yaxs, xlab = xlab, ylab = ylab, ...)
      
      ## Example showing that the above call produces the wrong aspect ratio
      plot(c(-180,180), c(-90,90), 
           xlim = c(-180,180), ylim = c(-90,90), pch = 16,
           main = "plot(r,useRaster=FALSE) -> \nincorrect aspect ratio")
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to ensure that a class posts a particular NSNotification? (I
In the 1.6 API, is there a way to ensure that the onStart() method
Is there a way to ensure that all WM_KEYDOWN events find their way into
When writing a print stylesheet, is there a way to ensure that an image
Is there any way to ensure that I can fit X numbers of characters
Is there a way to ensure that when a user views my Flex app,
Is there any way to ensure that when an object is deleted, it is
Is there a way to ensure that the serialization of an object is done
In a multi threaded application, is there a way to ensure that a Critical
Is there a way to ensure that when a trigger fire time arrived it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.