2015-02-09 5 views
0

Рассмотрим следующий пример из ggmap виньеткаЗаполните многоугольников с помощью ggmap

library(ggplot2) 
library(ggmap) 
library(rgeos) 
library(sp) 
library(maptools) 

# get an example shape file 
download.file('http://www2.census.gov/geo/tiger/PREVGENZ/tr/tr00shp/tr48_d00_shp.zip', destfile = 'census.zip') 

# unzip 
unzip("census.zip"); 

# read data into R 
shapefile <- readShapeSpatial("tr48_d00.shp", proj4string = CRS("+proj=longlat +datum=WGS84")) 

# convert to a data.frame for use with ggplot2/ggmap and plot 
data <- fortify(shapefile) 
qmap("texas", zoom = 6, maptype = "satellite") + geom_polygon(aes(x = long, y = lat, group = group), 
data = data, colour = "white", fill = "black", alpha = .4, size = .3) 

Теперь вопрос:

Я хочу использовать переменную для заполнения многоугольников, например:

# define a vector for the fill aestetic 
fill_colour <- as.factor(1:length(shapefile)) 
# plot 
qmap("texas", zoom = 6, maptype = "satellite") 
    + geom_polygon(aes(x = long, y = lat, group = group, fill = fill_colour), data = data, alpha = .4,size = .3) 

Но это не работает, я получаю следующую ошибку:

Error: Aesthetics must either be length one, or the same length as the dataProblems:fill_color 
+0

Я попробовал ссылку. Но кажется, что zip-файл больше не доступен. – jazzurro

+0

Спасибо, что заметили это, я отремонтировал ссылку и упростил код – Grigory

ответ

0

Я нашел решение, используя geom_map получая аналогичный результат:

fill_colour <- as.factor(1:length(shapefile)) 
qmap("texas", zoom = 6, maptype = "satellite") + geom_map(inherit.aes = FALSE, aes(fill=fill_colour, map_id = 1:length(shapefile)), map=data) + scale_fill_manual(values = c("0" = "red", "1" = "blue")) 
Смежные вопросы