0

我喜欢使用rayshader包绘制 3D 图并放置一些感兴趣的坐标,但它不起作用。

如果我在示例中进行 dem 高程rayshader,就可以了:

library(rayshader)
library(ggplot2)
library(raster)

#Here, I load a map with the raster package.
loadzip = tempfile() 
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
unlink(loadzip)


#And convert it to a matrix:
elmat = raster_to_matrix(localtif)

# Plot elevation
ggelmat = elmat %>%
 reshape2::melt() %>%
 ggplot() +
 geom_tile(aes(x=Var1,y=Var2,fill=value)) +
 scale_x_continuous("X",expand = c(0,0)) +
 scale_y_continuous("Y",expand = c(0,0)) +
 coord_fixed()
ggelmat

# \donttest{
plot_gg(ggelmat, multicore = TRUE, raytrace = TRUE, width = 7, height = 4,
       scale = 300, windowsize = c(1400, 866), zoom = 0.6, phi = 30, theta = 30, show.legend = FALSE)
render_snapshot(clear = TRUE)

图像1

但是,如果我创建一些随机点并尝试放入情节中:

#Create some 100 random coordinates
# grab 100 cell index numbers at random
samp <- sample(localtif, 100, replace = FALSE)

# and their location coordinates
samplocs <- xyFromCell(localtif, samp)
samplocs.df<-as.data.frame(samplocs)

pts<-spsample(localtif, 100, type = 'random')
pts.df<-as.data.frame(pts)

# Plot elevation with some coordinates
ggelmat2 = elmat %>%
 reshape2::melt() %>%
 ggplot() +
 geom_point(data = samplocs.df, mapping = aes(x = x, y = y)) +
 geom_tile(aes(x=Var1,y=Var2,fill=value)) +
 scale_x_continuous("X",expand = c(0,0)) +
 scale_y_continuous("Y",expand = c(0,0)) +
 coord_fixed()
ggelmat2

# \donttest{
plot_gg(ggelmat2, multicore = TRUE, raytrace = TRUE, width = 7, height = 4,
       scale = 300, windowsize = c(1400, 866), zoom = 0.6, phi = 30, theta = 30, show.legend = FALSE)
render_snapshot(clear = TRUE)
#

img2

它不起作用!另一个问题,我相信这是解决方案的一部分,是否可以在 X 和 Y 轴上显示原始图像系统坐标?

4

1 回答 1

1

您的elmat %>% reshape2::melt() %>% ggplot()模式导致绘图输入的Var1Var2列是行号和列号而不是坐标。此外,您正在从看起来samplelocs的对象中采样值而不是单元格。localtif

我在下面的代码中解决了这两点:

library(rayshader)
#> Warning: package 'rayshader' was built under R version 4.0.2
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.2
library(raster)
#> Loading required package: sp

#Here, I load a map with the raster package.
loadzip = tempfile() 
download.file("https://tylermw.com/data/dem_01.tif.zip", loadzip)
localtif = raster::raster(unzip(loadzip, "dem_01.tif"))
#> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
#> datum Unknown based on GRS80 ellipsoid in CRS definition
unlink(loadzip)


#And convert it to a matrix:
elmat = raster_to_matrix(localtif)

samp <- sample(seq_along(localtif), 100, replace = FALSE)

# and their location coordinates
samplocs <- xyFromCell(localtif, samp)
samplocs.df<-as.data.frame(samplocs)

# Melt like this
df <- xyFromCell(localtif, seq_along(localtif))
df <- as.data.frame(df)
df$value <- as.vector(elmat)

# Plot elevation with some coordinates
ggelmat2 = df %>%
  ggplot(aes(x = x, y =y)) +
  geom_tile(aes(fill=value)) +
  geom_point(data = samplocs.df) +
  scale_x_continuous("X",expand = c(0,0)) +
  scale_y_continuous("Y",expand = c(0,0)) +
  coord_fixed()
ggelmat2

于 2020-07-07T15:24:05.853 回答