R SpatialPolygonsDataFrame与Regular Dataframe合并

时间:2017-03-22 22:53:50

标签: r visualization geospatial polygons choropleth

我一直在使用ggplot创建一个等值区并将country / county文件下载为SpatialPolygonsDataFrame

countyData <- read.csv('County_data.csv')
readin =  readRDS('IRL_adm1.rds') 

然后我使用ggplot

创建了一个基本地图
basemap = ggplot(readin) +theme_minimal()+  
  geom_polygon( aes(x = long, y = lat, group = group),
                color = "white",  
                fill = "#9C9797")+ 
  coord_map()

到目前为止我没有遇到任何问题,并且创建了一个地图,但我想将一些数据与SpatialPolygonsDataFrame结合起来,以便能够为地图上的每个县着色

我尝试使用merge合并两个 -

combine= merge(readin,countyData, by="NAME_1")

然后创建最终的等值线 -

final_map = ggplot(combine)+ theme_minimal()+
  geom_polygon( aes(x = long, y = lat, group = group, fill= combine$Data),   #### I also tried combine@data$Data - but got the same error message ## Data being the vector I want to use to color the counties
                color = "grey", alpha = 1/5) +
  coord_map()+
  scale_fill_distiller(name = "Number of queries per county",  legend
                       palette = "Spectral")+ 
  theme(legend.position = "bottom")  

但是当我尝试显示地图时,我收到以下错误 -

Error: Aesthetics must be either length 1 or the same as the data (169288): x, y, group, fill

'combine'是正确的数据类型 - SpatialPolygonsDataFrame,当我检查组合@ data时,'Data'向量已被添加到它但我不确定为什么它看起来像是错误的长度

0 个答案:

没有答案