1

I want to make a map that has color for each state represent an variable. At same time, I want to add a pie chart for each state while keep the color in the last step.

So far, I can make the colored map with plot_usmap

enter image description here

and pie chart on map use geom_scatterpie separately

enter image description here

I wonder if I can combine them together as one map.

I tried to combine those two function with '+', but it didn't work.

jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
Sam
  • 11
  • 1

1 Answers1

2

Sure is this possible. But you have to be aware that by default we can only have one fill scale so you have to keep an eye on the type of values (continuous vs. discrete) mapped on the fill aesthetic for the plot_usmap and geom_scatterpie part. One option to circumvent that would be to use the ggnewscale package which allows for multiple scales for the same aesthetic as I do in my example below.

Using some fake example data based on my answer on this post and the default example from plot_usmap.

library(usmap)
library(ggplot2)
library(scatterpie)
library(ggnewscale)

data <- data.frame(region= c("AL", "AR", "AZ", "CA", "IA"), 
                   gas= c(25, 45, 45, 60, 75),
                   coal= c(45, 50, 45, 20, 15), 
                   wind= c(30, 5, 10, 20, 10), 
                   solar= c(10, 10, 10, 10, 10))

states <- us_map("states")

centroids <- usmapdata::centroid_labels("states")[c("x", "y", "abbr")]

data <- merge(data, centroids, by.x = "region", by.y = "abbr", all.x = TRUE)

plot_usmap(data = statepop, values = "pop_2015") +
  scale_fill_distiller(palette = "BuGn") +
  ggnewscale::new_scale_fill() +
  geom_scatterpie(aes(x, y, group = region),
                  data = data, cols = c("gas", "coal", "wind", "solar")
  ) +
  geom_text(aes(x, y, label = region),
            data = data, vjust = 1, nudge_y = -100000
  ) +
  theme(legend.position = "left")

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51