R 使用ggmap填充多边形

R 使用ggmap填充多边形,r,ggplot2,fill,ggmap,R,Ggplot2,Fill,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

考虑以下来自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)

我使用geom_地图找到了一个解决方案,得到了类似的结果:

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

fill\u color我尝试了链接。但是zip文件似乎不再可用。谢谢你注意到这一点,我修复了链接并简化了代码
Error: Aesthetics must either be length one, or the same length as the dataProblems:fill_color
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"))