简体   繁体   English

R ggplot geom_bar刻面闪避

[英]R ggplot geom_bar facet dodge

I'm having some trouble producing a faceted bar_plot in ggplot2. 我在ggplot2中生成多面的bar_plot时遇到了一些麻烦。 Perhaps it is something very obvious, but I can't figure it out:( I've the following dataset: 也许这很明显,但是我无法弄清楚:(我有以下数据集:

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

I am trying to make a bar_plot which shows the percentage of people living in rural, urban areas (and the average) for a number of countries, and wish to show this split by gender. 我正在尝试制作一个bar_plot,以显示许多国家在农村,城市地区生活的人口百分比(以及平均水平),并希望按性别显示这一比例。 I can plot one of these categories on a simple bar plot by using a subset call within the ggplot function as follows: 通过在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))

I would however like to compare the data across the location (eg urban, rural, and both). 但是,我想比较整个位置(例如城市,农村和两者)的数据。 I thought this would be a simple case of introducing a facet_wrap call, however I get some odd behaviour where the data is plotted across the three facets - I would expect 20 pairs of bars on each facet, however this code produces 20 pairs of bars spread over the three facets?! 我以为这是引入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))

I'm not sure why this is happening, but have searched for hints and tips and tried a number of approaches, but get the same result. 我不确定为什么会这样,但是已经搜索了提示和技巧并尝试了多种方法,但是得到了相同的结果。 Anybody have any idea how I could produce this plot? 有人知道我如何制作这个情节吗?

Thanks 谢谢

Marty 马蒂

Your data looks odd as you don't seem to have any combinations of male and female in the same strata (eg Angola has a male urban percent but no female). 您的数据看起来很奇怪,因为您似乎在同一层中没有男性和女性的任何组合(例如,安哥拉的男性城市百分比为女性,但没有女性)。 This is the data not the plotting. 这是数据而不是绘图。

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM