I am currently trying to create an interactive 3D surface plot using plotly in R. My dataset includes a matrix of Latitude and Longitude and I have a DEM for the region I would like to make this surface plot for. However, I am unable to do the same.
I have a dataframe Points.sp that contains a list of Lat/Long along with elevation data for areas where I have sampled.
> head(Points.sp) Points Long Lat Elevation 1 NA NA NA 2 Nair212 NA NA NA 4 CESF543 75.72130 12.88470 876.000 5 CESF1587 75.93559 11.93745 1494.523 6 tysoni_WILD15AMP650 75.82200 12.38000 1051.000 7 tysoni_SDBDU201274 75.65570 12.22010 1176.000
Here's what I have tried:
Points_new <- data.frame(Points.sp$Long, Points.sp$Lat, Points.sp$Elevation) Points_new <- as.matrix(Points_new) p <- plot_ly(z = ~Points_new) %>% add_surface() p
In the above plot, I manually loaded the elevation values for each of these points, but the problem being that I need these points on a DEM for the region, and everytime I try to load the DEM, I get an error saying - 'z' must be a numeric matrix.
The DEM here is a raster for the region and I obtained it from the SRTM dataset and cropped it to contain my region only.
elev <- raster("E:\\MasterThesis\\Absence Approach\\Data\\Elevation_Data\\alt\\alt") cr1 <- crop(elev, extent(WG.new)) #WG.new is a mask for a smaller region newelev<- mask(cr1, WG.new) > newelev class : RasterLayer dimensions : 1577, 624, 984048 (nrow, ncol, ncell) resolution : 0.008333334, 0.008333334 (x, y) extent : 72.87501, 78.07501, 8.11667, 21.25834 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs data source : in memory names : alt values : -14, 2625 (min, max) attributes : ID COUNT from: -431 2 to : 8233 1
If I try loading just the DEM alone by converting the raster to a matrix, I am able to view the DEM, but now I need to add the Lat and Long for locations from the Points.sp dataframe.
newelev <- as.matrix(newelev) p <- plot_ly(z = ~newelev) %>% add_surface() p
Any suggestions? I would ideally want points as small circles on the DEM for the region along with the DEM smoothed and stretched out.
Based on @wittich's elegant solution, I tried what he posted, but I am getting an image like the screenshot posted below.