1

I would like to plot pie charts of different fuel type for each state on USA map. my data includes fuel types and their amount and each state id. I want to know how to plot pie on map for each state with state id?

Thanks,

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))
neilfws
  • 32,751
  • 5
  • 50
  • 63
Arwen
  • 77
  • 4

1 Answers1

2

Using the usmap and the scatterpie packages this could be achieved via ggplot2 like so:

  1. Add coordinates for the pies to your data. In the code below I use the coordinates of the state centers provided by usmapdata::centroid_labels
  2. Add the pies from your data via geom_scatterpie
library(usmap)
library(ggplot2)
library(scatterpie)

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(regions = "states") +
  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
  )

EDIT If you want to exclude some states (or include only some states) you could do so via the exclude or include argument of plot_usmap:

plot_usmap(regions = "states", exclude = c("AK", "HI")) +
  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
  )

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Thank you so much, Stefan. It worked! Now I want to only have US 49 states (not including Alaska and Hawaii). Is there a way in usmap package to have only us 49 states? (for example in Rmap this will do it: rmap::map(mapUS49)). I searched it for us map package and couldn't find such option. – Arwen Jun 05 '22 at 16:22
  • Hi @Arwen. Sure is this possible. See my edit. – stefan Jun 05 '22 at 17:17