簡體   English   中英

R 中的表面光柵 map

[英]Surface raster map in R

我正在嘗試創建夜燈的 3D 柵格 map,其值表示為高程(表面 map,尖峰代表該值)。 我已經裁剪了柵格,到目前為止,我的代碼以二維外觀投影 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()
  

更新!

我按照使用 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)

結果

我測試了 persp(raster_new),它給了我一個我想要的類似結果,但沒有 3D 運動,小而全黑(所以沒有用)。 這是我的光柵的尺寸:

persp(raster_new)

在此處輸入圖像描述

我不確定用 plotly 處理矩陣時出了什么問題。 在我的柵格信息下方

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)

基礎 R 為 3D 矩陣的可視化提供persp() ,並且plotly提供了很好的交互式 Z746AA96369ABBBA52E621BFA 功能。

這意味着您需要將 class 矩陣的 object 傳遞給這些函數。 我相信您可以將raster package 中的RasterLayer強制轉換為帶有as.matrix(my_raster)的矩陣。 使用class(my_converted_raster)檢查它是否確實是一個矩陣。

使用內置的volcano數據(已經是一個矩陣):

persp(volcano)

在此處輸入圖像描述

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

在此處輸入圖像描述

你可以做

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

persp(r)

或者如果你想與它互動

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

這會在您的計算機上打開一個 rgl 設備:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM