Last updated: 2025-01-03
Checks: 7 0
Knit directory: R_tutorial/
This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20241223)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 9fccd1e. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Unstaged changes:
Modified: analysis/index.Rmd
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/Interactive_Maps.Rmd
) and
HTML (docs/Interactive_Maps.html
) files. If you’ve
configured a remote Git repository (see ?wflow_git_remote
),
click on the hyperlinks in the table below to view the files as they
were in that past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 9fccd1e | Ohm-Np | 2025-01-03 | wflow_publish("analysis/Interactive_Maps.Rmd") |
html | 24f878a | Ohm-Np | 2025-01-03 | Build site. |
Rmd | 20c6879 | Ohm-Np | 2025-01-03 | wflow_publish("analysis/Interactive_Maps.Rmd") |
Up to this point, we have created several plots for vector data, raster data, and data frames. However, R also supports interactive visualizations through powerful packages such as plotly, tmap, leaflet, mapview, among others. These tools enable the creation of dynamic and engaging visual representations of data.
In this chapter, we will explore various methods for creating visualizations in R, helping you communicate insights more effectively. To begin, we will install all the packages required for creating interactive maps in this chapter.
# set CRAN mirror
options(repos = c(CRAN = "https://cran.rstudio.com/"))
# packages are only installed when necessary, avoiding repeated output
packages <- c("plotly", "leaflet", "mapview")
installed_packages <- rownames(installed.packages())
missing_packages <- packages[!(packages %in% installed_packages)]
if(length(missing_packages)) install.packages(missing_packages, quiet = TRUE)
Creating interactive maps with plotly in R is a great way to visualize geospatial data dynamically. Below is an example of how you can create interactive maps using plotly with vector data and raster data.
The plotly
library can visualize vector data (e.g.,
polygons or points) by converting it into a sf
object and
plotting it using plot_ly
.
# Load required libraries
library(sf)
library(dplyr)
library(plotly)
# Load vector data
gadm_data <- read_sf("data/vector/kanchanpur.gpkg")
# Create an interactive map
fig <- plot_ly() %>%
add_sf(data = gadm_data,
type = "scatter",
color = ~NAME,
text = ~NAME,
hoverinfo = "text") %>%
layout(
annotations = list(
text = "Interactive Map of Kanchanpur District",
x = 0.5, # Center align
y = -0.1, # Position below the plot
showarrow = FALSE, # No arrow
xref = "paper", # Relative to the chart
yref = "paper", # Relative to the chart
font = list(size = 16) # Font size
)
)
fig
color
: Use a column in your dataset to differentiate
areas.hoverinfo
: Displays additional information when you
hover over the map.This interactive plot provides several features to explore the data dynamically. You can hover over elements on the map to view detailed information displayed as tooltips. Use the legend to toggle the visibility of specific categories or data groups. Zoom in and out using the scroll wheel on your mouse or the touchpad, and pan across the map by clicking and dragging. Additionally, double-clicking on the plot resets it to its original view. These tools make it easy to explore and analyze the data visually.
To visualize raster data interactively, you can convert it into a
data frame with terra
and then use plot_ly
to
plot it.
library(terra)
# Load raster data
r <- rast("data/raster/landcover_2019.tif")
# Convert raster to data frame
r_df <- as.data.frame(r, xy = TRUE)
colnames(r_df)
[1] "x"
[2] "y"
[3] "E080N40_PROBAV_LC100_global_v3.0.1_2019-nrt_Discrete-Classification-map_EPSG-4326"
# Rename the 3rd column
colnames(r_df)[3] <- "layer"
# Create an interactive heatmap
fig <- plot_ly(
data = r_df,
x = ~x,
y = ~y,
z = ~layer,
type = "heatmap",
colors = "viridis",
hoverinfo = "x+y+z"
) %>%
layout(title = "ESA Land Cover (2019)")
fig