-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from JosephBARBIERDARNAL/rayshader
Rayshader
- Loading branch information
Showing
37 changed files
with
2,218 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
|
||
**/.DS_Store | ||
.DS_Store | ||
*_cache/ | ||
*.tif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--- | ||
title: "2d maps with rayshader" | ||
descriptionMeta: "This post explains how to build and customize 2d maps with the rayshader package. It provides reproducible code and explain how input data must be formatted." | ||
descriptionTop: "This post thoroughly details the process of building and customizing **2D maps** using the [rayshader](package/rayshader.html) package. It includes step-by-step, reproducible code examples and clearly explains the required formatting for input data.<br><br>By following this guide, you will learn how to create **visually appealing maps** and tailor them to meet your specific needs, ensuring your data is presented in the most effective way possible. Additionally, the post covers various customization options within **rayshader**, enabling you to enhance your maps with different visual **styles and effects**." | ||
sectionText: "Map section" | ||
sectionLink: "map.html" | ||
DataToVizText: "Data to Viz" | ||
DataToVizLink: "data-to-viz.com/graph/map.html" | ||
url: "410-map-2d-with-rayshader" | ||
output: | ||
html_document: | ||
self_contained: false | ||
mathjax: default | ||
lib_dir: libs | ||
template: template_rgg.html | ||
css: style.css | ||
toc: TRUE | ||
toc_float: TRUE | ||
toc_depth: 2 | ||
df_print: "paged" | ||
--- | ||
|
||
|
||
```{r global options, include = FALSE} | ||
knitr::opts_chunk$set(warning=FALSE, message=FALSE, fig.align='center') | ||
``` | ||
|
||
<div class="container"> | ||
|
||
|
||
# Libraries | ||
*** | ||
|
||
The [rayshader package](package/rayshader.html) makes it **simple** to create **shaded 2D relief maps**. | ||
|
||
Since the package is on **CRAN**, you can **install it** with `install.packages("rayshader")`. | ||
|
||
We also load the **`raster` package** to **access data** to work with. | ||
|
||
```{r} | ||
# install.packages("rayshader") | ||
library(rayshader) | ||
library(raster) | ||
``` | ||
|
||
|
||
# Data format | ||
*** | ||
|
||
The [rayshader](package/rayshader.html) package requires input data in an **elevation matrix format**. This is a specific matrix where each cell holds the **elevation value** for the corresponding map point. | ||
|
||
Here's how to obtain the **elevation matrix** of the **Grand Canyon** from a raster file using the `raster` package. | ||
|
||
```{r} | ||
# Define a region for the SRTM data (example: Grand Canyon) | ||
extent_gc <- extent(-113.0, -112.0, 36.0, 37.0) | ||
srtm_gc <- getData("SRTM", lon = -112.5, lat = 36.5) | ||
srtm_gc_cropped <- crop(srtm_gc, extent_gc) | ||
# Convert the raster data to matrix | ||
elevation_matrix <- raster_to_matrix(srtm_gc_cropped) | ||
``` | ||
|
||
|
||
# Basic 2D map | ||
*** | ||
|
||
Let's see how to create a basic 2D map with [rayshader](package/rayshader.html). | ||
|
||
We use the `sphere_shade()` function to create the map. It takes the **elevation matrix** as input and returns a shaded map, and then plot it with the `plot_map()` function. | ||
|
||
```{r} | ||
elevation_matrix %>% | ||
sphere_shade(texture="desert", sunangle = 45) %>% | ||
plot_map() | ||
``` | ||
|
||
|
||
# Change texture | ||
*** | ||
|
||
[rayshader](package/rayshader.html) includes a **specialized function** to create textures: `create_texture()`. This function accepts **5 colors as input** and generates a texture suitable for use in the `sphere_shade()` function. | ||
|
||
```{r} | ||
custom_texture <- create_texture("#fff673","#55967a","#8fb28a","#55967a","#cfe0a9") | ||
elevation_matrix %>% | ||
sphere_shade(texture=custom_texture, sunangle = 45) %>% | ||
plot_map() | ||
``` | ||
|
||
# Change shade properties | ||
*** | ||
|
||
The `add_shadow()` function can be used to add a shadow to the map. The `ray_shade()` function creates a shadow based on the sun angle, while the `ambient_shade()` function creates a shadow based on the ambient light. | ||
|
||
```{r, cache=TRUE} | ||
elevation_matrix %>% | ||
sphere_shade(texture="desert", sunangle = 45, zscale = 50) %>% | ||
add_shadow(ray_shade(elevation_matrix), 0.5) %>% # Adds a ray-traced shadow | ||
add_shadow(ambient_shade(elevation_matrix), 0) %>% # Adds an ambient shadow | ||
plot_map() | ||
``` | ||
|
||
|
||
|
||
# Going further | ||
*** | ||
|
||
You might be interested in | ||
|
||
- creating [3d maps with rayshader](411-map-3d-with-rayshader.html) | ||
- learning more about [rayshader with more examples](package/rayshader.html) | ||
|
||
|
||
|
||
<!-- Close container --> | ||
</div> | ||
|
||
|
||
|
||
|
||
```{r, echo=FALSE} | ||
htmltools::includeHTML("htmlChunkRelatedMap.html") | ||
``` |
Oops, something went wrong.