plot3d() - How to change z axis surface color to heat map color

馋奶兔 提交于 2020-01-06 02:47:08

问题


In the above image the surface colored in z axis (like heat map). I am using plot3d()

plot3d(data$x, data$y, data$z, name = 'Plotly3D graph', type = 'l', axes=F)

I have to repeat the same color as of in the image. By using above code I can get the 3D square but I dont know where to set the color of z axis as same as the image. Please help me in plot3D. If full code is needed will post if required.

Here is sample code:

data1 <- read.csv(file.choose(),1)

# retrieve age column from csv file 
var1 <- data1$age

z1 <- rep(1, times=length(var1))

plot3d(var1, length(var1), z1, type="l", xaxt='n', yaxt='n', xlab="Jitter in 
ns", ylab="Counts", size=0.05,expand=0.75, col=color[zcol],
ticktype="detailed", zlab="")

回答1:


Here's one version, using a function from one of the answers to How do I generate a mapping from numbers to colors in R?.

# A function based on Dave X's answer to the colour mapping question
map2color <- function(x, pal, limits = range(x)){
  pal[findInterval(x, seq(limits[1], limits[2], length.out = length(pal) + 1), 
                   all.inside=TRUE)]
}

persp3d(volcano, col = map2color(volcano, rainbow(100)))

This produces this image:

To produce solid edges ("curtains" in plot3D), just surround the data with extra rows and columns of its minimum value. For example,

m <- min(volcano)
volcano2 <- cbind(m, rbind(m, volcano, m), m)

To make the edges look flat, you need to add x and y values, just a tiny bit outside the original ones:

x <- c(0.9999, 1:nrow(volcano), nrow(volcano) + 0.0001)
y <- c(0.9999, 1:ncol(volcano), ncol(volcano) + 0.0001)
persp3d(x, y, volcano2, col = map2color(volcano2, heat.colors(100)))

I switched the palette to heat.colors just for some variety.



来源:https://stackoverflow.com/questions/50079316/plot3d-how-to-change-z-axis-surface-color-to-heat-map-color

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!