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")
