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

Jose Claudio Faria joseclaudio.faria at terra.com.br
Wed Jul 26 13:10:36 CEST 2006


Dear Kilian,

Also give a looked at: 
http://wiki.r-project.org/rwiki/doku.php?id=graph_gallery:new-graphics

You will see a new and very flexible function to 3D plot.

Regards,
__
Jose Claudio Faria
Brasil/Bahia/Ilheus/UESC/DCET
Estatística Experimental/Prof. Adjunto
mails: joseclaudio.faria em terra.com.br
        jc_faria em uesc.br
        jc_faria em uol.com.br

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[1],v[2],v[3],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[1]$x,labelmid2d$y,"z label")



More information about the R-help mailing list