2012-06-15 5 views
7

저는 우주 데이터가있는 완전한 초보자입니다. 성공적으로 바운드 된지도를 그릴 다음 코드가 있습니다. data.frame이 저장하는 포인트를 추가하고 싶습니다. 제가 OpenStreetMap에이 문서에서 알아낼 수없는 미리 사과 ... 아래 번호 :OpenStreetMap을 사용하여 data.frame에서 점을 그립니다.

library(OpenStreetMap) 
stores <- data.frame(name=c("Commercial","Union","Bedford"), 
       longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037), 
       latitude=c(43.657471302616806,43.65663299041943,43.66091757424481)) 
lat <- c(43.68093,43.64278) 
lon <- c(-70.29548,-70.24097) 
portland <- openmap(c(lat[1],lon[1]),c(lat[2],lon[2]),zoom=15,'osm') 
plot(portland,raster=TRUE) 
#can't figure out what to put here. 

제가 저장 형식 공간 데이터에 대한 적절한 아니라고 의심. 미리 감사드립니다. --JT

답변

12

나는 OpenStreetMap 패키지를 모른다. 하지만 여전히 OpenStreet Map을 그리지 만 ggmap 패키지를 사용하여지도를 가져 와서 그릴 수있는 대안을 제시합니다. get_map 함수는 osm, google, stamen 및 cloudmade와 같은 다양한 소스에서 맵을 가져올 수 있습니다. source으로 설정하십시오. 또한 소스의 스타일은 maptype으로 설정됩니다. 지도의 경계는 위치 벡터에 지정됩니다. 또는 위치 벡터는 적절한 줌 레벨이 설정된지도의 중심을 제공 할 수 있습니다. 지도는 ggplot2으로 그려지며 점과 레이블을 ggplot 객체에 추가 된 것처럼지도에 추가 할 수 있습니다. 다음을 실행하려면 ggmapggplot2 패키지를 설치해야합니다.

library(ggmap) 

stores <- data.frame(name=c("Commercial","Union","Bedford"), 
     longitude=c(-70.25042295455933,-70.26050806045532,-70.27726650238037), 
     latitude=c(43.657471302616806,43.65663299041943,43.66091757424481)) 
location = c(-70.2954, 43.64278, -70.2350, 43.68093) 

# Fetch the map 
portland = get_map(location = location, source = "osm") 

# Draw the map 
portlandMap = ggmap(portland) 

# Add the points layer 
portlandMap = portlandMap + geom_point(data = stores, aes(x = longitude, y = latitude), size = 5) 

# Add the labels 
portlandMap + geom_text(data = stores, aes(label = name, x = longitude+.001, y = latitude), hjust = 0) 

결과는 다음과 같습니다

enter image description here

라벨은 백그라운드에서 길을 잃지 수 있습니다. 이 경우에는 이와 같은 것이 적절할 수 있습니다. 텍스트에 윤곽선을 지정하기 위해 code from here을 사용합니다.

portlandMap = ggmap(portland) + geom_point(data = stores, aes(x = longitude, y = latitude), size = 5) 

theta <- seq(pi/16, 2*pi, length.out=32) 
xo <- diff(location[c(1,3)])/250 
yo <- diff(location[c(2,4)])/250 

for(i in theta) { 
    portlandMap <- portlandMap + geom_text(data = stores, 
    aes_(x = stores$longitude + .001 + cos(i) * xo, 
     y = stores$latitude + sin(i) * yo, 
     label = stores$name), 
    size = 5, colour = 'black', hjust = 0) 
} 

portlandMap + 
    geom_text(data = stores, aes(x = longitude + .001, y = latitude, label=name), 
    size = 5, colour = 'white', hjust = 0) 

enter image description here

+0

감사 하니까요 ...이 완벽하게 작동합니다! – JimmyT

관련 문제