Creating Sized Circles Using R: A Step-by-Step Guide for Interactive Maps with Circle Sizes

Plotting Sized Circles Using R: A Step-by-Step Guide

Introduction

R is a popular programming language for statistical computing and graphics. It provides an efficient way to create high-quality visualizations, including plots of circles with varying sizes based on specific data points. In this article, we will explore how to achieve this using the ggplot2 library in R.

Background

The question provided at Stack Overflow presents a scenario where a user wants to visualize data points as sized circles in R, similar to what can be achieved in Tableau. The goal is to create an interactive map with markers representing individual data points, each associated with its respective size based on the total sales value.

Prerequisites

To follow along with this tutorial, you will need:

  • R installed on your system
  • A recent version of ggplot2 library (available through CRAN or the package manager within RStudio)
  • Familiarity with basic R concepts and data manipulation

Data Preparation

Before creating the visualization, we need to prepare our data. We’ll assume that you have a CSV file containing your dataset, which can be loaded into R as follows:

# Load necessary libraries
library(ggplot2)
library(readr)

# Load the dataset from the CSV file
dat <- read_csv("your_dataset.csv")

Creating the Map

To create an interactive map with markers representing individual data points, we’ll use the get_map function to obtain a satellite image of the area. We can then superimpose our data points on this map using ggmap.

# Calculate the mean longitude and latitude
lat <- dat$Latitude
lon <- dat$Longitude

# Create a new dataframe with additional columns (optional)
df <- as.data.frame(cbind(lon,lat))

# Obtain a satellite image of the area
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4,
                      maptype = "satellite", scale = 2)

# Plot the data points on the map
ggmap(mapgilbert) +
  geom_point(data = dat, aes(x = Longitude, y = Latitude, fill = factor(Stores) , alpha = 0.8, size = Total.Sales), shape = 21) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE)

Customizing Circle Sizes

The size aesthetic in the geom_point layer allows us to control the radius of each circle based on its corresponding value. By using the factor function and setting the fill variable as “Stores”, we can create a legend that corresponds to different store types.

# Create a new dataframe with additional columns (optional)
df <- as.data.frame(cbind(lon,lat))

# Plot the data points on the map, customizing circle sizes by factor(Stores)
ggmap(mapgilbert) +
  geom_point(data = dat, aes(x = Longitude, y = Latitude, fill = factor(Stores) , alpha = 0.8, size = Total.Sales), shape = 21) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE)

Conclusion

In this article, we’ve explored how to create a map with sized circles in R using the ggplot2 library. By following these steps, you can visualize your data points as interactive markers on an underlying satellite image, providing valuable insights into their spatial relationships and sizes.

Remember that customizing your plot’s appearance is just the beginning of what’s possible with ggplot2 and R. Be sure to explore other functions and aesthetics in this library to expand your visualization capabilities!


Last modified on 2023-10-16