R中的等值线绘图问题

发布于 2024-08-28 21:37:53 字数 2063 浏览 1 评论 0原文

编辑:我已经意识到问题的根源。我只有我有数据的县的计数信息,该信息少于我正在绘制的区域中的县数量。

按理说,问题代码行在这里:

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

是否有人对如何生成与地图库中的纽约州、新泽西州、康涅狄格州和宾夕法尼亚州的县数相匹配的适当长度的向量有建议?我想合并我拥有的计数数据,并包括我没有信息的县的零。

我正在尝试按照此处描述的教程进行操作: http://www.thisisthegreenroom.com /2009/choropleths-in-r/

下面的代码执行,但它要么没有将我的数据集与maps_counties数据正确匹配,要么没有按照我期望的顺序绘制它。例如,大纽约地区的结果区域没有显示密度,而宾夕法尼亚州的随机县则显示最高密度。

我的数据表的一般格式是:

county state count
fairfield connecticut 17
hartford connecticut 6
litchfield connecticut 3
new haven connecticut 12
...
...
westchester new york 70
yates new york 1
luzerne pennsylvania 1

请注意,此数据按州顺序排列,然后按县顺序排列,包括 CT、NJ、NY 等州的数据。宾夕法尼亚州。

首先,我读入我的数据集:

library(maps)
library(RColorBrewer)
d <- read.table("gissum.txt", sep="\t", header=TRUE)

#Concatenate state and county info to match maps library
d$stcon <- paste(d$state, d$county, sep=",")

#Color bins
colors = brewer.pal(5, "PuBu")
d$colorBuckets <- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))))

这是我的匹配

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

绘图:

map("county"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = colors[d$colorBuckets[na.omit(match(mapnames ,d$stcon))]]
  ,fill = TRUE
  ,resolution = 0
  ,lty = 0
  ,lwd= 0.5
)
map("state"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = "black"
  ,fill=FALSE
  ,add=TRUE
  ,lty=1
  ,lwd=2
)

map("county"
   ,c("new york","new jersey", "connecticut", "pennsylvania")
   ,col = "black"
   ,fill=FALSE
   ,add=TRUE
  , lty=1
  , lwd=.5
)
title(main="Respondent Home ZIP Codes by County")

我确信我错过了一些基本的内容:地图函数绘制项目的顺序 - 但我似乎无法弄清楚。感谢您的帮助。如果您需要更多信息,请告诉我。

EDIT: I have realized the source of my problem. I only have count information for the counties which I have data for, which is less than the number of counties in the area I'm plotting against.

It stands to reason that the problem lines of code are here:

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

Does anyone have advice on how to generate a vector of the appropriate length that would match the # of counties in NY, NJ, CT, and PA from the maps library? I want to merge the count data I have and include zeros for the counties I don't have information on.

I am trying to follow the tutorial described here: http://www.thisisthegreenroom.com/2009/choropleths-in-r/

The below code executes, but it is either not matching my dataset with the maps_counties data properly, or it isn't plotting it in the order I would expect. For example, the resulting areas for the greater NYC area show no density while random counties in PA show the highest density.

The general format of my data table is:

county state count
fairfield connecticut 17
hartford connecticut 6
litchfield connecticut 3
new haven connecticut 12
...
...
westchester new york 70
yates new york 1
luzerne pennsylvania 1

Note this data is in order by state and then county and includes data for CT, NJ, NY, & PA.

First, I read in my data set:

library(maps)
library(RColorBrewer)
d <- read.table("gissum.txt", sep="\t", header=TRUE)

#Concatenate state and county info to match maps library
d$stcon <- paste(d$state, d$county, sep=",")

#Color bins
colors = brewer.pal(5, "PuBu")
d$colorBuckets <- as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))))

Here is my matching

mapnames <- map("county",plot=FALSE)[4]$names
colorsmatched <- d$colorBuckets [na.omit(match(mapnames ,d$stcon))]

Plotting:

map("county"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = colors[d$colorBuckets[na.omit(match(mapnames ,d$stcon))]]
  ,fill = TRUE
  ,resolution = 0
  ,lty = 0
  ,lwd= 0.5
)
map("state"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = "black"
  ,fill=FALSE
  ,add=TRUE
  ,lty=1
  ,lwd=2
)

map("county"
   ,c("new york","new jersey", "connecticut", "pennsylvania")
   ,col = "black"
   ,fill=FALSE
   ,add=TRUE
  , lty=1
  , lwd=.5
)
title(main="Respondent Home ZIP Codes by County")

I am sure I am missing something basic re: the order in which the maps function plots items - but I can't seem to figure it out. Thanks for the help. Please let me know if you need any more information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤仙姿 2024-09-04 21:37:53

通过将您的数据与选定州地图中的数据合并,可以解决您的问题。这是您要找的吗?

library(maps);
library(RColorBrewer);

# Create Dummy Data Frame to Play With

d = rbind(c('fairfield','connecticut',17),c('westchester','new york',70), c('luzerne','pennsylvania',1));
d = data.frame(d);
names(d) = c("county", "state", "count");
d$count = as.numeric(as.character(d$count));
d$stcon = paste(d$state, d$county, sep=",");

# Extract mapnames for States

mapnames2 = map("county",c("new york","new jersey", "connecticut", "pennsylvania"),plot=FALSE)[4]$names;
mapnames2 = data.frame(mapnames2);
names(mapnames2) = "stcon";

# Merge with d

d = merge(mapnames2, d, all = T);
d$count[is.na(d$count)] = 0;


# Color bins
colors = brewer.pal(5, "PuBu");
d$colorBuckets = as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))));

map("county"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = colors[d$colorBuckets]
  ,fill = TRUE
  ,resolution = 0
  ,lty = 0
  ,lwd= 0.5
)
map("state"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = "black"
  ,fill=FALSE
  ,add=TRUE
  ,lty=1
  ,lwd=2
)

map("county"
   ,c("new york","new jersey", "connecticut", "pennsylvania")
   ,col = "black"
   ,fill=FALSE
   ,add=TRUE
  , lty=1
  , lwd=.5
)
title(main="Respondent Home ZIP Codes by County")

Here is a possible solution to your problem by merging your data with data from maps of select states. Is this what you were looking for?

library(maps);
library(RColorBrewer);

# Create Dummy Data Frame to Play With

d = rbind(c('fairfield','connecticut',17),c('westchester','new york',70), c('luzerne','pennsylvania',1));
d = data.frame(d);
names(d) = c("county", "state", "count");
d$count = as.numeric(as.character(d$count));
d$stcon = paste(d$state, d$county, sep=",");

# Extract mapnames for States

mapnames2 = map("county",c("new york","new jersey", "connecticut", "pennsylvania"),plot=FALSE)[4]$names;
mapnames2 = data.frame(mapnames2);
names(mapnames2) = "stcon";

# Merge with d

d = merge(mapnames2, d, all = T);
d$count[is.na(d$count)] = 0;


# Color bins
colors = brewer.pal(5, "PuBu");
d$colorBuckets = as.factor(as.numeric(cut(d$count,c(0,10,20,30,40,50,300))));

map("county"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = colors[d$colorBuckets]
  ,fill = TRUE
  ,resolution = 0
  ,lty = 0
  ,lwd= 0.5
)
map("state"
  ,c("new york","new jersey", "connecticut", "pennsylvania")
  ,col = "black"
  ,fill=FALSE
  ,add=TRUE
  ,lty=1
  ,lwd=2
)

map("county"
   ,c("new york","new jersey", "connecticut", "pennsylvania")
   ,col = "black"
   ,fill=FALSE
   ,add=TRUE
  , lty=1
  , lwd=.5
)
title(main="Respondent Home ZIP Codes by County")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文