首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >光线着色器:使用ggplot2绘制感兴趣的坐标不起作用

光线着色器:使用ggplot2绘制感兴趣的坐标不起作用
EN

Stack Overflow用户
提问于 2020-07-07 22:45:50
回答 1查看 231关注 0票数 0

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

如果我在rayshader示例中设置dem高程,则可以:

代码语言:javascript
复制
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)

但如果我创建一些随机点,并尝试将其放入图中:

代码语言:javascript
复制
#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轴上的原始图像系统坐标?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-07 23:24:06

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

我在下面的代码中提到了这两点:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62777892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档