Open In App

Create a Choropleth Map by using Plotly Package in R

Last Updated : 09 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

There are plenty of packages in R that can be used to make maps, like leaflet, mapview, ggplot, spplot, plotly, etc. Each of the packages has its own advantages and disadvantages. But all of them have the common goal of making it easy to create maps and visualize geospatial data. In this article, we will learn how to create Choropleth maps using plotly package in R programming.

Choropleth Maps using Plotly Package in R

The choropleth map is a type of thematic map that displays divided regions colored or patterned based on a specific variable or data category. It is a powerful visualization tool commonly used to represent spatial data, such as population density, average income, or election results. In R, we can create choropleth maps using the Plotly package, which provides interactive and customizable visualizations.

Choropleth Map

Choropleth Map

The Plotly package in R allows us to create interactive and dynamic data visualizations, including choropleth maps. It leverages the Plotly JavaScript library to generate interactive plots that can be easily customized and shared. The package provides various functions and options to create and customize choropleth maps based on our data and requirements.

The plotly package can in installed using the following syntax:

install.packages("plotly")

Steps to Create a Choropleth Map using Plotly

Let us see the step-by-step process to create a choropleth map in R.

Step 1: Load the required packages

The first step is to load the Plotly package in the R script.

library(plotly)

Step 2: Load the dataset

Prepare or generate the data that you want to visualize on the choropleth map. The data should contain information about the regions you wish to plot and the variable of interest associated with each region.

data <- data.frame(
country = c("USA", "India", "Mexico"),
value = c(50, 20, 30)
)

Step 3: Create a choropleth Map

We can create a choropleth map in R using the plot_ly() and plot_geo() functions of the Plotly package. The plot_ly() function is used to plot a number of charts such as bar plots, line plots, scatter plots, maps, etc. The choropleth map can be generated using plot_ly() function and passing the dataset, setting the type argument to “choropleth”.

plot_ly(datadata, type = "choropleth", ...)

The plot_geo() function is specifically used to generate different types of geographical maps in R programming. The plot_geo() has the following syntax:

plot_geo(data, ...)

It takes the dataset as the parameters and other options are optional that are used to customize the map.

Step 4: Display the Map

At last, display the map. You can use the plotly::print() function or any other appropriate method. This will render the map in the plot viewer or save it as an HTML file.

Choropleth Maps in R

Now, let us see a few examples of generating Choropleth Maps in R using the Plotly Package.

Example 1: Choropleth Map using plot_ly() function

In this example, we will create a choropleth map using the plot_ly() function of the Plotly package. We created a dataset of the states of USA and provided them with some values. Then using the plot_ly() function passed the dataset as the parameter and specify the type to “choropleth”, locations to “states”, z parameter to “values” and location mode to “USA-states”.

R




# loading package
library(plotly)
  
# creating dataset
data <- data.frame(
  state = c("CA", "TX", "NY", "FL", "IL"),
  value = c(20, 50, 35, 21, 22)
)
  
# ploting choropleth map
map <- plot_ly(
  data,
  type = "choropleth",
  locations = ~state,
  z = ~value,
  locationmode = "USA-states"
)
  
# displaying map
map


Output:

Choropleth Map using plot_ly() function

Choropleth Map using plot_ly() function

Example 2: Choropleth Map using plot_geo() function

In this example, we will create a choropleth map using the plot_geo() function of the Plotly package. We created a dataset of the countries of the world and provided them with some values. Then using the plot_geo() function passed the dataset as the parameter and specify z parameter to “values”, location mode to “Country names”, locations to “country”, and provided a marker parameter to a list to set the boundary color and width to red and 0.6 respectively of the countries mentioned in the dataset.

R




# load package
library(plotly)
  
# create dataset
data <- data.frame(
  country = c("Japan", "USA", "India", "Brazil", "Iran", "Australia", "Mexico"),
  value = c(60, 70, 73, 50, 64, 52, 86)
)
  
# plot the map
map <- plot_geo(
    data = data,
    z = ~value,
    locationmode = "Country Names"
    locations = ~country,
    marker = list(line = list(color = "red", width = 0.6))
  
# display the map
map


Output:

Choropleth Map using plot_geo() function



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads