2012-06-19 4 views
4

map2SpatialPolygons을 사용하여 미국의 하위 집합을 나타내는지도에 커널 밀도 플롯을 만들려고합니다. 나는 "지도와 ID의 길이가 다르다"는 오류가 계속 발생합니다. (전체 미국을 매핑 할 때)R : map2SpatialPolygons의 ID

nor_coast_poly <- map("world", "norway", fill=TRUE, col="transparent", plot=FALSE, ylim=c(58,72)) 
IDs <- sapply(strsplit(nor_coast_poly$names, ":"), function(x) x[1]) 
nor_coast_poly_sp <- map2SpatialPolygons(nor_coast_poly, IDs=IDs, proj4string=CRS("+proj=longlat +datum=wgs84")) 

이 코드는 작동 :

usmap <- map('usa', fill=TRUE, col="transparent", resolution=0, plot=FALSE) 
uspoly <- map2SpatialPolygons(usmap, IDs=usmap$names, proj4string=CRS("+proj=longlat +datum=WGS84")) 

하지만이 코드는 그렇지 않습니다

나는이 코드는 (map2SpatialPolygons에 대한 도움말에서) 작동하는지 알고있다 :

:

states.to.plot=c("illinois", "indiana", "ohio") 
dmap<-map("state", regions=states.to.plot, col="transparent", plot=FALSE) 
dpoly <- map2SpatialPolygons(dmap, IDs=dmap$names, proj4string=CRS("+proj=longlat +datum=WGS84")) 

그것은 오류가 발생합니다

Error in map2SpatialPolygons(dmap, IDs = dmap$names, proj4string = CRS("+proj=longlat +datum=WGS84")) : 
    map and IDs differ in length 

지도 ("상태"...)를 사용할 때 ID를 올바르게 얻으려면 어떻게해야합니까? ?map에서

답변

6

: 기본 fill = FALSE에 따라서

The return value is a list with x, y, range, and names components. ... If fill is FALSE, the x and y vectors are the coordinates of successive polylines, separated by NAs. If fill is TRUE, the x and y vectors have coordinates of successive polygons, again separated by NAs.

, 당신은 폴리 라인의 무리, 당신은 플롯하고 싶은 세 가지 상태 이상되는 "무리"를 얻고, 위의 오류가 이유입니다 . 그러나 정확한 수의 ID가 있어도 map이 다각형 대신 폴리선을 반환하기 때문에 여전히 오류가 발생합니다.

dmap<-map("state", regions=states.to.plot, col="transparent", plot=FALSE, fill = TRUE) 

dpoly <- map2SpatialPolygons(dmap, IDs = dmap$names, 
    proj4string=CRS("+proj=longlat +datum=WGS84")) 
+0

우수한 대답, 똑같은 질문을 여기 온 :

fill = TRUE으로, 당신은 당신이하고있는 다각형 후 얻을. – tomw