rggplot2plot3drayshader

rayshader: Plot coordinates of interest using ggplot2 doesn't work


I like to plot a 3D plot using rayshader package and put some coordinates of interest but it doesn't work.

If I make the dem elevation in rayshader example, it's OK:

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)

img1

But if I create some random point and try to put inside the plot:

#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

It doesn't work! Another question, and I believe that's part of the solution, is it possible to show the original image system coordinates in the X and Y axis?


Solution

  • Your elmat %>% reshape2::melt() %>% ggplot() pattern causes the Var1 and Var2 columns of the plot input to be row and column numbers instead of coordinates. Also, your samplelocs were sampling values instead of cells from the localtif objects it seems.

    I adressed these two points in the code below:

    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