简体   繁体   中英

R ggplot geom_bar facet dodge

I'm having some trouble producing a faceted bar_plot in ggplot2. 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. I can plot one of these categories on a simple bar plot by using a subset call within the ggplot function as follows:

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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