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

时间:2014-02-26 10:32:11

标签: r ggplot2 maps label

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

Label points in geom_point

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

中的标签功能

ggplot centered names on a map

由于我已经尝试了很长时间,所以我决定提出这个问题,并希望这里的某个人可以给我最终推动我想要的东西。我的绘图功能:

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")

它给了我错误:ggplot2不知道如何处理class 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

2 个答案:

答案 0 :(得分:14)

尝试这样的事情?

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

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

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

  4. Based on this example我读了一张世界地图,提取了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() 
    

    Population map of South America

    次要技术说明:实际上是coordinates in the sp package doesn't quite find the centroid, but it should usually give a sensible location for a label。如果要在更复杂的情况like non-contiguous shapes中标记真实质心,请在gCentroid包中使用rgeos

答案 1 :(得分:3)

这里接受的答案可能有效,但实际问题明确指出存在错误&#34; ggplot2不知道如何处理班级不平等的数据。&#34;

它给你错误的原因是因为包含centroids.df需要是一个命名变量(例如伴随着&#34; data =&#34;)

目前:

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")

应该是(注意:&#34; data = centroids.df&#34;):

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

此问题已在此处解决:How to deal with "data of class uneval" error from ggplot2?