0

I have the latitudes and longitudes to create a sphere in R. What I need to do is color the sphere according to corresponding amplitudes. It is 3 columns in a excel file: lat, long and amplitude. So each row is a data point. What I get out is a uniform pattern - It cannot be reading the values from the amplitude column.

test$x<-0.5*cos(test$lat)*cos(test$long)
test$y<-0.5*cos(test$lat)*sin(test$long)
test$z<-0.5*sin(test$lat)
test$color<-test$amplitude
ramp<- colorRamp(c("blue", "red"))((test$color)/max(test$color))
plot3d(x= test $x,y= test $y,z= test $z,xlim=c(-0.6,0.6), ylim=c(-0.6,0.6),zlim=c(-0.6,0.6),xlab = "X", ylab = "Y", zlab = "Z", col=test$color)

Expected result is for each x,y,z point to be colored according to the amplitude.sphere_4

Megan
  • 1
  • We don't have `test` so this is not yet a MCVE but does this answer your question? https://stackoverflow.com/questions/20549540/how-to-create-3d-matlab-style-surface-plots-in-r/20553451#20553451 (I spend a long time on that answer and it was rather fun and eventually quite satisfying.) – IRTFM Aug 22 '19 at 16:32
  • Can you provide the complete code please? – Chelmy88 Aug 22 '19 at 17:12

1 Answers1

0

You computed ramp, but didn't use it. The colorRamp function returns a matrix of RGB values, which can't be used as a color in rgl; you need to use the rgb() function to convert to colors. For example, with some fake data:

test <- data.frame(lat = runif(2000, -89, 89)*pi/180, 
                   long = runif(2000, 0, 359)*pi/180 )
test$x <- 0.5*cos(test$lat)*cos(test$long)
test$y <- 0.5*cos(test$lat)*sin(test$long)
test$z <- 0.5*sin(test$lat)
test$amplitude <- test$z
test$color <- abs(test$amplitude)
ramp <- colorRamp(c("blue", "red"))((test$color)/max(test$color))
ramp <- rgb(ramp, maxColorValue = 255)
library(rgl)
plot3d(x=test$x, y=test$y, z=test$z, xlab = "X", ylab = "Y", zlab = "Z", col=ramp)

This produces the following plot:

screenshot

user2554330
  • 37,248
  • 4
  • 43
  • 90