简体   繁体   English

R中的基本Choropleth状态图

[英]Basic Choropleth State Map in R

I apologize because I am quite certain this is a basic question. 我很抱歉,因为我可以肯定这是一个基本问题。 All I want to do is create a very simple choropleth map in R using the maps package. 我要做的就是使用maps包在R中创建一个非常简单的Choropleth映射。 This is my first attempt at mapping any data in R. 这是我第一次尝试映射R中的任何数据。

My geography is the lower 48 states including DC. 我的地理位置是包括DC在内的下48个州。 Here are the first few rows of the dataset I want to plot. 这是我要绘制的数据集的前几行。

> head(choro, n=7)
    AL     AR     AZ     CA     CO     CT     DC 
 "red"  "red"  "red" "blue" "blue" "blue"  "red" 

When I attempt to plot a basic map: 当我尝试绘制基本地图时:

map("state",
        regions = names(choro),
        lty = 1, lwd =1,
        boundary=TRUE,
        fill=TRUE,
        col=choro)

The attached image is what I see. 所附图片就是我所看到的。 I am new to R, but I imagine the states that are not filled in did not merge correctly, although, I believe I am using 'standard' state abbreviations. 我是R的新手,但我想未填充的状态不能正确合并,尽管我相信我使用的是“标准”状态缩写。

What am I doing wrong!/need to fix? 我在做什么错!/需要解决?

Any help will be very much appreciated. 任何帮助将不胜感激。

If you take out the regions=names(choro), you will get a more completely filled map. 如果您将regions = names(choro)取出,则会得到一个更完整的地图。 If you want the items that are probably missing from the choro vector to be drawn then why not substitute "white" as the color? 如果要绘制Choro向量中可能缺少的项目,那为什么不用“白色”代替颜色呢? 替代文字 If you need a more complete answer then provide a full copy of choro. 如果您需要更完整的答案,请提供完整的choro副本。

choro <- c( "red",  "red",  "red" ,"blue" ,"blue", "blue",  "red")
require(maps)
data(state)
names(choro) <-names(state)[1:7]
map("state",  lty = 1, lwd =1,
        boundary=TRUE, fill=TRUE,
        col=choro)

See attached graphic: 参见附图:

Related question here: Choropleth mapping issue in R And tutorial here: http://www.thisisthegreenroom.com/2009/choropleths-in-r/ 相关问题在这里: R中的Choropleth映射问题 And此处的教程: http : //www.thisisthegreenroom.com/2009/choropleths-in-r/

As mentioned previously, you'll want to merge your colors with the regions that are plotted. 如前所述,您将希望将颜色与绘制的区域合并。 The second link above shows a technique to match state abbreviations with the full state names that R wants/needs to use the maps package. 上面的第二个链接显示了一种将状态缩写与R希望/需要使用maps包的完整状态名进行匹配的技术。 Depending on where your choro data is coming from, it may be easiest to replace the state abbreviations with full state names before reading into R. The other thing of consequence to note is that there are 63 state objects that are plotted. 根据choro数据的来源,最简单的方法是在读入R之前用完整的状态名称替换状态缩写。还要注意的另一件事是,绘制了63个状态对象。 For example, New Yorsk has a few different objects. 例如,新约斯克(New Yorsk)有一些不同的对象。 Mapping to these duplicates will be necessary for a complete looking map. 完整映射需要映射到这些重复项。

I would recommend making two calls to the map function - the first to plot your fill, the second to add a different outline. 我建议两次调用map函数-第一个调用绘制填充,第二个添加不同的轮廓。 For example: 例如:

# Extract mapnames for States
mapnames <- data.frame(
    state = map("state",plot=FALSE)[4]$names
    , col = sample(c("pink", "purple", "lavender", "blue"), 63, replace = TRUE)
)

#Plot the colors
map("state", regions = mapnames$state, col = mapnames$col, fill = TRUE, lty = 1, lwd= 1)
#Plot the outlines
map("state", regions = mapnames$state, col = "black", fill = FALSE, add = TRUE, lty = 1, lwd = 1)

You can also take a look at the map_data() function in ggplot2 for other examples of merging data to create choropleth maps easily. 您还可以查看map_data()中的map_data()函数, ggplot2获取其他合并数据的示例,以轻松创建Choropleth地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM