rayshader:使用 ggplot2 绘制感兴趣的坐标不起作用
rayshader: Plot coordinates of interest using ggplot2 doesn't work
我喜欢使用 rayshader
包绘制 3D 图并放置一些感兴趣的坐标,但它不起作用。
如果我在 rayshader
示例中进行 dem 提升,就可以了:
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)
但是如果我创建一些随机点并尝试将其放入绘图中:
#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)
#
没用!另一个问题,我相信这是解决方案的一部分,是否可以在 X 和 Y 轴上显示原始图像系统坐标?
您的 elmat %>% reshape2::melt() %>% ggplot()
模式导致绘图输入的 Var1
和 Var2
列是行号和列号而不是坐标。此外,您的 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
我喜欢使用 rayshader
包绘制 3D 图并放置一些感兴趣的坐标,但它不起作用。
如果我在 rayshader
示例中进行 dem 提升,就可以了:
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)
但是如果我创建一些随机点并尝试将其放入绘图中:
#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)
#
没用!另一个问题,我相信这是解决方案的一部分,是否可以在 X 和 Y 轴上显示原始图像系统坐标?
您的 elmat %>% reshape2::melt() %>% ggplot()
模式导致绘图输入的 Var1
和 Var2
列是行号和列号而不是坐标。此外,您的 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