gpt4 book ai didi

r - R ggplot中 map 多边形的标签中心

转载 作者:行者123 更新时间:2023-12-04 00:47:07 24 4
gpt4 key购买 nike

我正在尝试通过在 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 不知道如何处理 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

最佳答案

试试这样的东西?


  • 原始 map 对象。
  • 在您正在绘制的数据框中,确保有列
    您要标记的 ID,以及它们的经度和纬度
    质心。
  • 在 ggplot 中使用 geom_text 添加标签。

  • 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 .使用 gCentroidrgeos如果您想在更复杂的情况下在真实质心处进行标记,请打包 like non-contiguous shapes .

    关于r - R ggplot中 map 多边形的标签中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22038640/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com