这是他已经讨论过的问题的扩展here。代码没有运行shp文件
运行
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([email protected]$NAME_1, function(x) agrep(x, data$Row.Labels, max.distance=.3, ignore.case=T)[1])
[email protected]$maj <- data$MAJORITY[i]
f_india <- merge(x=f_india, y=unique([email protected]), 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")
当该图出来是该代码:
当对SHP文件正在运行相同的代码(从here第一连杆)
出来成为:
这是错误的!请使用shp文件帮助我获得第一张地图。
谢谢!工作! – user2458552