【发布时间】:2016-11-18 09:57:40
【问题描述】:
我有一张要绘制 60 个城镇的地图。 我想显示城镇的名称,但它太杂乱了。有没有办法给它们编号并将名称显示为如下图例?
我尝试了“calibrate”包中的“textxy”功能,但它只显示在地图上。
谢谢!
【问题讨论】:
我有一张要绘制 60 个城镇的地图。 我想显示城镇的名称,但它太杂乱了。有没有办法给它们编号并将名称显示为如下图例?
我尝试了“calibrate”包中的“textxy”功能,但它只显示在地图上。
谢谢!
【问题讨论】:
使用包ggmap。它允许您获取基本地图并使用ggplot函数在地图顶部绘制坐标。这是一个可重现的示例:
library(ggmap)
library(dplyr)
# Get the base map
nevada <- get_map(location = 'nevada', maptype = "satellite", source = "google", zoom = 6)
# View the basemap
ggmap(nevada)
# Create data frame with cities and lat/long and an index
cities <- data.frame(city = c("Las Vegas", "Caliente", "Elko"),
lat = c(36.169941, 37.614965, 40.832421),
long = c(-115.13983, -114.511938, -115.763123))
cities <- cities %>% mutate(index = seq.int(nrow(cities)))
# Map with legend, colored by city
ggmap(nevada) +
geom_point(data = cities, aes(x = long, y = lat, color = paste(index, city)), cex = 3) +
labs(color = "cities") +
geom_text(data = cities, aes(x = long, y = lat, label = index), hjust = 2, color = "white")
如果您希望所有城市的颜色都相同,请使用fill = city 而不是color = city,并在aes() 之外指定颜色(例如color = "red")。
# Map with legend, all cities same color
ggmap(nevada) +
geom_point(data = cities, aes(x = long, y = lat, fill = city), cex = 3, color = "red") +
labs(fill = "cities") +
geom_text(data = cities, aes(x = long, y = lat, label = index), hjust = 2, color = "white")
【讨论】: