3
votes

I have a dataframe in which values c are specified for Cartesian coordinates (x, y). I use plot() and the result has too much space in Y-axis. Is there a way to edit that?

library(raster)
x <- c(1:60)
y <- c(2:5)
c <- rnorm(60)
Data1 <- data.frame(x, y, c)
raster1 <- rasterFromXYZ(Data1)
plot(raster1)

enter image description here

1
I'm sorry to say that this isn't easy to control. Your best option will be to play around with the size and shape of the device to which you plot the raster. See this question and its two answers -- one by me and one by the raster package's primary author -- for closely related advice and code. Or use spplot() or rasterVis::levelplot(), which are more sophisticated in their adjustment of the plot's x and y limits. (Try spplot(raster1) to see what I mean by that.) - Josh O'Brien
image() also gets the job done and that's a part of the raster package. - Badger
@Badger -- The downside there is that it loses the 1-to-1 aspect ratio that you usually want when plotting a map. And if you do image(raster1, asp=1), to get the aspect ratio right, you're right back to the same problem the OP started with... - Josh O'Brien
Finally, here is a more complete explanation of why this is so hard to accomplish, using plot(). - Josh O'Brien

1 Answers

-1
votes

While not a fix, it helps to note that in plot(), xlim and ylim end up providing the max and min.

Obviously, it's handled differently in the raster library. When I use plot(raster1, ylim=c(2,5)), the axis isn't adjusted at all. However, if you use plot(raster1, xlim=c(0,20)) clearly affects the x-axis.

I'm not sure why it won't affect the y-axis.