0

Trying to make a chloropleth map of the US. Fill for each state should be one color of varying darkness depending on the value for each respective state in the column assigned to it. Instead of a gradient scale in one color, I get a rainbow of colors with each individual value having its own distinct color on the rainbow scale. The legend is three columns of rainbow gradient colors with each distinct value having its own color instead of a small bar with a gradient of one color and a range of values.

library(ggplot2)
library(maps)
suppressMessages(library(ggmap))
library(mapproj)
suppressMessages(library(dplyr))

Get map data for US states

 states_map <- map_data("state")

Create lowercase state names

Cats_vs_Dogs <- mutate(Cats_vs_Dogs, Location = tolower(Location))

ggplot(data=Cats_vs_Dogs, aes(fill = Cat.Population)) +
geom_map(map = states_map, aes(map_id = Location), color="white", size=0.3) +
expand_limits(x = states_map$long, y = states_map$lat) +
coord_map("polyconic") +
theme_void() +
ggtitle("2018 US Cat Populations By State")

enter image description here

Uwe
  • 41,420
  • 11
  • 90
  • 134
  • 2
    Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung May 12 '19 at 04:49
  • 2
    It seems that `Cat.Population` is of type character and thus creates a discrete scale. There are commas included in the strings as thousands separator. These need to be removed and the result coerced to numeric before plotting. You may try `fill = as.numeric(gsub(",", "", Cat.Population))` in the first call of `aes()`. – Uwe May 12 '19 at 07:48
  • Possible duplicate of [How to read data when some numbers contain commas as thousand separator?](https://stackoverflow.com/questions/1523126/how-to-read-data-when-some-numbers-contain-commas-as-thousand-separator) – camille May 12 '19 at 19:04

0 Answers0