0

I have this simple code to read shp file and plot in 3D.

library(sf)
library(ggplot2)
library(ggmap)    # for fortifying shapefiles
nc <- st_read("shp/polygons.shp")
#plot(st_zm(nc), max.plot = 1)
shapefile_df <- fortify(nc)
map <- ggplot() +
  geom_path(data = shapefile_df, 
            aes(x=long, y=lat, group = group),
            color = 'gray', fill = 'white', size = .2)

print(map) 

but I am getting an error

    Error in `geom_path()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error in `FUN()`:
! object 'long' not found
Run `rlang::last_error()` to see where the error occurred.

Here is the nc object

enter image description here

user1298426
  • 3,467
  • 15
  • 50
  • 96
  • 1
    You are referring to `long` in the `aes()` call. Look at `names(shapefile_df)` to see what names are available, because apparently `long` is not. – user2554330 Dec 17 '22 at 10:13
  • 1
    You can plot sf objects directly in ggplot. Just use `ggplot(nc) + geom_sf()` – Allan Cameron Dec 17 '22 at 10:28
  • @AllanCameron this does print the shp file but in 2d. I am looking for 3d visualization based on z coordinate of a polygon. Is it possible? – user1298426 Dec 17 '22 at 11:49
  • 2
    @user1298426 ggplot is a 2D plotting library. You can color the polygons according to their z variable, but if you want a true 3d map then you will need to look elsewhere, for example the rayshader package – Allan Cameron Dec 17 '22 at 11:54
  • another option is using [mapdeck to show elevation](https://stackoverflow.com/a/60049642/4002530) – tospig Dec 18 '22 at 00:02

0 Answers0