使用ggplot根据给定的变量着色状态 [英] Coloring the states according to a given variable using ggplot

查看:218
本文介绍了使用ggplot根据给定的变量着色状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经绘制了地图如下。我需要帮助添加一些额外的层。我知道有很多方法可以做到这一点,但作为ggplot的新手,我无法超越这一点。任何帮助将不胜感激。thanks




我使用了代码:

  shp_state1< -readShapeSpatial(...)
mapindia< -fortify(shp_state1)
q = ggplot(data = mapindia,aes(long,lat,group = group,color))+ geom_polygon(fill =#FF9999,color =black)+ coord_map )

现在我有以下数据(作为数据框):

  Row.Labels LATITUDE LONGITUDE MAJORITY 
1安得拉邦78.30000 17.200001是
2 ASSAM 91.50000 26.090000否
3比哈尔85.13000 25.370001否
4昌迪加尔76.79855 30.744196否
5 CHHATTISGARH 81.63000 21.230000否
6 DADRA& NAGAR HAVELI 72.96667 20.266666是
7 DAMAN& DIU 72.80640 20.251890否
8德里72.80640 20.251890否
9 GOA 73.96992 15.384293是
10 GUJARAT 72.40000 23.030001否
11哈里亚纳纳75.95947 29.017748否
12喜马偕尔75.95947 29.017748否
13 Jharkhand 85.33000 23.350000否
14 KARNATAKA 75.68481 14.849231是
15 KERALA 76.82739 9.470736是
16 MAHARASHTRA 75.64087 19.590844是
17 MANIPUR 93.58000 24.440001否
18 MEGHALAYA 91.00000 25.299999否
19 MP 93.00000 23.299999否
20 Odissa 77.21067 28.623932否
21 PONDICHERRY 79.82803 11.937899否
22旁遮普邦75.50000 30.400000否
23拉贾斯坦邦75.52000 26.549999否
24 TAMIL NADU 88.40000 27.200001是的
25 TRIPURA 91.25000 23.500000否
26 UP 91.25000 23.500000否
27 Uttrakhand 78.20000 30.110001否
28 WEST BENGAL 88.24000 22.340000否


数据< - 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 ,MEGHALAYA,
MP,Odissa,PONDICHERRY,Punjab,Rajasthan,TAMIL NADU,
TRIPURA,UP,Uttrakhand 西元),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,
MAKEORITY = c(是,
否,否,否,否,是,否,否,是,否,否,否,否,否,否,否 ,否,否,
否,否,是,否,否,否,否)),.Names = c .Labels,
LATITUDE,LONGITUDE,MAJORITY),class =data.frame,row.names = c(NA,-28L))

如何根据是或否对sats进行颜色?

解决方案

考虑到您的数据存储在名为数据的数据框中,以下是一种方式:

 库(raster);图书馆(ggplot2)
india< - getData('GADM',country =IND,level = 1)
f_india< - fortify(india)
i< - sapply @ data $ NAME_1,function(x)agrep(x,data $ Row.Labels,max.distance = .3,ignore.case = T)[1])$ ​​b $ b 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)),]#以防止这个http://stackoverflow.com/questions/24039621/code-not-working-for-other-shp-文件
ggplot(f_india,aes(x = long,y = lat,group = group,fill = maj))+
geom_polygon(color =black)



您可能需要调整 i ,它将数据框中的名称与地图数据中的名称相连接将投票拉入地图数据。


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

解决方案

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.

这篇关于使用ggplot根据给定的变量着色状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆