簡體   English   中英

R ggplot geom_bar刻面閃避

[英]R ggplot geom_bar facet dodge

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

我不確定為什么會這樣,但是已經搜索了提示和技巧並嘗試了多種方法,但是得到了相同的結果。 有人知道我如何制作這個情節嗎?

謝謝

馬蒂

您的數據看起來很奇怪,因為您似乎在同一層中沒有男性和女性的任何組合(例如,安哥拉的男性城市百分比為女性,但沒有女性)。 這是數據而不是繪圖。

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