【发布时间】:2014-06-04 14:04:24
【问题描述】:
here 中提出的问题已解决,但不知何故不适用于其他 shp 文件。知道为什么吗? 我一直在尝试使用来自Coloring the states according to a given variable using ggplot(第一个链接)的 shp 文件
代码:
library(maptools)
library(ggplot2)
download.file("http://geocommons.com/overlays/204743.zip", destfile="204743.zip")
dir.create("ind")
unzip("204743.zip", exdir="ind")
india <- readShapeSpatial("ind/india_state.shp")
f_india <- fortify(india)
i <- sapply(india@data$NAME, 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=india@data, by.x="id", by.y="ID")
ggplot(f_india, aes(x=long, y=lat, group=group, fill=maj)) +
geom_polygon(colour="black")
错误的输出:
任何帮助将不胜感激。
【问题讨论】:
-
您的
merge可能弄乱了数据的顺序。尝试在它之前创建一个索引并在merge之后重新排序数据。 -
只是为了确认上述内容 - 合并肯定是问题所在。如果你 ggplot 只是印度的多边形,那么 f_india 你会看到问题。