# Re: [R] Axis Title in persp() Overlaps with Axis Labels

From: Ben Bolker <bolker_at_ufl.edu>
Date: Wed 26 Jul 2006 - 07:11:22 EST

Paul Murrell <p.murrell <at> auckland.ac.nz> writes:

>
> Hi
>
> Kilian Plank wrote:
> > Good morning,
> >
> > in a 3D plot based on persp() the axis title (of dimension z) overlaps with
> > the axis labels.
> > How can the distance (between axis labels and axis title) be increased?
>

> Paul

Another way to do it: get the perspective matrix back from persp() and use trans3d() to redo essentially the same calculations that persp() does to decide where to put the label:

```x <- seq(-10, 10, length= 30)
y <- x
f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
z <- outer(x, y, f)
```

z[is.na(z)] <- 1

par(mfrow=c(2, 2))
persp(x, y, z, theta = 30, phi = 30, expand = 0.5,

col = "lightblue", ticktype="detailed")

persp(x, y, z, theta = 30, phi = 30, expand = 0.5,

```      col = "lightblue", ticktype="detailed",
zlab="\n\n\n\nz")

p1 <- persp(x, y, z, theta = 30, phi = 30, expand = 0.5,
col = "lightblue", ticktype="detailed",zlab="")

```

ranges <- t(sapply(list(x,y,z),range))
means <- rowMeans(ranges)

## label offset distance, as a fraction of the plot width labelspace <- 0.12 ## tweak this until you like the result

```xpos <- min(x)-(diff(range(x)))*labelspace
ypos <- min(y)-(diff(range(y)))*labelspace
labelbot3d <- c(xpos,ypos,min(z))
labeltop3d <- c(xpos,ypos,max(z))
labelmid3d <- c(xpos,ypos,mean(range(z)))

trans3dfun <- function(v) { trans3d(v,v,v,p1) }
labelbot2d <- trans3dfun(labelbot3d)
labelmid2d <- trans3dfun(labelmid3d)
```

labeltop2d <- trans3dfun(labeltop3d)
labelang <- 180/pi*atan2(labeltop2d\$y-labelbot2d\$y,labeltop2d\$x-labelbot2d\$x) par(xpd=NA,srt=labelang) ## disable clipping and set string rotation text(labelmid2d\$x,labelmid2d\$y,"z label")

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed Jul 26 07:15:40 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 26 Jul 2006 - 08:24:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.