R代码生成具有特定颜色的美国各州地图 [英] R code to Generating map of US states with specific colors

查看:69
本文介绍了R代码生成具有特定颜色的美国各州地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to generate the map of U.S. in which each state can have one of the following colors:

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

我创建了一个数据框,states_info,以使其与每个州的颜色相匹配.

I have created a data frame, states_info, to match each state with it's color.

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

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

I have tried various ways to get the colors for each state correct, but my code is not working. (btw, "colors" is a factor variable and contain the hex value of specific colors)

方法1:

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

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

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

I get a map, but the colors for the states are not correct. This approach probably requires matching, but I cannot figure it out.

方法2:我通过将每个州的纬度和经度与state_info数据框合并以绘制地图来创建数据框

Approach 2: I create a data frame by merging the latitudes and longitudes for each state with my state_info dataframe to draw the map

    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)

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

This approach generates a map using its own color gradient and not the colors I specified. What am I doing wrong? Thank you.

推荐答案

让ggplot2为您完成艰苦的工作:

Let ggplot2 do the hard work for you:

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

这篇关于R代码生成具有特定颜色的美国各州地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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