Coloring the states according to a given variable

2019-07-21 21:29发布

问题:

I have plotted the map as follows. I need help with adding some extra layer. I know there are many ways to do this, but as a newbie to ggplot I am really clueless beyond this point. Any help would be much appreciated.thanks

I have used the code:

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()

now i have the following data(as data frame):

             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))

How can I colour the sates according as "yes" or "no" ?

回答1:

Considering your data is stored in a data frame named data, here's one way:

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") 

You may want to adjust i which connects the names from your data frame with the names from the map data to pull in the votes into the map data.



标签: r map ggplot2