# [R] Arrow and text on a 3d plot

Robin Hankin r.hankin at soc.soton.ac.uk
Thu Dec 2 12:53:40 CET 2004

```Hi

the best way to do it is to make a function like the trans3d() function
in help(persp)
and use it in your own function.  trans3d()
translates from (x,y,z) coords to plottable positions.  The following
works for me:

xyz <- matrix(rnorm(60),20,3)
xyz1 <- xyz+rnorm(60)/10

p3dpairs(xyz,xyz1,col="red",theta=40,r=1e9)

[ignore the warnings]

with

"p3dpairs" <-
function(x,x1, xlim=NULL,ylim=NULL,zlim=NULL,col=par("col"),
pch=par("pch"), cex=par("cex"), ...){
if(is.matrix(x)){
z <- x[,3]
y <- x[,2]
x <- x[,1]
}

if(is.matrix(x1)){
z1 <- x1[,3]
y1 <- x1[,2]
x1 <- x1[,1]
}

if(missing(zlim)) {
z.grid <- matrix(range(z),2,2)
} else {
z.grid <- matrix(zlim,2,2)
}

if(missing(xlim)){ xlim <- range(x) }
if(missing(ylim)){ ylim <- range(y) }

persp(xlim, ylim, z.grid, col = NA, border=NA, ...) -> res

trans3d <- function(x,y,z, pmat) {
tr <- cbind(x,y,z,1) %*% pmat
list(x = tr[,1]/tr[,4], y= tr[,2]/tr[,4])
}

out <- trans3d(x,y,z,pm=res)
out1 <- trans3d(x1,y1,z1,pm=res)
points(out, col=col, pch=pch, cex=cex, ...)

for(i in 1:length(out\$x)){
lines(c(out\$x[i],out1\$x[i]),c(out\$y[i],out1\$y[i]), col="gray", ...)
}
return(invisible(out))
}

On Dec 2, 2004, at 11:14 am, Arturo T. De Zan wrote:

> I am currently working on surface plots (with R 2.0.1) and I want to
> improve the aspect of them. Having a typical 'cow-boy hat' surface
> [source: Spector (1994), p. 206]:
>
> pts <- seq(from = -8, to = 8, length = 50)
> cow <- function(x, y)
> {
> z <- sqrt(x^2+y^2)
> sin(z)/z
> }
> out <- outer(pts, pts, cow)
> sur <- persp(x=pts, y=pts, z=out, theta=45, phi=30)
>
> What I exactly wanted is:
>
> a) how to add an arrow pointing the maximum point in the surface, for
> example: a short arrow starting on the maximum point, with slope 45Âº
> and finishing near the top-edge of the plot.
>
> b) how to add a typical text at the end of the arrow, as "this is the
> maximum point", as described in the above reference.
>
> N.B.: the author used the S function "perspp", but R does not have it.
>
> Could anyone help me?
>
> Thank you very much in advance.
>
> Arturo De Zan
> PhD candidate
> UPC-Universitat Politecnica de Catalunya
> Barcelona, Spain.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help