代码未运行shapefile

时间:2014-06-10 06:42:05

标签: r map ggplot2

这是他已经讨论here的问题的扩展。

运行时的代码

data <- structure(list(Row.Labels = c("Andhra Pradesh", "ASSAM", "Bihar", 
              "Chandigarh", "CHHATTISGARH", "DADRA & NAGAR HAVELI", "DAMAN & DIU", 
              "Delhi", "GOA", "GUJARAT", "Haryana", "Himachal", "Jharkhand", 
              "KARNATAKA", "KERALA", "MAHARASHTRA", "MANIPUR", "MEGHALAYA", 
              "MP", "Odissa", "PONDICHERRY", "Punjab", "Rajasthan", "TAMIL NADU", 
              "TRIPURA", "UP", "Uttrakhand", "WEST BENGAL"), LATITUDE = c(78.3, 
              91.5, 85.13, 76.79855, 81.63, 72.96667, 72.8064, 72.8064, 73.96992, 
              72.4, 75.95947, 75.95947, 85.33, 75.68481, 76.82739, 75.64087, 
              93.58, 91, 93, 77.21067, 79.82803, 75.5, 75.52, 88.4, 91.25, 
              91.25, 78.2, 88.24), LONGITUDE = c(17.200001, 26.09, 25.370001, 
              30.744196, 21.23, 20.266666, 20.25189, 20.25189, 15.384293, 23.030001, 
              29.017748, 29.017748, 23.35, 14.849231, 9.470736, 19.590844, 
              24.440001, 25.299999, 23.299999, 28.623932, 11.937899, 30.4, 
              26.549999, 27.200001, 23.5, 23.5, 30.110001, 22.34), MAJORITY = c("Yes", 
              "No", "No", "No", "No", "Yes", "No", "No", "Yes", "No", "No", 
              "No", "No", "Yes", "Yes", "Yes", "No", "No", "No", "No", "No", 
              "No", "No", "Yes", "No", "No", "No", "No")), .Names = c("Row.Labels", 
              "LATITUDE", "LONGITUDE", "MAJORITY"), class = "data.frame", row.names = c(NA, -28L))

library(raster); library(ggplot2)
india <- getData('GADM', country="IND", level=1) 
f_india <- fortify(india)
i <- sapply(india@data$NAME_1, function(x) agrep(x, data$Row.Labels, max.distance=.3, ignore.case=T)[1]) 
india@data$maj <- data$MAJORITY[i]
f_india <- merge(x=f_india, y=unique(india@data), by.x="id", by.y="ID_1",all.x=T) 
f_india <- f_india[with(f_india, order(id, order)), ] # to prevent this https://stackoverflow.com/questions/24039621/code-not-working-for-other-shp-files
ggplot(f_india, aes(x=long, y=lat, group=group, fill=maj)) + 
  geom_polygon(colour="black") 

图表来自:India

在shp文件(来自here的第一个链接)

上运行时的代码相同

出来是: enter image description here

哪个错了!!请帮助我使用shp文件获取第一张地图。

1 个答案:

答案 0 :(得分:2)

代码试图匹配ID值和行号,这很脆弱。我怀疑这些身份证号值是“官方的”#39;您可以依赖的标识符来匹配数据集。美国有一套&#34; FIPS&#34;您可以用来匹配正确编码数据集中的状态和县等的标识符,但我在这里看不到这样的事情。 gadm数据有一个PID可能是某种东西,但是你的数据集或你指向我们的shapefile中没有匹配的PID。

您唯一可以依赖的是区域名称,而那些区域名称的变体使得精确匹配很难。

此外,请不要尝试使用强化内容,直到您的地图对象中有一个额外的列包含您要映射的变量。至少那时你可以绘制并在正确的地方检查它。

如果您的地图和数据中的列彼此一致,那么就像以下一样简单:

require(rgdal)
ishape = readOGR(".","india_state")
ishape$MAJORITY = factor(data$MAJORITY[match(tolower(ishape$NAME), tolower(data$Row.Labels))])
spplot(ishape, "MAJORITY")

enter image description here

即使在我将所有内容压平为小写字母之后,还有许多名称不匹配的空白区域。修复它们并重复。因此,您要映射的内容已经在地图数据中,因此使用ggplot进行映射更为直接。

您可以看到哪些与之匹配:

> unique(as.character(ishape@data[is.na(ishape$MAJORITY),"NAME"]))
 [1] "Daman(Daman&Diu)"    "Sikkim"              "Himachalpradesh"    
 [4] "Jammu & Kashmir"     "Rajastan"            "Madhya Pradesh"     
 [7] "Uttaranchal"         "Uttar Pradesh"       "Mizoram"            
[10] "Arunachal Pradesh"   "Nagaland"            "Orissa"             
[13] "Laksha Dweeps"       "Andaman and Nicobar"

纠正那些(拉贾斯坦/拉贾斯坦邦等),你会得到更全面的地图。