我正在嘗試使用ggmap製作包含三個圖層的地圖。這些層如下:通過ggmap在geom_polygon數據中繪製文本標籤R
- 美國(調色劑精簡版)的地圖
- 一組色一些值的狀態的幾何形狀(下面模擬數據)
- 標籤的狀態名稱,作爲每個州的中心的註釋。
要做到這一點,我創建了一個美國各州的地圖,其中各州的顏色是隨機值(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
這不是錯誤的來源,但不應該使用'tract_poly'作爲'geom_text'的數據框。對於'geom_text',每個狀態需要一行,但是'tract_poly'對於每個狀態都有數千行(每個狀態邊界有一行)。例如,你可以使用'data = tract_poly%>%group_by(state)%>%slice(1)'。 – eipi10