简体   繁体   English

R 中的表面光栅 map

[英]Surface raster map in R

I am trying to create a 3D raster map of nightlights with the values represented as an elevation (surface map with spikes representing the value).我正在尝试创建夜灯的 3D 栅格 map,其值表示为高程(表面 map,尖峰代表该值)。 I already crop the raster and so far my code to project the map, in 2D looks, like below but not sure how to add the 3D or which function should I use.我已经裁剪了栅格,到目前为止,我的代码以二维外观投影 map,如下所示,但不确定如何添加 3D 或我应该使用哪个 function。

ras_df <- as.data.frame(crop_raster, xy=TRUE, na.rm=TRUE)
colnames(ras_df) <- c("x", "y", "value")
ras_df$log_value_plus1 <- log(ras_df$value+1)

fig <- ggplot() +
  geom_raster(data=ras_df, aes(x=x,y=y, fill=log_value_plus1), alpha=1) +
 
  geom_polygon(data=shapfile_fortified, aes(x=long, y=lat, group=group), 
               fill=NA, color="grey60", size=0.25) +
   coord_quickmap()
  

UPDATE!更新!

I follow the instructions suggested using plotly but I get an empty matrix.我按照使用 plotly 建议的说明进行操作,但我得到一个空矩阵。

raster_new<-raster::as.matrix(crop_raster)
class(raster_new)
#matrix
plot_ly(z = ~raster_new) %>% add_surface()

#same empty matrix using
plot_ly(z = raster_new,  type="surface",showscale=FALSE)

结果

I tested persp(raster_new) and it gives me a similar result I would like to have but with no 3D movement, small and all dark (so no useful).我测试了 persp(raster_new),它给了我一个我想要的类似结果,但没有 3D 运动,小而全黑(所以没有用)。 This are the dimensions of my raster:这是我的光栅的尺寸:

persp(raster_new)

在此处输入图像描述

I am no sure what is going wrong when the matrix is processed with plotly.我不确定用 plotly 处理矩阵时出了什么问题。 Below my raster info在我的栅格信息下方

class      : RasterLayer 
dimensions : 4352, 4959, 21581568  (nrow, ncol, ncell)
resolution : 0.004166667, 0.004166667  (x, y)
extent     : -8.672916, 11.98958, 18.96042, 37.09375  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : light_raster_2016_.tif 
names      : light_raster_ALG_2016_ 
values     : 0, 21980.4  (min, max)

Base R provides persp() for 3D visualization of matrices, and plotly offers a nice interactive 3D capabilities.基础 R 为 3D 矩阵的可视化提供persp() ,并且plotly提供了很好的交互式 Z746AA96369ABBBA52E621BFA 功能。

This means you'll need to pass an object of class matrix to these functions.这意味着您需要将 class 矩阵的 object 传递给这些函数。 I believe you can coerce a RasterLayer from the raster package into a matrix with as.matrix(my_raster) .我相信您可以将raster package 中的RasterLayer强制转换为带有as.matrix(my_raster)的矩阵。 Use class(my_converted_raster) to check that it indeed is a matrix.使用class(my_converted_raster)检查它是否确实是一个矩阵。

Using the built-in volcano data (already a matrix):使用内置的volcano数据(已经是一个矩阵):

persp(volcano)

在此处输入图像描述

library(plotly)
plot_ly(z = ~volcano) %>% add_surface()

在此处输入图像描述

You can do你可以做

library(raster)
r <- raster(volcano)
extent(r) <- c(0, 610, 0, 870)

persp(r)

Or if you want to interact with it或者如果你想与它互动

library(rasterVis)
library(rgl)
plot3D(r, col = rainbow)

Which opens an rgl device on your computer:这会在您的计算机上打开一个 rgl 设备:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM