64

I am trying to create a choropleth map of the US that has the default color changed from gray to white.

I have records for 18 of 48 states and the code works to color by value, but for those states where I have no records the states are gray. I would like them to be white.

How do I change the color?

library(maps)
library(plyr)
library(ggplot2)
records1<-read.csv('E:/My Documents/records_by_state.csv')
records<-data.frame(state=tolower(rownames(records1)), records1)
head(records)
all_states<-map_data("state")
head(all_states)
record_map<-merge(all_states, records, by.x="region", by.y="state.name")
record_map<-arrange(record_map, group, order)
head(record_map)

p<- ggplot()

p<- p + geom_polygon(data=record_map, aes(x=long, y=lat, group=group,    fill=record_map$Records), colour="black"
         )+ scale_fill_continuous(low="thistle2", high="darkred", guide="colorbar")
P1 <- p + theme_bw() +labs(fill= "Records by State"
                    , title= "By State", x="", y="")
P1 + scale_y_continuous(breaks=c()) + scale_x_continuous(breaks=c()) +  theme(panel.border= element_blank())
Uwe
  • 41,420
  • 11
  • 90
  • 134
user2320821
  • 1,141
  • 3
  • 13
  • 19

2 Answers2

119

You can change color of NA values (states without data) by changing argument na.value in scale_fill_continuos().

+scale_fill_continuous(low="thistle2", high="darkred", 
                       guide="colorbar",na.value="white")
Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201
0

@DidzisElferts's answer may remove the label for NA (i.e. the text "NA", for instance, may disappear in the graph), as @TobiO's comment indicates. To prevent that, supply all labels for the variable to labels argument in scale_fill_*()

+ scale_fill_continuous(
    low = "thistle2",
    high = "darkred",
    guide = "colorbar",
    na.value = "white",
    ## Add label for NA (here, the label is `none`)
    ## Replace `...` with your labels for non-NA levels below
    labels = c(..., "none") 
  )
Carlos Luis Rivera
  • 3,108
  • 18
  • 45