2016-08-04 23 views
1

我试图产生US的地图,其中每个状态可以具有下列颜色中的一种状态,以匹配每个国家与它的颜色。R代码里面到美国的生成地图特定颜色

head(states_info) 
    State.Code region St_Abbr Num_Estab colors 
1   1 alabama  AL  13123 #f7931e 
3   4 arizona  AZ  18053 #f7931e 
4   5 arkansas  AR  9154 #4a77bb 
5   6 california  CA 143937 #787878 
6   8 colorado  CO  21033 #d3dfbd 
7   9 connecticut  CT  17176 #f7931e 

我已经尝试过各种方法让每个状态的颜色正确,但我的代码不工作。 (顺便说一句, “颜色” 是一个因素变量和包含的特定颜色的十六进制值)

方法1:

地图( '州',填充= TRUE,COL = states_info $颜色)

我得到一张地图,但各州的颜色不正确。这种方法可能需要匹配,但我无法弄清楚。

方法2:我通过合并每个状态的纬度和经度与我的state_info数据帧来绘制地图

states_location <- map_data("state") 
    map.df <- merge(states_location,states_info, by=intersect(states_location$region, states_info$region), all=TRUE) 
    map.df <- map.df[order(map.df$order),] 

    ggplot(map.df, aes(x=long,y=lat,group=group))+ 
    geom_polygon(aes(fill=region.x))+ 
     geom_path()+ 
     scale_color_hue(states_info$colors) 

该方法使用其自己的颜色梯度,而不是颜色产生地图创建的数据帧我指定。我究竟做错了什么? 谢谢。

+1

'scale_color_manual'是你在找什么。 – AlexR

回答

0

使用基本地图包可能有点棘手。各州的命令和命名并不标准,有几个州有不止一个地区,即纽约的曼哈顿岛。正确标记/着色地图需要一点操作。
在此解决方案中,我创建了一个数据框状态列表来保存状态名称,岛屿和索引。然后将其与您的数据框state_info合并,然后绘制。

#function to split strings an return a dataframe 
strtodf<-function (list){ 
    slist<-strsplit(list, ":") 
    x<-sapply(slist, FUN= function(x) {x[1]}) 
    y<-sapply(slist, FUN= function(x) {x[2]}) 
    df<-data.frame(state=x, island=y, stringsAsFactors = FALSE) 
    return(df) 
} 

#user defined coloring scheme 
# Example data for to test solution 
colors<- c("#7aad42","#4a77bb","#f7931e","#d3dfbd","#787878") 
region<-c("washington", "new york", "virginia", "pennsylvania", "ohio") 
states_info<-data.frame(region, colors) 
# End of example data 

#dataframe to hold state names for mapping purposes 
library(maps) 
maplist<-map("state", namesonly = TRUE, plot=FALSE) 
statelist<-strtodf(maplist) #convert to dataframe 
statelist$row<-as.numeric(rownames(statelist)) #index column 

#merge the data from and resort into proper order 
statelist<-merge(statelist, states_info, by.x = "state", by.y="region", sort=FALSE, all.x=TRUE) 
statelist<-statelist[order(statelist$row),] 

#plot the map 
maplist<-map("state", fill=TRUE, col=statelist$colors) 
1

让GGPLOT2做艰苦的工作适合你?

library(ggplot2) 

read.table(text="State.Code region St_Abbr Num_Estab colors 
1   1 alabama  AL  13123 #f7931e 
3   4 arizona  AZ  18053 #f7931e 
4   5 arkansas  AR  9154 #4a77bb 
5   6 california  CA 143937 #787878 
6   8 colorado  CO  21033 #d3dfbd 
7   9 connecticut  CT  17176 #f7931e", 
      stringsAsFactors=FALSE, header=TRUE, comment.char="") -> df 

usa_map <- map_data("state") 

gg <- ggplot() 
gg <- gg + geom_map(data=usa_map, map=usa_map, 
        aes(long, lat, map_id=region), 
        color="#2b2b2b", size=0.15, fill=NA) 
gg <- gg + geom_map(data=df, map=usa_map, 
        aes(fill=colors, map_id=region), 
        color="#2b2b2b", size=0.15) 
gg <- gg + scale_color_identity() 
gg <- gg + coord_map("polyconic") 
gg <- gg + ggthemes::theme_map() 
gg 

enter image description here

+0

谢谢。我可以使用您的代码重现您的结果。但是,如果我从文件读取数据,它不起作用。我确实设法使用下面的代码来运行它: – mvm