我已按如下方式绘制地图。我需要帮助添加一些额外的图层。我知道有很多方法可以做到这一点,但作为ggplot的新手,我真的无能为力。任何帮助将非常感谢。谢谢
我使用过代码:
shp_state1<-readShapeSpatial("...")
mapindia<-fortify(shp_state1)
q=ggplot(data=mapindia, aes(long, lat, group=group,colour)) + geom_polygon(fill="#FF9999", colour="black") +coord_map()
现在我有以下数据(作为数据框):
Row.Labels LATITUDE LONGITUDE MAJORITY
1 Andhra Pradesh 78.30000 17.200001 Yes
2 ASSAM 91.50000 26.090000 No
3 Bihar 85.13000 25.370001 No
4 Chandigarh 76.79855 30.744196 No
5 CHHATTISGARH 81.63000 21.230000 No
6 DADRA & NAGAR HAVELI 72.96667 20.266666 Yes
7 DAMAN & DIU 72.80640 20.251890 No
8 Delhi 72.80640 20.251890 No
9 GOA 73.96992 15.384293 Yes
10 GUJARAT 72.40000 23.030001 No
11 Haryana 75.95947 29.017748 No
12 Himachal 75.95947 29.017748 No
13 Jharkhand 85.33000 23.350000 No
14 KARNATAKA 75.68481 14.849231 Yes
15 KERALA 76.82739 9.470736 Yes
16 MAHARASHTRA 75.64087 19.590844 Yes
17 MANIPUR 93.58000 24.440001 No
18 MEGHALAYA 91.00000 25.299999 No
19 MP 93.00000 23.299999 No
20 Odissa 77.21067 28.623932 No
21 PONDICHERRY 79.82803 11.937899 No
22 Punjab 75.50000 30.400000 No
23 Rajasthan 75.52000 26.549999 No
24 TAMIL NADU 88.40000 27.200001 Yes
25 TRIPURA 91.25000 23.500000 No
26 UP 91.25000 23.500000 No
27 Uttrakhand 78.20000 30.110001 No
28 WEST BENGAL 88.24000 22.340000 No
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))
我怎样才能按照&#34;是&#34;或&#34;不&#34; ?
答案 0 :(得分:4)
考虑到您的数据存储在名为data
的数据框中,这是一种方式:
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 http://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")
您可能需要调整i
,它将数据框中的名称与地图数据中的名称相关联,以将投票输入地图数据。