GGR图中地图多边形中心的标注

GGR图中地图多边形中心的标注,r,ggplot2,maps,label,R,Ggplot2,Maps,Label,我正在尝试使用R中的ggplot标记我的多边形。我在stackoverflow上找到了一个主题,我认为它非常接近我想要的,除了点 我在网上找到了一些方法。现在我首先需要找到每个形状的中心位置,然后我必须把这些位置和名称放在一起。然后将其链接到geom_text()中的标签函数 由于我已经尝试了很长一段时间,现在我决定问这个问题,并希望这里的人能给我的最后推动我想要什么。我的绘图功能: region_of_interest.fort <- fortify(region_of_intere

我正在尝试使用R中的ggplot标记我的多边形。我在stackoverflow上找到了一个主题,我认为它非常接近我想要的,除了点

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

由于我已经尝试了很长一段时间,现在我决定问这个问题,并希望这里的人能给我的最后推动我想要什么。我的绘图功能:

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

试试这样的

  • 从中获取多边形质心的数据帧 原始地图对象

  • 在要打印的数据框中,确保有用于打印的列 要标记的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() 
    
  • 在ggplot中使用geom_文本添加标签

  • 我读了一张世界地图,提取了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() 
    
    library(rgdal)#用于读取世界地图数据
    图书馆(rgeos)#在不需要gpclib的情况下进行加固
    图书馆(地图工具)
    图书馆(GG2)
    库(比例)#用于使用逗号格式化ggplot比例
    #数据来自http://thematicmapping.org/downloads/world_borders.php.
    #直接链接:http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
    #解包并将文件放入目录“数据”
    
    worldMap这里接受的答案可能有效,但实际提出的问题特别指出有一个错误“ggplot2不知道如何处理uneval类的数据。”

    给出错误的原因是因为包含的centroids.df需要是一个命名变量(例如,伴随“data=”)

    目前:

    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")
    
    应为(注意:“数据=质心.df”):


    这个问题在这里得到了解决:

    我们可以用一个您使用的数据示例来回答您的问题。你可以在这里获得更多信息:我感谢你的评论。作为R的初学者,你链接到的页面对我来说非常困难。但是我想我有一个相当清楚的问题,肯定有人已经做过了。当我排除标签时,函数不会打印。你可以想象空的形状,形状或数据并不重要。现在我想在里面贴个标签。做你想做的。谢谢你的评论。我正在尝试使用你的脚本,这一切对我来说似乎非常合乎逻辑,我只需要让它工作。在实现你的代码后,我已经修改了原来的问题。我想我有你在评论中提到的问题。解决方法是不读取强化DF的ID和原始地图的质心。而是使用上面代码中的@data。希望这有帮助。非常感谢你一直以来对我的帮助!我知道了,它很漂亮!如果你在ggplot中生产叶绿体,你也可能对它感兴趣
    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")