2013-04-25 10 views
12

Tôi đang cố tạo bản đồ choropleth của Hoa Kỳ có màu mặc định thay đổi từ xám sang trắng.Làm cách nào để thay đổi màu NA từ xám sang trắng trong bản đồ ggplot choropleth?

Tôi có hồ sơ cho 18 trong số 48 tiểu bang và mã hoạt động với màu theo giá trị, nhưng đối với những tiểu bang mà tôi không có hồ sơ, các trạng thái có màu xám. Tôi muốn chúng có màu trắng.

Làm cách nào để thay đổi màu?

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

Trả lời

26

Bạn có thể thay đổi màu sắc của các giá trị NA (tiểu bang không có dữ liệu) bằng cách thay đổi lập luận na.value trong scale_fill_continuos().

+scale_fill_continuous(low="thistle2", high="darkred", 
         guide="colorbar",na.value="white") 
+0

Cảm ơn bạn - SO nhiều ---- THANK YOU – user2320821

+1

FYI: na.value = "transparent" sẽ xóa hoàn toàn –