-3

I am using the package from this source. My aim is to change the colors in the Choropleth map, but fail.

chargers <- read.csv("Public charging stations_outlets_state.csv", header = TRUE)
df <- as.data.frame(chargers)

chargers$statelower <- tolower(chargers$State)


p1 <- ggplot(df, aes(map_id = chargers$statelower)) + 
geom_map(aes(fill = chargers$Stations), map = fifty_states) + 
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
labs(x = "", y = "") +
theme(legend.position = "bottom",
panel.background = element_blank())
p1

Error: Discrete value supplied to continuous scale

I also tried to supply the colors manually, but failed. Additionally I tried many answers here in the forum but didn't succeed.

When I dput the data I get following information:

> dput(chargers)
structure(list(State = structure(c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 
48L, 49L, 50L, 51L, 52L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("", "Alabama", "Alaska", "Arizona", "Arkansas", 
"California", "Colorado", "Connecticut", "Delaware", "District of Columbia", 
"Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", 
"Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", 
"Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", 
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", 
"New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", 
"Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", 
"South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", 
"Washington", "West Virginia", "Wisconsin", "Wyoming"), class = "factor"), 
    Stations = structure(c(6L, 10L, 30L, 45L, 33L, 43L, 23L, 
    32L, 7L, 3L, 46L, 18L, 44L, 31L, 13L, 8L, 11L, 6L, 51L, 9L, 
    38L, 40L, 25L, 21L, 35L, 26L, 29L, 47L, 17L, 5L, 20L, 42L, 
    4L, 37L, 12L, 28L, 41L, 39L, 27L, 48L, 16L, 22L, 24L, 2L, 
    14L, 15L, 36L, 49L, 50L, 19L, 34L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "1,123", "1,168", 
    "1,193", "110", "114", "122", "127", "155", "16", "185", 
    "19", "207", "208", "212", "235", "246", "265", "285", "288", 
    "326", "33", "334", "388", "401", "408", "430", "432", "44", 
    "444", "485", "49", "5,080", "51", "56", "575", "583", "592", 
    "594", "597", "66", "67", "695", "70", "72", "771", "79", 
    "85", "868", "87", "90"), class = "factor"), X = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L), .Label = c("", "charging outlets)"), class = "factor"), 
    statelower = c("alabama", "alaska", "arizona", "arkansas", 
    "california", "colorado", "connecticut", "delaware", "district of columbia", 
    "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", 
    "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", 
    "massachusetts", "michigan", "minnesota", "mississippi", 
    "missouri", "montana", "nebraska", "nevada", "new hampshire", 
    "new jersey", "new mexico", "new york", "north carolina", 
    "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", 
    "rhode island", "south carolina", "south dakota", "tennessee", 
    "texas", "utah", "vermont", "virginia", "washington", "west virginia", 
    "wisconsin", "wyoming", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "")), row.names = c(NA, -108L), class = "data.frame")

I understand that there is an issue with discrete value and continuous scale, but I don't know how to solve it.

Can someone help me with that?

trotta
  • 1,232
  • 1
  • 16
  • 23
Cassy29
  • 3
  • 1
  • 3
  • 1
    What is `fifty_states`? – jay.sf Jul 07 '19 at 14:31
  • 1
    You've referenced 3 different data frames, but only posted 1. Additionally, [don't use `$` inside your `aes`](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio). If you look at your data, you have `Stations` as a factor, which you probably don't want – camille Jul 07 '19 at 15:33

1 Answers1

1

The error is telling you that you have a categorical variable where a continuous variable should be. You don't mention which variable it is, which usually R tells you. It might help if you paste the entire error. However, if R is reading it incorrectly and it actually is a continuous variable, you can use this: as.numeric(variable_name). If it is actually a categorical variable, then something is wrong in the approach, ie you're using the wrong chart type.

Digging into your code, you have Stations as class ="factor". Try changing this to numeric.

wisamb
  • 470
  • 3
  • 11