Creating an Interactive ggvis Choropleth Map with Tooltips in R Shiny: A Step-by-Step Guide

Introduction

In this post, we will explore how to create an interactive ggvis choropleth map with tooltips in a Shiny application. The application should allow users to switch between different datasets and update the tooltip information accordingly.

We will start by understanding the basics of ggvis and Shiny, and then move on to building the specific application.

Understanding ggvis and Shiny

ggvis is a data visualization library that provides an interactive interface for creating plots. It is built on top of the ggplot2 package and allows users to create a wide range of visualizations, including choropleth maps.

Shiny is a framework for building web applications in R. It allows users to create dynamic and interactive user interfaces using R code.

Building the Application

Our goal is to build an application that displays a choropleth map with tooltips. The map should allow users to switch between different datasets, and the tooltip information should update accordingly.

Server-Side Code

The server-side code is responsible for creating the data frame and rendering the plot.

library(rgdal)   
library(ggplot2) 
library(ggvis)

# Create sample data frames
mapdata1<-data.frame(
  state=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
  income=runif(50,min=100,max=9000))

mapdata2<-data.frame(
  state=c("alabama","alaska","arizona","arkansas","california","colorado","connecticut","delaware","florida","georgia","hawaii","idaho","illinois","indiana","iowa","kansas","kentucky","louisiana","maine","maryland","massachusetts","michigan", "minnesota","mississippi","missouri","montana","nebraska","nevada","new hampshire","new jersey","new mexico","new york","north carolina","north dakota","ohio","oklahoma", "oregon","pennsylvania","rhode island","south carolina","south dakota","tennessee","texas","utah","vermont","virginia","washington","west virginia","wisconsin","wyoming"),
  income=runif(50,min=50,max=14000))

UI Code

The UI code is responsible for creating the user interface.

library(shiny)
library(ggvis)

shinyUI(fluidPage(
  fluidRow(
    column(3,
           wellPanel(
             selectInput("segment",
                         "Choose segment:",
                         choices = c("K 1",
                                     "K 2")
             )
           )
    ),
    column(9,
           ggvisOutput("plot")

    )
  )
))

Server-Side Code (continued)

# Create reactive function for data input
dataInput<-reactive({
  switch(input$segment,
         "K 1" = mapdata1,
         "K 2" = mapdata2)
})

# Define tooltip function
values <-function(x){
  if(is.null(x)) return(NULL)
  dat = dataInput()
  row = head(dat[dat$state %in% unique(x$state), ], 1)
  paste0("State: ", row$state,"<br />",
         "Income: ", row$income, "<br />")
}

# Create reactive function for plot
vis <-reactive({
  data<-dataInput()
  data %>%
    group_by(state) %>%
    ggvis(~long, ~lat)  %>%
    hide_axis("x") %>% 
    hide_axis("y")%>%
    add_tooltip(values,"hover")%>%
    layer_paths(fill = ~income)
})

Explanation

In the above code, we first create two sample data frames mapdata1 and mapdata2. We then define a reactive function dataInput() that switches between these two datasets based on the user’s selection.

Next, we define another reactive function values() that returns the tooltip information for a given state. This function uses the dataInput() function to get the current dataset and then finds the row corresponding to the selected state.

Finally, we create a reactive function vis() that renders the choropleth map with tooltips. This function uses the ggvis library to create the map and adds tooltips using the add_tooltip() function. The tooltip text is generated by the values() function.

Testing the Application

To test the application, we run the server-side code and then open a web browser to view the application.

We can switch between different datasets by selecting “K 1” or “K 2” from the dropdown menu. This should update the tooltip information accordingly.

Conclusion

In this post, we have explored how to create an interactive ggvis choropleth map with tooltips in a Shiny application. We have used sample data frames and defined reactive functions for data input, tooltip generation, and plot rendering.


Last modified on 2023-11-29