0

I'm trying to color Arizona, Utah, and Idaho by different colors. Ideally I'd use a color gradient to color them by a variable I choose. But I can't seem to find any other information on the web about doing this.

This is the code I have so far:

library(ggplot2)
ggplot(data = azutid) + 
geom_polygon(aes(x = long, y = lat, group = group), fill = "green", color = "black") + 
  coord_fixed(1.3) + 
    guides(fill = FALSE)

I imported the map and regions from the basic "maps" package. Thanks!

www
  • 38,575
  • 12
  • 48
  • 84
  • put `fill = region` inside `aes()` then check out `scale_fill_discrete()` – johnson-shuffle Apr 07 '18 at 21:06
  • Possible duplicate of [Colour specific states on a US map](https://stackoverflow.com/questions/46978161/colour-specific-states-on-a-us-map) – Michael Harper Apr 08 '18 at 08:37
  • Please include all the relevant code in your question. In this case, this should include how you imported the map and regions. Please check here on other tips of [asking a great question](https://stackoverflow.com/q/5963269/7347699) – Michael Harper Apr 08 '18 at 08:39

1 Answers1

0

It can be done easily through the use of leaflet package. Although you will have to download the census data from https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html

You can also add costum popups and highlight on hover labels to the map.

library(leaflet)
library(spdplyr)
library(rgdal)
states <- readOGR(dsn = "./cb_2016_us_state_20m/cb_2016_us_state_20m.shp",
layer = "cb_2016_us_state_20m", verbose = FALSE)
group1 <- c("AZ","UT","ID")
newobj <- states %>%
filter(as.character(STUSPS) %in% group1)
m <- leaflet(newobj) %>%
setView(-96, 37.8, 4) %>% addProviderTiles("CartoDB.Positron")
pal = colorQuantile("YlOrRd", domain = newobj$value, n=7)
m %>% addPolygons(fillColor = "orange",
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7)

enter image description here

ReKx
  • 996
  • 2
  • 10
  • 23