问题
I am trying to create a mesh3D plot in plotlyR using R studio but I am having problems with how to create a colour scale to my mesh.
I have a nice mesh plot using the code,
zmean <- apply(faces, MARGIN=1, function(row){mean(points[row,3])})
facecolor = colour_ramp(
brewer_pal(palette="RdBu")(9)
)(rescale(x=zmean))
(the above I found from, a website just to make it plot, but it is not the mesh colour scale I want)http://moderndata.plot.ly/trisurf-plots-in-r-using-plotly/http://moderndata.plot.ly/trisurf-plots-in-r-using-plotly/
p <- plot_ly(
x = points[, 1], y = points[, 2], z = points[, 3],
i = faces[, 1]-1, j = faces[, 2]-1, k = faces[, 3]-1,
type = "mesh3d",
facecolor = "red",
)
p
I'm not very sure what face colour does, intensity does or colour scale does, but I want to map a different colour scale onto the mesh based on a value called bi, that I have measured for each point in the xyz-array (ie, each x = points[, 1], y = points[, 2], z = points[, 3] point has a value of bi assigned to it in my dataframe). And I want to scale the colour where
vert$colour[vert$bi<= 0.5] <- "red"
vert$colour[vert$bi> 0.5 & vert$bi<=1.5] <- "green"
vert$colour[vert$bi>= 1.5] <- "purple"
or similar in a blending contour fashion.
Any thoughts on how to plot this?
here is the plot if that helps
https://plot.ly/~neilsrini/1377/points-2-vs-points-1/
回答1:
For those who are interrsted here is the solution
p1 <- plot_ly(
x = points[, 1], y = points[, 2], z = points[, 3],
i = faces[, 1]-1, j = faces[, 2]-1, k = faces[, 3]-1,
type = "mesh3d",
facecolor = face$colour
)
The above was the code to plot. face$colour was made as follows.
Using the below function to convert each face into a mean value of the measured vector bi
zmean <- apply(faces, MARGIN=1, function(row){mean(bipts[row,1])})
then transforming the value of zmean into a colour
face$colour[zmean<= 0.5] <- "#800026" etc etc
来源:https://stackoverflow.com/questions/39156281/mesh3d-how-to-colour-the-faces-of-the-triangles-r-plotly