【问题标题】:R code to Generating map of US states with specific colorsR代码生成具有特定颜色的美国各州地图
【发布时间】:2016-12-11 04:34:01
【问题描述】:

我正在尝试生成美国地图,其中每个州都可以具有以下颜色之一:

EScolors <- c("#7aad42","#4a77bb","#f7931e","#d3dfbd","#787878")

我创建了一个数据框 states_info,将每个状态与其颜色相匹配。

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

我尝试了各种方法来正确获取每个状态的颜色,但我的代码无法正常工作。 (顺便说一句,“颜色”是一个因子变量,包含特定颜色的十六进制值)

方法一:

map('state',fill=TRUE,col=states_info$colors)

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

方法 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)

这种方法使用自己的颜色渐变生成地图,而不是我指定的颜色。我究竟做错了什么? 谢谢。

【问题讨论】:

  • ?scale_color_manual 是你要找的。​​span>

标签: r colors maps states


【解决方案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

【讨论】:

  • 谢谢。我可以使用您的代码重现您的结果。但是,如果我从文件中读取数据,它就不起作用。我确实设法使用下面的代码让它工作:
【解决方案2】:

使用基本地图包可能有点棘手。州的顺序和命名不规范,有几个州有不止一个地区,即纽约的曼哈顿岛。需要进行一些操作才能正确标记/着色地图。
在这个解决方案中,我创建了一个数据框 statelist 来保存州名、岛屿和索引。然后将其与您的数据框 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多