我在ggplot2中制作一个多面的bar_plot时遇到了一些麻烦。也许这是非常明显的事情,但我无法弄明白:(我有以下数据集:
structure(list(COUNTRY = structure(c(1L, 4L, 7L, 10L, 13L, 16L,
19L, 2L, 5L, 8L, 11L, 14L, 17L, 20L, 3L, 6L, 9L, 12L, 15L, 18L,
2L, 5L, 8L, 11L, 14L, 17L, 20L, 3L, 6L, 9L, 12L, 15L, 18L, 1L,
4L, 7L, 10L, 13L, 16L, 19L, 3L, 6L, 9L, 12L, 15L, 18L, 1L, 4L,
7L, 10L, 13L, 16L, 19L, 2L, 5L, 8L, 11L, 14L, 17L, 20L), .Label = c("Angola",
"Botswana", "Burundi", "Comoros", "Eritrea", "Ethiopia", "Kenya",
"Lesotho", "Madagascar", "Malawi", "Mozambique", "Namibia", "Rwanda",
"Somalia", "South Africa", "Swaziland", "Tanzania", "Uganda",
"Zambia", "Zimbabwe"), class = "factor"), Year = structure(c(2L,
2L, 14L, 16L, 16L, 11L, 12L, 2L, 4L, 15L, 5L, 10L, 16L, 16L,
2L, 17L, 14L, 11L, 12L, 10L, 2L, 4L, 15L, 5L, 10L, 16L, 16L,
2L, 17L, 14L, 11L, 12L, 10L, 2L, 2L, 14L, 16L, 16L, 11L, 12L,
2L, 17L, 14L, 11L, 12L, 10L, 2L, 2L, 14L, 16L, 16L, 11L, 12L,
2L, 4L, 15L, 5L, 10L, 16L, 16L), .Label = c("1998", "2000", "2001/2",
"2002", "2003", "2003/4", "2004", "2005", "2005/6", "2006", "2006/7",
"2007", "2007/8", "2008/9", "2009", "2010", "2011"), class = "factor"),
sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L), .Label = c("m", "f", "b"), class = "factor"),
location = structure(c(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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Urban", "Rural", "Total",
"Capital.City", "Other.Cities.towns", "Urban.Non.slum", "Urban.Slum"
), class = "factor"), percent = c(60.4, 42.3, 85.4919452426806,
96.3, 90.2847535659154, 87.7347421555771, 87.7323067592087,
80.4, 80.6, 93.8186266493188, 75.0109418832216, 36.8, 87.1059275774722,
90.1216932603937, 66.8, 83.6279398931798, 89.690685909038,
88.8207941092749, 94.6139558774441, 88.0251085200726, 70.4,
54.7, 86.1919805548309, 56.9792710715853, 13.1, 75.6355555697382,
86.8196674671991, 42.5, 61.9452522893308, 77.597285694676,
88.3453320625631, 94.5192341778471, 80.6271302923487, 44.1,
29, 77.8542469357068, 90, 86.7073851186482, 83.8921034867784,
76.4094871587916, 49.3, 63.952805392032, 77.004884485532,
88.6723566877386, 93.9560433940531, 82.3095948307742, 56.1,
31.1, 80.0235653889704, 91.5, 88.3809682134183, 85.5656196766576,
80.0539027063387, 77, 61.2, 89.2538966046165, 59.6756344409838,
23, 79.6749544074645, 86.9507859695728)), .Names = c("COUNTRY",
"Year", "sex", "location", "percent"), row.names = c(1L, 4L,
7L, 10L, 13L, 16L, 19L, 22L, 25L, 28L, 31L, 34L, 37L, 40L, 43L,
46L, 49L, 52L, 55L, 58L, 62L, 65L, 68L, 71L, 74L, 77L, 80L, 83L,
86L, 89L, 92L, 95L, 98L, 101L, 104L, 107L, 110L, 113L, 116L,
119L, 123L, 126L, 129L, 132L, 135L, 138L, 141L, 144L, 147L, 150L,
153L, 156L, 159L, 162L, 165L, 168L, 171L, 174L, 177L, 180L), class = "data.frame")
我正试图制作一个bar_plot,显示一些国家的农村,城市地区(和平均水平)居民的百分比,并希望按性别分列。我可以通过在ggplot函数中使用子集调用在一个简单的条形图上绘制其中一个类别,如下所示:
ggplot(edu_melt[c(edu_melt$sex!="b" & edu_melt$location==c("Urban")), ], aes(x=COUNTRY, y=percent, fill=sex)) + geom_bar(position="dodge", width=0.5) + facet_grid(~location) + labs(x="Country") + theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))
然而,我想比较整个地区的数据(例如城市,乡村和两地)。我认为这是引入facet_wrap调用的一个简单例子,但是我得到一些奇怪的行为,其中数据被绘制在三个方面 - 我希望每个方面上有20对条形,但是这个代码产生20对条形扩展在三个方面?!
ggplot(edu_melt_over[c(edu_melt_over$sex!="b"),], aes(x=COUNTRY, y=percent, fill=sex)) + geom_bar(position="dodge", width=0.5, space=1) + facet_wrap(~location, nrow=3) + labs(x="Country", title="Proportion Net Primary School Enrolement in ESA") + theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))
我不确定为什么会发生这种情况,但是已经搜索了提示和提示并尝试了一些方法,但得到了相同的结果。任何人都知道如何制作这个情节?
由于
玛蒂
答案 0 :(得分:0)
您的数据看起来很奇怪,因为您似乎没有同一层次中男性和女性的任何组合(例如,安哥拉有男性城市百分比,但没有女性)。这是数据而不是绘图。
ggplot(edu_melt[edu_melt$sex!="b", ], aes(x=COUNTRY, y=percent, fill=sex)) +
geom_bar(position="dodge", width=0.25) + facet_grid(location~.) + labs(x="Country") +
theme(axis.text.x = element_text(angle=30))