Side by Side Maps with tmap in Shiny
=====================================================
In this article, we will explore how to create side-by-side maps using the tmap package in R and Shiny. We will dive into the code, explain each step in detail, and provide examples along the way.
Introduction
The tmap package is a powerful tool for creating thematic maps in R. It provides an easy-to-use interface for plotting maps with various overlays such as borders, shapes, and text labels. In this article, we will use Shiny to create a user interface that allows users to select two variables from their data and plot side-by-side maps.
Prerequisites
To follow along with this article, you will need:
- R 3.6 or later
- Shiny 1.4 or later
- The
tmappackage installed in your R environment - A basic understanding of R and Shiny programming
Data Preparation
Before we begin, let’s assume that we have a data frame called turcov with the following structure:
| NAME_1 | GID_0 | NAME_0 | GID_1 | VARNAME_1 | NL_NAME_1 | TYPE_1 | ENGTYPE_1 | CC_1 | HASC_1 | sub4 | mart1 | fark | kategori | mart2 | mart3 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Adana | TUR | Turkey | TUR.1_1 | … | … | … | … | … | … | 1000 | 200 | 10 | A | 300 | 40 |
| Istanbul | TUR | Turkey | TUR.1_2 | … | … | … | … | … | … | 1500 | 250 | 15 | B | 350 | 45 |
We will use this data to create our side-by-side maps.
Creating the Shiny App
First, we need to create a new R script and add the following code:
# Load necessary libraries
library(shiny)
library(tmap)
# Define UI for app
ui <- fluidPage(
# Add title to app
titlePanel("Side-by-Side Maps with tmap"),
# Add two columns to layout
column(6,
tmapOutput("map1", width = "100%", height = 800)),
column(6,
tmapOutput("map2", width = "100%", height = 800))
)
# Define server-side logic for app
server <- function(input, output) {
# Create breaks and colors for maps
breaks <- c(0, 20, 50, 100, Inf)
renk <- c("blue", "yellow", "orange", "red")
# Create tmap objects for each map
output$map1 <- renderTmap({
tm_shape(turcov) +
tm_fill(input$hafta[1], breaks = breaks, palette = renk,
title = "İllere göre 100 binde vaka sayıları(input$hafta[1])") +
tm_borders() +
tm_text("NAME_1")
})
output$map2 <- renderTmap({
tm_shape(turcov) +
tm_fill(input$hafta[2], breaks = breaks, palette = renk,
title = "İllere göre 100 binde vaka Sayıları(input$hafta[2])") +
tm_borders() +
tm_text("NAME_1")
})
# Arrange tmaps in a side-by-side layout
tmap_arrange(output$map1, output$map2, ncol = 2)
}
# Run app
shinyApp(ui = ui, server = server)
This code defines the user interface for our app and the server-side logic that creates the maps.
Running the App
To run the app, simply copy the above code into a new R script and execute it using RStudio or another R environment. Once the app is running, you can interact with it by selecting different variables from your data and seeing how they affect the plots.
Conclusion
In this article, we explored how to create side-by-side maps using the tmap package in R and Shiny. We defined a user interface that allows users to select two variables from their data and plot side-by-side maps. With this technique, you can easily compare different variables and overlays on your thematic maps.
We hope this article has been helpful! If you have any questions or need further clarification, please don’t hesitate to ask.
Last modified on 2024-07-09