2015-04-06 111 views
2

我正在嘗試使用ggmap製作包含三個圖層的地圖。這些層如下:通過ggmap在geom_polygon數據中繪製文本標籤R

  1. 美國(調色劑精簡版)的地圖
  2. 一組色一些值的狀態的幾何形狀(下面模擬數據)
  3. 標籤的狀態名稱,作爲每個州的中心的註釋。

要做到這一點,我創建了一個美國各州的地圖,其中各州的顏色是隨機值(rnorm),這部分是成功的。從這裏我試圖使用geom_text在每個州的中心的經度和緯度座標上打印每個州的縮寫。失敗的部分是「geom_text」覆蓋,並出現以下錯誤:

Error: 'x' and 'units' must have length > 0 In addition: Warning messages: 1: In gpclibPermit() : support for gpclib will be withdrawn from maptools at the next major release 2: Removed 855070 rows containing missing values (geom_text).

這裏是劇本,我一直在努力爲獨立運行。它將下載形狀文件和狀態數據中心,以及模擬數據以填充狀態。我已經測試過它,它適用於我已經註釋掉的內容(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 
+0

這不是錯誤的來源,但不應該使用'tract_poly'作爲'geom_text'的數據框。對於'geom_text',每個狀態需要一行,但是'tract_poly'對於每個狀態都有數千行(每個狀態邊界有一行)。例如,你可以使用'data = tract_poly%>%group_by(state)%>%slice(1)'。 – eipi10

回答

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

謝謝大家的建議。我現在正在修復這個問題,這一切都是有道理的。從我所讀到的內容來看,這個錯誤可能是由於繪圖超出了可映射範圍,這將使得lon/lat座標被翻轉。在我有時間讓它工作後更新。 –

+0

考慮到格雷格的修復(考慮到elpi的建議),我能夠得到這個工作。問題出在 'name(center)[2:3] < - c('long_c','lat_c')' 這是通過重命名反轉long/lat。它是由取代: '名稱(中心)[2:3] < - C( 'lat_c', 'long_c')' 我然後用於GregF的地圖此外 'geom_text(AES(X = new_long,Y = new_lat,label = state), data = center, alpha = 1, color =「black」)' 而且一切正常 –

1

試試這個

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