In rstudio, we can do many things related to spatial data, especially for map visualization either with 2D or 3D views. This time we will share how to display 3D maps with the help of the Rayshaders package, before that we have to prepare shp format data first.:
library(rgdal)
## Loading required package: sp
## Please note that rgdal will be retired during October 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2023/05/15/evolution4.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-7, (SVN revision (unknown))
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.6.2, released 2023/01/02
## Path to GDAL shared files: C:/Users/DELL/AppData/Local/R/win-library/4.3/rgdal/gdal
## GDAL does not use iconv for recoding strings.
## GDAL binary built with GEOS: TRUE
## Loaded PROJ runtime: Rel. 9.2.0, March 1st, 2023, [PJ_VERSION: 920]
## Path to PROJ shared files: C:/Users/DELL/AppData/Local/R/win-library/4.3/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.6-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
library(rayshader)
library(viridis)
## Loading required package: viridisLite
library(viridisLite)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(magick)
## Linking to ImageMagick 6.9.12.98
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
library(av)
library(ggplot2)
with the following script :
dataDir <- "C:/Users/DELL/Documents"
setwd(dataDir)
following script :
shp <- st_read("C://Users/DELL/Documents/Vektor/Kab_NgawiBaru.shp")
## Reading layer `Kab_NgawiBaru' from data source
## `C:\Users\DELL\Documents\Vektor\Kab_NgawiBaru.shp' using driver `ESRI Shapefile'
## Simple feature collection with 19 features and 5 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 513068.2 ymin: 9157696 xmax: 574071.9 ymax: 9199217
## Projected CRS: WGS 84 / UTM zone 49S
print(shp)
## Simple feature collection with 19 features and 5 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 513068.2 ymin: 9157696 xmax: 574071.9 ymax: 9199217
## Projected CRS: WGS 84 / UTM zone 49S
## First 10 features:
## Id Kecamatan Luas_Wil Kecamata_1 Jml_Pen geometry
## 1 0 Karanganyar 159.8370 Karanganyar 83831 POLYGON ((535754.8 9191640,...
## 2 0 Pitu 90.9051 Pitu 51078 POLYGON ((551069.2 9184529,...
## 3 0 Ngawi 71.4555 Ngawi 42827 POLYGON ((554971.4 9185929,...
## 4 0 Bringin 68.6072 Bringin 29847 POLYGON ((569188.6 9183428,...
## 5 0 Widodaren 113.7050 Widodaren 71870 POLYGON ((532530.5 9186706,...
## 6 0 Gerih 33.5129 Gerih 26730 POLYGON ((546619.5 9167243,...
## 7 0 Kwadungan 32.5183 Kwadungan 38052 POLYGON ((551738.1 9169683,...
## 8 0 Geneng 54.9854 Geneng 51699 POLYGON ((552011.1 9173573,...
## 9 0 Karangjati 71.7387 Karangjati 30874 POLYGON ((564886.9 9170199,...
## 10 0 Pangkur 30.3504 Pangkur 27593 POLYGON ((560410.6 9173405,...
because I want to display the population class, so I fill it with the Jml_Pen variable. The variable must match what is listed in the table attribute
shp = mutate(shp, Klass = Jml_Pen)
following script
ggVktor <- ggplot(data=shp)+
geom_sf(aes(fill = Klass ))+
scale_fill_viridis()+
ggtitle("Peta Jumlah Penduduk Kabupaten Ngawi (BPS 2022)")+
theme_bw()
plot_gg(ggVktor,multicore = TRUE, width = 5, height = 5, scale = 200,
windowsize=c(1280,720), zoom = 0.60, phi = 50, sunangle = 120, theta = 45)
render_snapshot()
Finished