3

I'm trying to create a perspective plot based on data I have, in longitude/latitude format.

enter image description here

The latitude and longitude are plotting correctly but the background is oddly black. How can I change that.

persp(lga_crop_05, expand =0.5, phi = 35, col= "lightblue", ticktype = "detailed")

That's the code where I create the plot. lga_crop_05 is my rasterlayer which contains the lat/long/values for my plot which I attached above.

I would appreciate any help. Thanks

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Alex
  • 81
  • 1
  • 4
  • 2
    Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete and verifyible example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – lmo Jun 10 '16 at 15:58

2 Answers2

4

What has gone wrong?

Nothing wrong with the code; but with our eyes. Try the following code:

## a function to produce a perspective plot
## "n" controls how refined your grid is
foo <- function(n) {
  x <- seq(-1.95, 1.95, length = n)
  y <- seq(-1.95, 1.95, length = n)
  z <- outer(x, y, function(a, b) a*b^2)
  persp(x, y, z, col = "lightblue")
  }

If we have a 10 * 10 grid, the colour looks perfect.

foo(10)

nice

If we have a 100 * 100 grid, the colour still looks OK.

foo(100)

ok

Now, if we have extremely refined data, for example, on a 1000 * 1000 grid. Everything will just look like black.

 foo(1000)

oh no!

Note that you have a raster. I would suspect that your data are simply too refined. Have you checked how many cells you have in your lga_crop_05?

How to get around?

Set border = NA. Try modified function:

foo1 <- function(n) {
  x <- seq(-1.95, 1.95, length = n)
  y <- seq(-1.95, 1.95, length = n)
  z <- outer(x, y, function(a, b) a*b^2)
  persp(x, y, z, col = "lightblue", border = NA)
  }

Great

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • Is there a way to reduce the number of gridlines drawn on the perspective plot? You can do this by reducing the resolution of the data, but it would be better to be able to use all the data, but draw gridlines, say, every 10th point rather than every point. If this is not possible, another option is to reduce the linewidth of the gridlines (e.g., add `lwd=0.2` as an argument to `persp`). – eipi10 Jun 10 '16 at 16:21
  • That removes the gridlines completely, making it hard to see the topology of the surface. Is there a way to keep all the data, but draw fewer gridlines? – eipi10 Jun 10 '16 at 16:27
  • Thanks so much @ZheyuanLi – Alex Jun 10 '16 at 17:25
  • @ZheyuanLi, is there a good command to add to persp() to add lines to my plot? I have a shapefile which is an outline and I want to add it to overlay this plot. I've tried a few ways but haven't had any success. – Alex Jun 10 '16 at 17:32
  • @eipi10 if you want to see the topology, you can also use the `shade` argument – Plinth Jun 07 '21 at 15:02
4

I used Mr.Li's example data, thank you. I think this is what you want.

# I changed x and y length irregular.
x <- seq(-1.95, 1.95, length = 500)
y <- seq(-1.95, 1.95, length = 200)
z <- outer(x, y, function(a, b) a*b^2)

# make persp.object and draw it
surf <- persp(x, y, z, col = "lightblue", border = NA, theta = -30)

# draw lines parallel to x axis. seq(...) depends on your data's length(y)
for(i in seq(10, 190, length=10)) lines(trans3d(x, y[i], z[,i], pmat = surf), col = "red")
# draw lines parallel to y axis. seq(...) depends on your data's length(x)
for(i in seq(25, 475, length=10)) lines(trans3d(x[i], y, z[i,], pmat = surf), col = "blue")

plot

cuttlefish44
  • 6,586
  • 2
  • 17
  • 34