Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

persp(): what is wrong with my color?

Tags:

plot

r

raster

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

like image 965
Alex Avatar asked Jan 25 '26 09:01

Alex


2 Answers

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

like image 158
Zheyuan Li Avatar answered Jan 27 '26 00:01

Zheyuan Li


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

like image 35
cuttlefish44 Avatar answered Jan 26 '26 23:01

cuttlefish44



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!