如何在ggplot色图中将NA颜色从灰色更改为白色? [英] How do I change the NA color from gray to white in a ggplot chloropleth map?

查看:235
本文介绍了如何在ggplot色图中将NA颜色从灰色更改为白色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个默认颜色从灰色变为白色的美国的Choropleth地图。

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

我有48个州的18个记录,代码通过值进行着色,但对于没有记录的状态,状态为灰色。我希望他们是白色的。

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.

如何更改颜色?

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

提前谢谢。

推荐答案

您可以通过在 na.value 来更改NA值(无数据状态)的颜色> scale_fill_continuos()。

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

这篇关于如何在ggplot色图中将NA颜色从灰色更改为白色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆