I have drawed a picture with persp, it's 3d map with different color, indicate different altitude. In gnuplot, the corresponding command 'splot' will generate a picture beside to indicate the relationship between color and altitude. But in R, how to draw it? I have read the manual of legend, but they are all about how to draw a legend with colored text, not a continuous varing color with corresponding number.
Duncan Murdoch
2008-Sep-05 13:09 UTC
[R] how to draw the legend about color from 3d picture
On 9/5/2008 8:06 AM, yk wrote:> I have drawed a picture with persp, it's 3d map with different color, > indicate different altitude. In gnuplot, the corresponding command > 'splot' will generate a picture beside to indicate the relationship > between color and altitude. But in R, how to draw it? I have read the > manual of legend, but they are all about how to draw a legend with > colored text, not a continuous varing color with corresponding number.I don't think there's an automatic way to do this (though probably some package provides one). If you want to write your own, take a look at example(filled.contour). The legend it draws is done by this code: plot.new() plot.window(xlim = c(0, 1), ylim = range(levels), xaxs = "i", yaxs = "i") rect(0, levels[-length(levels)], 1, levels[-1], col = col) if (missing(key.axes)) { if (axes) axis(4) } else key.axes box() (but it has done a lot of setup using layout() and par() before this). Duncan Murdoch
Look at the color.legend function in the plotrix package to see if it does what you want. You may want to use the layout function to split the graphics device into 2 sections, one for the persp plot and the other to hold the legend. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of yk > Sent: Friday, September 05, 2008 6:07 AM > To: r-help at r-project.org > Subject: [R] how to draw the legend about color from 3d picture > > I have drawed a picture with persp, it's 3d map with > different color, indicate different altitude. In gnuplot, the > corresponding command 'splot' will generate a picture beside > to indicate the relationship between color and altitude. But > in R, how to draw it? I have read the manual of legend, but > they are all about how to draw a legend with colored text, > not a continuous varing color with corresponding number. > > ______________________________________________ > R-help at r-project.org 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. >
Sebastien Guyader
2010-Jul-07 18:25 UTC
[R] how to draw the legend about color from 3d picture
It may not help the original poster, but here's a solution based on what Greg said above: # Load plotrix library(plotrix) # Create a new layout to divide the graphics in 2, the first one (displaying the persp() graph) being 4 times larger than the second one (displying the legend) layout(matrix(c(1,2),1,2,byrow=T),widths=c(4,1)) # Setup the color palette, here for exemple a 20-step heatmap from white through yellow to red colors grey.colors <- colorRampPalette( c("white", "yellow", "black") ) color <- grey.colors(20) # Run the persp() command with right definition of facets and colors zfacet <- z[-1,-1] + z[-1,-ncol(z)] + z[-nrow(z),-1] + z[-nrow(z),-ncol(z)] persp(x, y, z, col=color[cut(zfacet, nbcol)], theta=...) # Stetup and display the legend, col.labels <- c("0.0","0.2","0.4","0.6","0.8","1.0") # For z values between 0 and 1 plot(0:10,type="n",axes=FALSE,xlab="",ylab="") # Blank plot required for the legend to be added to color.legend(1,1.5,9,8,col.labels,color,gradient="y",align="rb") -- View this message in context: http://r.789695.n4.nabble.com/how-to-draw-the-legend-about-color-from-3d-picture-tp866475p2281321.html Sent from the R help mailing list archive at Nabble.com.