How do I change the NA color from gray to white in a ggplot choropleth map?

前提是你 提交于 2019-11-29 16:07:59

问题


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())

回答1:


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")


来源:https://stackoverflow.com/questions/16220812/how-do-i-change-the-na-color-from-gray-to-white-in-a-ggplot-choropleth-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!