Creating Colored and Shaped Points on a Map with Black Borders Using ggplot2 in R

Understanding the Problem: Creating Colored and Shaped Points on a Map with Black Borders

As a data visualization enthusiast, you want to create a map that showcases points colored based on variables in your dataset and shaped according to another variable. However, when these shapes overlap, you need to have black borders around them for better visibility. In this article, we’ll delve into the world of ggplot2 and explore how to achieve this by leveraging the geom_point function along with other geometric elements.

Introduction to ggplot2

For those unfamiliar with ggplot2, it’s a powerful data visualization library in R that provides an elegant grammar-based approach for creating complex, beautiful, and informative plots. Its syntax is inspired by the concept of object-oriented programming and focuses on defining the structure of your plot through layers.

In this article, we’ll explore how to create a map with colored and shaped points while maintaining black borders when necessary.

Setting Up Your Map

To begin, you need to load the required packages. The ggplot2 package is already included in most R environments, but if not, you can install it using:

install.packages("ggplot2")

Now that we have our environment set up, let’s define a simple example dataset (df) and map data (states). We’ll use these to create our desired visualization.

Creating Your Dataset

Let’s first define a sample dataset df with the required variables. This dataset will serve as the basis for our plot:

# Load necessary libraries
library(ggplot2)

# Create your dataset (lon, lat, type, mean)
df <- data.frame(
  lon = c(-95.350278, -95.257593, -104.987625, -104.998113),
  lat = c(29.752778, 29.733726, 39.751184, 39.704005),
  type = c("aa", "bb", "aa", "bb"),
  mean = c(5.369, 3.476, 7.857, 2.356)
)

# For the map, we'll use a simple US map
states <- map_data("state")

Creating Your Map

Now that our dataset is defined, let’s create the map using ggplot2:

# Create your map with colored and shaped points
mymap <- ggplot(data = states) +
  geom_polygon(aes(x = long, y = lat, group = group), fill = NA, color = "black") +
  coord_fixed(1.3) +
  guides(fill = FALSE) +
  geom_point(data=df, aes(x=lon, y=lat, color=mean, shape=type), size=5, alpha=0.8) +
  scale_colour_gradient(low = "royalblue4", high = "red") +
  theme_bw() +
  theme(legend.position = "right",
        axis.text = element_blank(), axis.line = element_blank(),
        axis.ticks = element_blank(), panel.border = element_blank(),
        panel.grid = element_blank(), axis.title = element_blank(),
        plot.title = element_text(hjust=0.5, size=14, face="bold"))

Understanding the Solution

The solution to your question lies in using fill to color your shapes and then setting color as black to create borders around them.

# Use "fill" for coloring and set "color" as black
mymap <- ggplot(data = states) +
  geom_polygon(aes(x = long, y = lat, group = group), fill = NA, color = "black") +
  coord_fixed(1.3) +
  geom_point(data=df, aes(x = lon, y = lat, fill = mean, shape = type), 
            color = "black", size=5, stroke = 2) +
  scale_fill_gradient(low = "royalblue4", high = "red") +
  scale_shape_manual(values = c(24,21)) +
  theme_bw() +
  theme(legend.position = "right",
        axis.text = element_blank(), axis.line = element_blank(),
        axis.ticks = element_blank(), panel.border = element_blank(),
        panel.grid = element_blank(), axis.title = element_blank(),
        plot.title = element_text(hjust=0.5, size=14, face="bold"))

In the modified code above:

  • We added fill to your points, which fills the shape according to mean.
  • We set color as black ("black"), making sure that there’s a border around each point even if they overlap.
  • The rest of the code remains the same.

This solution will create a beautiful map with colored and shaped points while maintaining black borders when necessary.


Last modified on 2025-02-23