标记R ggplot中地图多边形的中心

Zue*_*nie 12 maps label r ggplot2

我试图通过在R中使用ggplot来标记我的多边形.我在stackoverflow上找到了一个主题,我认为它非常接近我想要的除了点数.

在geom_point中标记点

我在网上找到了一些方法.现在我首先需要找到每个形状的中心位置,然后我必须将这些位置与名称放在一起.然后将其链接到geom_text()中的标注函数

ggplot在地图上居中显示名称

因为我已经尝试了很长时间,所以我决定提出这个问题,并希望这里的某个人可以给我最终的推动力.我的绘图功能:

region_of_interest.fort <- fortify(region_of_interest, region = "score")
region_of_interest.fort$id <- as.numeric(region_of_interest.fort$id)
region_of_interest.fort$id <- region_of_interest.fort$id


region_of_interest.fort1 <- fortify(region_of_interest, region = "GM_NAAM")
region_of_interest.fort1$id <- as.character(region_of_interest.fort1$id)
region_of_interest.fort1$id <- region_of_interest.fort1$id

idList <- unique(region_of_interest.fort1$id)
centroids.df <- as.data.frame(coordinates(region_of_interest))
names(centroids.df) <- c("Longitude", "Latitude")
randomMap.df <- data.frame(id = idList, shading = runif(length(idList)), centroids.df)

ggplot(data = region_of_interest.fort, aes(x = long, y = lat, fill = id, group = group)) +
  geom_polygon() +
  geom_text(centroids.df, aes(label = id, x = Longitude, y = Latitude)) +
  scale_fill_gradient(high = "green", low = "red", guide = "colorbar") +
  coord_equal() +
  theme() +
  ggtitle("Title")
Run Code Online (Sandbox Code Playgroud)

它给了我错误:ggplot2不知道如何处理类uneval的数据

我的数据

region_of_interest$GM_NAAM
 [1] Groningen        Haren            Ooststellingwerf Assen            Aa en Hunze      Borger-    Odoorn   
 [7] Noordenveld      Westerveld       Tynaarlo         Midden-Drenthe  
415 Levels: 's-Gravenhage 's-Hertogenbosch Aa en Hunze Aalburg Aalsmeer Aalten ... Zwolle

region_of_interest$score
 [1] 10 -2 -1  2 -1 -4 -4 -5  0  0
Run Code Online (Sandbox Code Playgroud)

Sil*_*ish 15

尝试这样的事情?

  1. 从原始地图对象获取多边形质心的数据框.

  2. 在您正在绘制的数据框中,确保要标记的ID列以及这些质心的经度和纬度.

  3. 在ggplot中使用geom_text添加标签.

基于这个例子,我读取了一张世界地图,提取了ISO3 ID作为我的多边形标签,并制作了一个国家ID,种群以及质心经度和纬度的数据框.然后,我在世界地图上绘制人口数据,并在质心处添加标签.

library(rgdal) # used to read world map data
library(rgeos) # to fortify without needing gpclib
library(maptools)
library(ggplot2)
library(scales) # for formatting ggplot scales with commas

# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'

worldMap <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
# Change "data" to your path in the above!
worldMap.fort <- fortify(world.map, region = "ISO3")
# Fortifying a map makes the data frame ggplot uses to draw the map outlines.
# "region" or "id" identifies those polygons, and links them to your data. 
# Look at head(worldMap@data) to see other choices for id.
# Your data frame needs a column with matching ids to set as the map_id aesthetic in ggplot. 
idList <- worldMap@data$ISO3
# "coordinates" extracts centroids of the polygons, in the order listed at worldMap@data
centroids.df <- as.data.frame(coordinates(worldMap))
names(centroids.df) <- c("Longitude", "Latitude")  #more sensible column names
# This shapefile contained population data, let's plot it.
popList <- worldMap@data$POP2005

pop.df <- data.frame(id = idList, population = popList, centroids.df)

ggplot(pop.df, aes(map_id = id)) + #"id" is col in your df, not in the map object 
  geom_map(aes(fill = population), colour= "grey", map = worldMap.fort) +
  expand_limits(x = worldMap.fort$long, y = worldMap.fort$lat) +
  scale_fill_gradient(high = "red", low = "white", guide = "colorbar", labels = comma) +
  geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids
  coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + #let's view South America
  labs(x = "Longitude", y = "Latitude", title = "World Population") +
  theme_bw() 
Run Code Online (Sandbox Code Playgroud)

南美洲人口地图

小技术说明:实际上coordinatessp包装中并没有找到质心,但它通常应该为标签提供合理的位置.如果要在非连续形状等更复杂的情况下标记真实质心,请gCentroidrgeos包中使用.