使用ggplot2在地图上绘制条形图 [英] Plotting bar chart on map using ggplot2

查看:347
本文介绍了使用ggplot2在地图上绘制条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用 ggplot2 来绘制地图上的条形图。

  library(ggplot2)
q = ggplot(data = mapindia,aes(long,lat,group = group,color))+ geom_polygon(fill =#FF9999,color =black)+ coord_map()
q = q + geom_point(aes(x = LATITUDE,y = LONGITUDE,group = state_name),data = religion)+ coord_map()
q < - q + geom_subplot2d(aes(long,lat,subplot = geom_bar(aes(value,fill = fact))),ref = NULL,width = rel(0.8),data = simdat)
print(q)

但出现以下错误:

  ##使用binwidth 2.12 
##使用binwidth 2.13
##不知道如何自动选择data.frame类型的对象的缩放比例。默认为连续
## eval中的错误(expr,envir,enclos):找不到对象'group'

我的数据集

  dput(head(mapindia,100))
structure(list(long = c(72.94823,72.948775,72.96202,72.964302,
72.968211,72.970708,72.964092,72.956172,72.943367,72.931651,
72.93708,72.948365,72.947281,72.938496,72.930794,72.924501,
72.918209,72.910397 ,72.893036,72.879257,72.878837,72.880537,
72.882884,72.882518,72.885103,72.88869,72.892337,72.89675,
72.904402,72.911528,72.924481,72.944418,72.950934,72.955821,
72.962662,72.95561,72.94823 ,73.043316,73.042348,73.040185,
73.035095,73.028379,73.024913,73.022964,73.022209,73.025029,
73.032294,73.03815,73.043791,73.046396,73.047159,73.053124,
73.066142,73.080464,73.092181,73.105632 ,73.114977,73.134375,
73.147935,73.15217,73.156084,73.16586,73.178224,73.182115,
73.182861,7 3.181982,73.182834,73.190533,7320268,73.213414,
73.227186,73.235096,73.237463,73.230614,73.219987,73.211622,
73.210741,73.202164,73.190022,73.177,73.165391,73.150641,$ b $ 73.1438,73.13869, 73.133474,73.13281,73.136697,73.160516
73.178507 73.192372 73.206024 73.208846 73.214068 73.225464 $ b $ 73.236084 73.258275 73.268327 73.272309 73.269801 73.265777
)lat = c(20.466602 ,20.459483,20.44602,20.43615,20.424953,
20.412235,20.398937,20.402725,20.410493,20.404022,20.392923,
20.385058,20.379266,20.374414,20.375353,20.377813,20.380272,
20.382634,20.390107,20.390654 ,20.405481,20.415684,20.428257,
20.443762,20.453246,20.460464,20.466909,20.47418,20.482435,
20.486774,20.501579,20.496404,20.491097,20.487117,20.477538,
20.469934,20.466602,20.219381,20.232198 ,20.240647,20.24748,
20.255641,20.261145,20.266745,20.276713,20.279753,20.284505,
20.287736,20.2938 20.34353,20.343433,20.343213,20.346733,20.349013,
20.353571,20.362402,20.374462,20.372372,20.361166,20.351196,
20.342554,20.331159,20.351396,20.351396, 20.330208,20.330965,20.330202,20.329626,
20.325824,20.314522,20.304079,20.303419,20.292882,20.28424,
20.276552,20.275796,20.266398,20.258519,20.251874,20.241429,
20.228231,20.216457, 20.193763,20.18094,20.184923,
20.182923,20.183772,20.186809,20.198582,20.209307,20.209965,
20.198458,20.186199,20.173564,20.166254,20.15885),命令= C(1L,
2L,3L ,4L,5L,6L,7L,8L,9L,10L,11L,12L,13L,14L,15L,
16L,17L,18L,19L,20L,21L,22L,23L,24L,25L,26L ,27L,28L,
29L,30L,31L,32L,33L,34L,35L,36L,37L,1L,2L,3L,4L,
5L,6L,7L,8L,9L,10L ,11L,12L,13L,14L,15L,16L,17L,18L,
19L,20L,21L,22L,23L,24L,25L,26L,27L,28L,29L,30L,31L, b 32L,33L,34L,35L,3 6L,37L,38L,39L,40L,41L,42L,43L,44L,
45L,46L,47L,48L,49L,50L,51L,52L,53L,54L,55L,56L,57L,孔= c(假,假,假,
假, b FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE ,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE ,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE ,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,
FALSE ,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,图1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, ),.Label = c(1,2),class =factor),group = structure(c(1L,
1L,1L,1L,1L,1L,1L,1L,1L 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, ,1L,
1L,1L,1L,1L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
2L,2L,2L,2L ,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L, ,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L, (2,2L),.Label = c(0.1,1.1,2.1,3.1,4.1,5.1,
5.2,6.1 7.1,8.1,9.1,10.1,11 1,12.1,13.1,
14.1,15.1,16.1,17.1,18.1,19.1,20.1,21.1,
22.1,23.1,24.1,25.1,26.1,27.1,28.1,29.1,
30.1,31.1,32.1 33.1,34.1,35.1,36.1,37.1,
38.1,39.1,40.1,41.1,42.1,43.1,44.1 ,45.1,
46.1,47.1,48.1,49.1,50.1,51.1,52.1,53.1,
54.1, 55.1,56.1,57.1,58.1,59.1,60.1,61.1,
62.1,63.1,64.1,65.1,66.1 ,67.1,68.1,69.1,
70.1,71.1,71.2,72.1,73.1,74.1,75.1,76.1
77.1,78.1,79.1,80.1,81.1,82.1,83.1,84.1,
85.1,86.1,87.1 ,87.2,88.1,88.2,89.1,90.1,
91.1,92.1,93.1,94.1,95.1,96.1 97.1,98.1,
99.1,100.1,101.1,102.1,103.1,104.1,105.1,
106.1,107.1 ,108.1,109.1,110.1,111.1,112.1,
113.1,114.1,115.1,116.1,117.1,118.1 119.1,
120.1,121.1,122.1,123.1,124.1,125.1,126.1,
127.1,128.1,129.1,130.1 131.1,132.1,133.1,
134.1,135.1,136.1,137.1,138.1,139.1,140.1,
141.1 ,142.1,143.1,144.1,145.1,146.1,147.1,
148.1,149.1,150.1,151.1,152.1 153.1,154.1,
155.1,156.1,157.1,158.1,159.1,160.1,161.1,
162.1,163.1 ,164.1,165.1,166.1,167.1,168.1,
),class =factor),id = c(0,0,0, 0,0,0,0,
,0,0,0,0,0,0,0,0 0,0,0,0,0,
0,0,0,0,0,0, 0,0,0,0,0,0,0,
,0,0,0,0,1 1,1,1,1,1,1,1,1,1,
1,1 1,1,1,1,1,1,1,1,1,1,
1,1 1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1, ,
1,1,1,1, 1,1,1,1,1,1,1,1,1,
1,1)) .Names = c(long,lat,order,hole,piece,
group,id),row.names = c(NA,100L) =data.frame)

dput(head(simdat,100))
structure(list(state = structure(c(2L,2L,2L,2L,2L,2L, 2L,
2L,3L,3L,3L,3L,3L,3L,3L,3L,4L,4L,4L,4L,4L,4L,4L,
4L,5L,5L,5L, 5L,5L,5L,5L,5L,6L,6L,6L,6L,6L,6L,6L,
6L,7L,7L,7L,7L,7L,7L,7L,7L,8L,8L, 8L,8L,8L,8L,8L,
8L,9L,9L,9L,9L,9L,9L,9L,9L,10L,10L,10L,10L,10L, 10L,11L,11L,11L,11L,11L,11L,11L,11L,12L,12L,
12L,12L,12L,12L,12L,12L,13L,13L,13L,13L,13L,13L, 13L,
13L,14L,14L,14L,14L),.Label = c(,安得拉邦,ASSAM,
Bihar,Chandigarh,CHHATTISGARH, DADRA& NAGAR HAVELI,
DAMAN& DIU,Delhi,GOA,GUJARAT,Haryana,Himachal,
Jharkhand,KARNATAKA,KERALA,MAHARASHTRA,MANIPUR bMEGHALAYA,MP,Odissa,PONDICHERRY,Punjab,Rajasthan,
TAMIL NADU,TRIPURA,UP,Uttrakhand ),class =factor),
long = c(78.30000305,78.30000305,78.30000305,78.30000305,
78.30000305,78.30000305,78.30000305,78.30000305,91.5,
91.5,91.5,91.5,91.5 ,91.5,91.5,91.5,85.12999725,85.12999725,
85.12999725,85.12999725,85.12999725,85.12999725,85.12999725,
85.12999725,76.798553,76.798553,76.798553,76.798553,
76.798553,76.798553,76.798553,76.798553 ,81.62999725,
81.62999725,81.62999725,81.62999725,81.62999725,81.62999725,
81.62999725,81.62999725,72.96666718,72.96666718,72.96666718,
72.96666718,72.96666718,72.96666718,72.96666718,72.96666718,
72.806396 ,72.806396,72.806396,72806396,72806396,7 2.806396
72.806396 72.806396 72.806396 72.806396 72.806396 72.806396
72.806396 72.806396 72.806396 72.806396 73.96992109
73.96992109 73.96992109 73.96992109 73.96992109 73.96992109
73.96992109,73.96992109,72.40000153,72.40000153,72.40000153,
72.40000153,72.40000153,72.40000153,72.40000153,72.40000153,
75.959473,75.959473,75.959473,75.959473,75.959473,75.959473,
75.959473,75.959473,75.959473, 75.959473,75.959473,75.959473
75.959473,75.959473,75.959473,75.959473 85.33000183
85.33000183 85.33000183 85.33000183)lat = c(17.20000076,
17.20000076,17。 ,
17.20000076,17.20000076,26.09000015,26.09000015,26.09000015,
26.09000015,26.09000015,26.09000015,26.09000015,26.09000015,
25.37000084,25.37000084,25.37000084,25.37000084,25.37000084,
25.37000084,25.370000 84.23万元,30.744196,30.744196元,30.744196元,30.744196元,30.744196元,30.744196元,30.744196元,30.744196元,30.744196元,21.22999954元,21.22999954元,21.22999954元,21.22999954元,21.22999954元,21.22999954元,21.22999954元,21.22999954元,20.26666641元, 20.26666641
20.26666641,20.26666641,20.26666641,20.26666641,20.26666641
20.26666641,20.25189,20.25189,20.25189,20.25189,20.25189
20.25189 20.25189 20.25189 20.25189 20.25189 20.25189
20.25189,20.25189,20.25189,20.25189,20.25189,15.38429276,
15.38429276,15.38429276,15.38429276,15.38429276,15.38429276,
15.38429276,15.38429276,23.03000069,23.03000069,23.03000069,
23.03000069,23.03000069, 23.03000069,23.03000069,23.03000069,
29.017748,29.017748,29.017748,29.017748,29.017748,29.017748,
29.017748,29.017748,29.017748,29.017748,29.017748,29.017748,
29.017748,29.017748,29.017748,29.017748, 23.35000038,
23.35000038,23.35000038,23.35000038),值= c(134L,3L,
1098L,16645L,123L,2070L,37L,66L,2L,4131L,1L,21L,
0L,127L ,5L,651L,220L,260L,1827L,17596L,20L,0L,0L,
22L,16L,0L,2L,0L,18L,450L,0L,22L,30L,41L,56L, b 3L,53L,66L,11L,4674L,0L,28L,0L,1L,328L,3L,0L,
33L,0L,34L,5L,5L,11L,0L,440L,0L,0L,318L ,16L,122L,
2990L,35L,0L,44L,1L,671L,0L,43L,0L,302L,0L,0L,
0L,40L,10054L,379L,1492L,160L,77L ,385L,4348L,0L,
157L,59L,0L,12L,334L,159L,2013L,12L,0L,3L,29L,
0L,15L,16L,0L,49L,10L,329L ),变量=结构(列表(
fact = structure(c(1L,2L,3L,4L,5L,6L,7L,8L,1L,
4L,7L,8L,2L,3L, 5L,7L,3L,1L,6L,4L,8L,2L,7L,
6L,5L,4L,5L,1L,7L,8L,4L,6L,2L,3L,7L,6L,8L,
1L,5L,3L,4L,2L, 7L,6L,5L,8L,4L,1L,2L,3L,5L,
4L,8L,6L,7L,3L,2L,1L,2L,7L,4L,5L,6L,3L,8L,
1L,4L,7L,6L,3L,2L,5L,1L,8L,4L,3L,7L,5L,6L,
2L,8L,1L,2L,1L,5L,3L) ,标签= c(佛教,Budhist,
基督教,印度教,耆那教,穆斯林,其他,锡克教
),class =因子)),.Names =fact,row.names = c(1L,
29L,57L,85L,113L,141L,169L,197L,2L,86L,170L,198L,
30L,58L,114L,142L,59L,3L,143L,87L,199L,31L,171L,
115L,200L,172L,4L,32L,60L,88L,116L,144L,201L,173L, $ b 33L,61L,5L,145L,117L,89L,118L,6L,174L,202L,90L,$ b $ 146L,34L,62L,175L,147L,203L,7L,119L,63L,91L,35L,
176L,148L,120L,204L,92L,8L,36L,64L,121L,93L,205L,
149L,177L,65L,37L,9L,38L,178L,94L,122L,150L, 66L,
206L,10L,95L,179L,151L,67L,39L,123L,11L,207L,96L,$ b $ 68L,180L,124L,152L,40L,208L,12L, 41L,13L,125L,69L
),class =data.frame)),.Names = c(state,long,lat,
value,variable ),row.names = c(NA,100L),class =data.frame)


解决方案

ggsubplot有一个已知问题和R3.1.0。请参阅此此处在这里

我可以用ggplot绘制你的数据,但不能用ggsubplot绘制数据

  library(ggplot2)
df $ fact< - df $ variable $ fact
df $ state< - as.character(df $ state)
df< - df [,-5]
ggplot(df,aes(x = fact,y = value,fill = fact))+
geom_bar(stat ='identity',position ='dodge')+
facet_wrap(〜state)+
theme(axis.text.x = element_blank()
,axis.title.x = element_blank()
,axis.ticks.x = element_blank()
,strip.text = element_text(size = 8))



如果您想要表示您的数据与馅饼,你可以尝试像

  library(dplyr)
df1< - df%>%
group_by(stat e)%>%
mutate(tsum = sum(value),
prop = value / tsum)

ggplot(df1,aes(x = factor(1), y = prop,fill = fact1))+
geom_bar(stat =identity)+
coord_polar(theta ='y')+
facet_wrap(〜state,ncol = 4)+
主题(axis.text.x = element_blank()
,axis.title.x = element_blank()
,axis.text.y = element_blank()
,轴。 title.y = element_blank()
,axis.ticks = element_blank()
,strip.text = element_text(size = 8))



对于subplot来说,像这样的东西应该可以工作:

  library(maptools)
data(wrld_simpl)
india< - wrld_simpl [wrld_simpl $ name =='India',]
dfindia< - fortify(印度)
p< - ggplot()+
geom_polygon(data = dfindia,aes(x = long,y = lat,group = group))+
geom_coord()
p + geom_subplot(data = simdat,
aes(long,lat,group = state,
subplot = geom_bar(aes(x = fact,y = value,fill = state),
stat =身份)),宽度= 10,身高= 10)


I have to plot bargraphs on a map using ggplot2.

library(ggplot2)
q=ggplot(data=mapindia, aes(long, lat, group=group,colour)) + geom_polygon(fill="#FF9999", colour="black") +coord_map()
q=q+geom_point(aes(x=LATITUDE, y=LONGITUDE,group=state_name),data=religion)+coord_map()
q <- q+geom_subplot2d(aes(long,lat,subplot = geom_bar(aes(value,fill=fact))), ref = NULL, width = rel(0.8), data = simdat)
print(q)

But got the following error:

## Using binwidth 2.12
## Using binwidth 2.13
## Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
## Error in eval(expr, envir, enclos) : object 'group' not found

My data sets

dput(head(mapindia, 100))
structure(list(long = c(72.94823, 72.948775, 72.96202, 72.964302, 
72.968211, 72.970708, 72.964092, 72.956172, 72.943367, 72.931651, 
72.93708, 72.948365, 72.947281, 72.938496, 72.930794, 72.924501, 
72.918209, 72.910397, 72.893036, 72.879257, 72.878837, 72.880537, 
72.882884, 72.882518, 72.885103, 72.88869, 72.892337, 72.89675, 
72.904402, 72.911528, 72.924481, 72.944418, 72.950934, 72.955821, 
72.962662, 72.95561, 72.94823, 73.043316, 73.042348, 73.040185, 
73.035095, 73.028379, 73.024913, 73.022964, 73.022209, 73.025029, 
73.032294, 73.03815, 73.043791, 73.046396, 73.047159, 73.053124, 
73.066142, 73.080464, 73.092181, 73.105632, 73.114977, 73.134375, 
73.147935, 73.15217, 73.156084, 73.16586, 73.178224, 73.182115, 
73.182861, 73.181982, 73.182834, 73.190533, 73.20268, 73.213414, 
73.227186, 73.235096, 73.237463, 73.230614, 73.219987, 73.211622, 
73.210741, 73.202164, 73.190022, 73.177, 73.165391, 73.150641, 
73.1438, 73.13869, 73.133474, 73.13281, 73.136697, 73.160516, 
73.178507, 73.192372, 73.206024, 73.208846, 73.214068, 73.225464, 
73.236084, 73.258275, 73.268327, 73.272309, 73.269801, 73.265777
), lat = c(20.466602, 20.459483, 20.44602, 20.43615, 20.424953, 
20.412235, 20.398937, 20.402725, 20.410493, 20.404022, 20.392923, 
20.385058, 20.379266, 20.374414, 20.375353, 20.377813, 20.380272, 
20.382634, 20.390107, 20.390654, 20.405481, 20.415684, 20.428257, 
20.443762, 20.453246, 20.460464, 20.466909, 20.47418, 20.482435, 
20.486774, 20.501579, 20.496404, 20.491097, 20.487117, 20.477538, 
20.469934, 20.466602, 20.219381, 20.232198, 20.240647, 20.24748, 
20.255641, 20.261145, 20.266745, 20.276713, 20.279753, 20.284505, 
20.287736, 20.293816, 20.299704, 20.309768, 20.311575, 20.320982, 
20.333331, 20.339792, 20.343499, 20.343211, 20.346733, 20.349013, 
20.353571, 20.362402, 20.374462, 20.372372, 20.361166, 20.351196, 
20.342554, 20.331159, 20.330208, 20.330965, 20.330202, 20.329626, 
20.325824, 20.314522, 20.304079, 20.303419, 20.292882, 20.28424, 
20.276552, 20.275796, 20.266398, 20.258519, 20.251874, 20.241429, 
20.228231, 20.216457, 20.204968, 20.193763, 20.18094, 20.184923, 
20.182923, 20.183772, 20.186809, 20.198582, 20.209307, 20.209965, 
20.198458, 20.186199, 20.173564, 20.166254, 20.15885), order = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 
58L, 59L, 60L, 61L, 62L, 63L), hole = c(FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), piece = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("1", "2"), class = "factor"), group = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L), .Label = c("0.1", "1.1", "2.1", "3.1", "4.1", "5.1", 
"5.2", "6.1", "7.1", "8.1", "9.1", "10.1", "11.1", "12.1", "13.1", 
"14.1", "15.1", "16.1", "17.1", "18.1", "19.1", "20.1", "21.1", 
"22.1", "23.1", "24.1", "25.1", "26.1", "27.1", "28.1", "29.1", 
"30.1", "31.1", "32.1", "33.1", "34.1", "35.1", "36.1", "37.1", 
"38.1", "39.1", "40.1", "41.1", "42.1", "43.1", "44.1", "45.1", 
"46.1", "47.1", "48.1", "49.1", "50.1", "51.1", "52.1", "53.1", 
"54.1", "55.1", "56.1", "57.1", "58.1", "59.1", "60.1", "61.1", 
"62.1", "63.1", "64.1", "65.1", "66.1", "67.1", "68.1", "69.1", 
"70.1", "71.1", "71.2", "72.1", "73.1", "74.1", "75.1", "76.1", 
"77.1", "78.1", "79.1", "80.1", "81.1", "82.1", "83.1", "84.1", 
"85.1", "86.1", "87.1", "87.2", "88.1", "88.2", "89.1", "90.1", 
"91.1", "92.1", "93.1", "94.1", "95.1", "96.1", "97.1", "98.1", 
"99.1", "100.1", "101.1", "102.1", "103.1", "104.1", "105.1", 
"106.1", "107.1", "108.1", "109.1", "110.1", "111.1", "112.1", 
"113.1", "114.1", "115.1", "116.1", "117.1", "118.1", "119.1", 
"120.1", "121.1", "122.1", "123.1", "124.1", "125.1", "126.1", 
"127.1", "128.1", "129.1", "130.1", "131.1", "132.1", "133.1", 
"134.1", "135.1", "136.1", "137.1", "138.1", "139.1", "140.1", 
"141.1", "142.1", "143.1", "144.1", "145.1", "146.1", "147.1", 
"148.1", "149.1", "150.1", "151.1", "152.1", "153.1", "154.1", 
"155.1", "156.1", "157.1", "158.1", "159.1", "160.1", "161.1", 
"162.1", "163.1", "164.1", "165.1", "166.1", "167.1", "168.1"
), class = "factor"), id = c("0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", 
"0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", 
"1", "1")), .Names = c("long", "lat", "order", "hole", "piece", 
"group", "id"), row.names = c(NA, 100L), class = "data.frame")

dput(head(simdat, 100))
structure(list(state = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 14L, 14L, 14L, 14L), .Label = 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"), class = "factor"), 
    long = c(78.30000305, 78.30000305, 78.30000305, 78.30000305, 
    78.30000305, 78.30000305, 78.30000305, 78.30000305, 91.5, 
    91.5, 91.5, 91.5, 91.5, 91.5, 91.5, 91.5, 85.12999725, 85.12999725, 
    85.12999725, 85.12999725, 85.12999725, 85.12999725, 85.12999725, 
    85.12999725, 76.798553, 76.798553, 76.798553, 76.798553, 
    76.798553, 76.798553, 76.798553, 76.798553, 81.62999725, 
    81.62999725, 81.62999725, 81.62999725, 81.62999725, 81.62999725, 
    81.62999725, 81.62999725, 72.96666718, 72.96666718, 72.96666718, 
    72.96666718, 72.96666718, 72.96666718, 72.96666718, 72.96666718, 
    72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 
    72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 72.806396, 
    72.806396, 72.806396, 72.806396, 72.806396, 73.96992109, 
    73.96992109, 73.96992109, 73.96992109, 73.96992109, 73.96992109, 
    73.96992109, 73.96992109, 72.40000153, 72.40000153, 72.40000153, 
    72.40000153, 72.40000153, 72.40000153, 72.40000153, 72.40000153, 
    75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 
    75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 75.959473, 
    75.959473, 75.959473, 75.959473, 75.959473, 85.33000183, 
    85.33000183, 85.33000183, 85.33000183), lat = c(17.20000076, 
    17.20000076, 17.20000076, 17.20000076, 17.20000076, 17.20000076, 
    17.20000076, 17.20000076, 26.09000015, 26.09000015, 26.09000015, 
    26.09000015, 26.09000015, 26.09000015, 26.09000015, 26.09000015, 
    25.37000084, 25.37000084, 25.37000084, 25.37000084, 25.37000084, 
    25.37000084, 25.37000084, 25.37000084, 30.744196, 30.744196, 
    30.744196, 30.744196, 30.744196, 30.744196, 30.744196, 30.744196, 
    21.22999954, 21.22999954, 21.22999954, 21.22999954, 21.22999954, 
    21.22999954, 21.22999954, 21.22999954, 20.26666641, 20.26666641, 
    20.26666641, 20.26666641, 20.26666641, 20.26666641, 20.26666641, 
    20.26666641, 20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 
    20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 
    20.25189, 20.25189, 20.25189, 20.25189, 20.25189, 15.38429276, 
    15.38429276, 15.38429276, 15.38429276, 15.38429276, 15.38429276, 
    15.38429276, 15.38429276, 23.03000069, 23.03000069, 23.03000069, 
    23.03000069, 23.03000069, 23.03000069, 23.03000069, 23.03000069, 
    29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 
    29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 29.017748, 
    29.017748, 29.017748, 29.017748, 29.017748, 23.35000038, 
    23.35000038, 23.35000038, 23.35000038), value = c(134L, 3L, 
    1098L, 16645L, 123L, 2070L, 37L, 66L, 2L, 4131L, 1L, 21L, 
    0L, 127L, 5L, 651L, 220L, 260L, 1827L, 17596L, 20L, 0L, 0L, 
    22L, 16L, 0L, 2L, 0L, 18L, 450L, 0L, 22L, 30L, 41L, 56L, 
    3L, 53L, 66L, 11L, 4674L, 0L, 28L, 0L, 1L, 328L, 3L, 0L, 
    33L, 0L, 34L, 5L, 5L, 11L, 0L, 440L, 0L, 0L, 318L, 16L, 122L, 
    2990L, 35L, 0L, 44L, 1L, 671L, 0L, 43L, 0L, 302L, 0L, 0L, 
    0L, 40L, 10054L, 379L, 1492L, 160L, 77L, 385L, 4348L, 0L, 
    157L, 59L, 0L, 12L, 334L, 159L, 2013L, 12L, 0L, 3L, 29L, 
    0L, 15L, 16L, 0L, 49L, 10L, 329L), variable = structure(list(
        fact = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 
        4L, 7L, 8L, 2L, 3L, 5L, 6L, 3L, 1L, 6L, 4L, 8L, 2L, 7L, 
        5L, 8L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 8L, 7L, 2L, 3L, 1L, 
        6L, 5L, 4L, 5L, 1L, 7L, 8L, 4L, 6L, 2L, 3L, 7L, 6L, 8L, 
        1L, 5L, 3L, 4L, 2L, 7L, 6L, 5L, 8L, 4L, 1L, 2L, 3L, 5L, 
        4L, 8L, 6L, 7L, 3L, 2L, 1L, 2L, 7L, 4L, 5L, 6L, 3L, 8L, 
        1L, 4L, 7L, 6L, 3L, 2L, 5L, 1L, 8L, 4L, 3L, 7L, 5L, 6L, 
        2L, 8L, 1L, 2L, 1L, 5L, 3L), .Label = c("Buddhist", "Budhist", 
        "Christian", "Hindu", "Jain", "Muslim", "Others", "Sikh"
        ), class = "factor")), .Names = "fact", row.names = c(1L, 
    29L, 57L, 85L, 113L, 141L, 169L, 197L, 2L, 86L, 170L, 198L, 
    30L, 58L, 114L, 142L, 59L, 3L, 143L, 87L, 199L, 31L, 171L, 
    115L, 200L, 172L, 4L, 32L, 60L, 88L, 116L, 144L, 201L, 173L, 
    33L, 61L, 5L, 145L, 117L, 89L, 118L, 6L, 174L, 202L, 90L, 
    146L, 34L, 62L, 175L, 147L, 203L, 7L, 119L, 63L, 91L, 35L, 
    176L, 148L, 120L, 204L, 92L, 8L, 36L, 64L, 121L, 93L, 205L, 
    149L, 177L, 65L, 37L, 9L, 38L, 178L, 94L, 122L, 150L, 66L, 
    206L, 10L, 95L, 179L, 151L, 67L, 39L, 123L, 11L, 207L, 96L, 
    68L, 180L, 124L, 152L, 40L, 208L, 12L, 41L, 13L, 125L, 69L
    ), class = "data.frame")), .Names = c("state", "long", "lat", 
"value", "variable"), row.names = c(NA, 100L), class = "data.frame")

解决方案

There is an known issue with ggsubplot and R3.1.0. See more on this here and here.

I can plot your data with ggplot but not with ggsubplot

library(ggplot2)
df$fact <- df$variable$fact
df$state <- as.character(df$state)
df <- df[ ,-5]
ggplot(df, aes(x = fact, y = value, fill = fact)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  facet_wrap(~state) +
  theme(axis.text.x = element_blank()
        ,axis.title.x = element_blank()
        ,axis.ticks.x = element_blank()
        ,strip.text = element_text(size = 8))

If you want to represent your data with pies, you could try something like

library(dplyr)
df1 <- df %>%
  group_by(state) %>%
  mutate(tsum = sum(value),
         prop = value/tsum)

ggplot(df1, aes(x=factor(1), y=prop, fill = fact1)) +
  geom_bar(stat="identity") +
  coord_polar(theta = 'y') +
  facet_wrap(~ state, ncol = 4)  +
  theme(axis.text.x = element_blank()
        ,axis.title.x = element_blank()
        ,axis.text.y = element_blank()
        ,axis.title.y = element_blank()
        ,axis.ticks = element_blank()
        ,strip.text = element_text(size = 8))

For subplot, something like this should work:

library(maptools)
data(wrld_simpl)
india <- wrld_simpl[wrld_simpl$NAME == 'India', ]
dfindia <- fortify(india)
p <- ggplot() +
       geom_polygon(data=dfindia, aes(x=long, y=lat,group=group)) +
       geom_coord()
p + geom_subplot(data=simdat,
             aes(long, lat, group=state,
                 subplot = geom_bar(aes(x=fact, y=value, fill = state),
                                    stat="identity")), width = 10, height=10)

这篇关于使用ggplot2在地图上绘制条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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