나는 OpenStreetMap
패키지를 모른다. 하지만 여전히 OpenStreet Map을 그리지 만 ggmap
패키지를 사용하여지도를 가져 와서 그릴 수있는 대안을 제시합니다. get_map
함수는 osm, google, stamen 및 cloudmade와 같은 다양한 소스에서 맵을 가져올 수 있습니다. source
으로 설정하십시오. 또한 소스의 스타일은 maptype
으로 설정됩니다. 지도의 경계는 위치 벡터에 지정됩니다. 또는 위치 벡터는 적절한 줌 레벨이 설정된지도의 중심을 제공 할 수 있습니다. 지도는 ggplot2
으로 그려지며 점과 레이블을 ggplot 객체에 추가 된 것처럼지도에 추가 할 수 있습니다. 다음을 실행하려면 ggmap
및 ggplot2
패키지를 설치해야합니다.
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)
결과는 다음과 같습니다
라벨은 백그라운드에서 길을 잃지 수 있습니다. 이 경우에는 이와 같은 것이 적절할 수 있습니다. 텍스트에 윤곽선을 지정하기 위해 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)
감사 하니까요 ...이 완벽하게 작동합니다! – JimmyT