如何在美国单独绘制某些州并标记它们?
目前我已经能够使用此功能绘制状态
library(maps)
map("state",c('New York','massachusetts','Pennsylvania'),fill=TRUE,col="dark green",projection="polyconic")
但我还没有给它们贴上标签。
答案 0 :(得分:0)
您可以使用ggmap和ggplot2包在美国地图中绘制美国州。
library(ggmap)
library(ggplot2)
usaState<- data.frame(c("new york", "massachusetts", "pennsylvania"))
colnames(usaState) <- "statename"
LatLon <- c(apply(usaState, 1, geocode))
LatLonDF <- do.call(rbind.data.frame, LatLon)
stateLatLonDF <- cbind(usaState, LatLonDF)
myMap <- ggmap(get_map(location = 'united states', zoom = 4, source = 'google', maptype="terrain"))
myMap <- myMap + geom_text(data = stateLatLonDF, aes(x = lon, y = lat, label = statename), size = 3, vjust = 0, hjust = -0.1, color = "red")
print(myMap)
可以根据需要更改缩放,文本参数等。希望这会有所帮助。
答案 1 :(得分:0)
@Gughan在前面的代码片段中,我使用了get_map(location =&#39;美国&#39;)来获取地图,这将带来整个美国地图。要放大到特定位置,可以通过纬度和经度的平均值获得get_map()的位置。通过控制get_map()中的缩放参数,并运行以下代码,我将东海岸放大。
library(ggmap)
library(ggplot2)
usaState<- data.frame(c("new york", "massachusetts", "pennsylvania"))
colnames(usaState) <- "statename"
LatLon <- c(apply(usaState, 1, geocode))
LatLonDF <- do.call(rbind.data.frame, LatLon)
stateLatLonDF <- cbind(usaState, LatLonDF)
map <- get_map(location = c(lon = mean(stateLatLonDF$lon), lat = mean(stateLatLonDF$lat)), color="color",source="google", maptype="terrain", zoom=7)
ggmap(map) + geom_point(data = stateLatLonDF, aes(x = lon, y = lat, color = statename, alpha = 0.5, fill = "red"), size = 5, shape = 21) + geom_text(data = stateLatLonDF, aes(x = lon, y = lat, label = statename), size = 3, vjust = 0, hjust = -0.1, color = "blue") + scale_colour_discrete(name = "statename")