【问题标题】:Plotting text labels over geom_polygon data in ggmap in R在 R 中的 ggmap 中的 geom_polygon 数据上绘制文本标签
【发布时间】:2015-06-11 06:09:43
【问题描述】:

我正在尝试使用 ggmap 制作具有三层的地图。层数如下:

  1. 美国地图(toner-lite)
  2. 一组几何图形,为某个值的状态着色(下面的模拟数据)
  3. 状态名称的标签,作为每个状态中心的注释。

为此,我创建了一张美国各州地图,其中各州用随机值 (rnorm) 着色,这部分是成功的。从这里我尝试使用 geom_text 在每个州中心的经纬度坐标处打印每个州的缩写。失败的部分是“geom_text”覆盖,出现以下错误:

错误:“x”和“单位”的长度必须 > 0 另外:警告 消息:1:在 gpclibPermit() 中:对 gpclib 的支持将是 在下一个主要版本 2 中从 maptools 中退出:删除 855070 包含缺失值的行 (geom_text)。

这是我自己努力运行的脚本。它将下载 shapefile 和状态数据中心,以及模拟数据以填充状态。我已经对其进行了测试,它可以达到我注释掉的内容(geom_text 层)。

我已经搜索过这个问题的答案,所以如果您对如何做我正在尝试的事情有任何建议,请告诉我。如果有更好的策略将标签放置在多边形填充顶部,我会全神贯注(或者在这种情况下是眼睛)。

###Combining Census data with a tract poly shapefile
library(maptools)
library(ggplot2)
library(gpclib)
library(ggmap)
library(rgdal)
library(dplyr)

#Set working directory to where you want your files to exist (or where they already exist)
setwd('~/Documents/GIS/USCensus/')
#Read and translate coord data for shape file of US States
if(!file.exists('tl_2014_us_state.shp')){
        download.file('ftp://ftp2.census.gov/geo/tiger/TIGER2014/STATE/tl_2014_us_state.zip',
                      'tl_2014_us_state.zip')
        files <- unzip('tl_2014_us_state.zip')
        tract <- readOGR(".","tl_2014_us_state") %>% spTransform(CRS("+proj=longlat +datum=WGS84"))
} else {
        tract <- readOGR(".","tl_2014_us_state") %>% spTransform(CRS("+proj=longlat +datum=WGS84"))
}

#two column dataset of state abbreviations and center of state
#Downloadable from: https://dev.maxmind.com/static/csv/codes/state_latlon.csv
if(!file.exists('state_latlon.csv')){
        download.file('http://dev.maxmind.com/static/csv/codes/state_latlon.csv','state_latlon.csv')
}
centers <- read.csv('state_latlon.csv')
#Change values of longitude and latitude from state center data so as not to interfere with shapefile at merge
names(centers)[2:3] <- c('long_c','lat_c')

#simulated data for plotting values
mydata<- data.frame(rnorm(55, 0, 1)) #55 "states" in the coord dataset for state centers
names(mydata)[1] <- 'value'

#hold names in tract dataset and for simulated data
ntract<-names(tract)
ndata<-names(mydata) 

#Turn geo data into R dataframe
gpclibPermit()
tract_geom<-fortify(tract,region="STUSPS")

#Merge state geo data with simulated data
state_data <- cbind(centers,mydata)
#merge state center and value data with shapefile data
tract_poly <- merge(state_data,tract_geom,by.x="state",by.y="id", all = F) 
tract_poly<-tract_poly[order(tract_poly$order),]

#Create map of US
mymap <- get_stamenmap(bbox = c(left = -124.848974,
                                bottom = 24.396308,
                                right = -66.885444,
                                top = 49.384358),zoom=5,
                       maptype="toner-lite")

#This plots a map of the US with just the state names as labels (and a few other landmarks). Used for reference
USMap <- ggmap(mymap,extent='device') +
        geom_polygon(aes(x = long, y = lat, group = group, fill = value),
                     data = tract_poly,
                     alpha = 1, 
                     color = "black",
                     size = 0.2) #+
#         geom_text(aes(x = long_c, y = lat_c, group = group, label = state),
#                   data= tract_poly,
#                   alpha = 1,
#                   color = "black")

USMap

【问题讨论】:

  • 这不是错误的来源,但您不应使用tract_poly 作为geom_text 的数据框。对于geom_text,每个州都需要一行,但tract_poly 每个州都有数千行(每个州边界段都有一行)。例如,您可以使用data=tract_poly %&gt;% group_by(state) %&gt;% slice(1)

标签: r overlay gis ggmap annotate


【解决方案1】:

这是一个奇怪的错误消息,最终导致了问题。一路上的某个地方,您已经翻转了中心的纬度和经度。 (我还考虑了上面elpi的建议,并没有直接使用您的中心数据集重复绘制缩写)。下面的代码有效,但我建议修复您的中心数据集。

centers$new_long <- centers$lat_c
centers$new_lat <- centers$long_c
USMap <- ggmap(mymap,extent='device') +
        geom_polygon(aes(x = long, y = lat, group = group, fill = value),
                     data = tract_poly,
                     alpha = 1, 
                     color = "black",
                     size = 0.2) +
         geom_text(aes(x = new_long, y = new_lat, label = state),
                   data= centers,
                   alpha = 1,
                   color = "black")

【讨论】:

  • 谢谢大家的建议。我现在正在解决这个问题,这一切都说得通。从我读到的内容来看,该错误可能是由于绘图超出了可映射范围,这使得 lon/lat 坐标被翻转。我有时间让它工作后会更新。
  • 考虑到 GregF 的修复(考虑到 elpi 的建议),我能够让它工作。问题出在names(centers)[2:3] &lt;- c('long_c','lat_c') 行中,它通过重命名来反转长/纬度。它被替换为:names(centers)[2:3] &lt;- c('lat_c','long_c') 然后我使用了 GregF 的地图添加 geom_text(aes(x = new_long, y = new_lat, label = state), data= centers, alpha = 1, color = "black") 一切正常
【解决方案2】:

试试这个

centroids <- setNames(do.call("rbind.data.frame", by(tract_poly, tract_poly$group, function(x) {Polygon(x[c('long', 'lat')])@labpt})), c('long', 'lat')) 
centroids$label <- tract_poly$state[match(rownames(centroids), tract_poly$group)]
USMap + with(centroids, annotate(geom="text", x = long, y=lat, label = label, size = 2.5))

(via)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多