Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot stacked bar plot from 2 separate data frames

I have been struggling with this conundrum all day and getting close, but no cigar. I have two data frames which are the results of two separate socio-economic surveys from two districts within a city. I want to compare columns from these data frames side by side in a bar plot to show the frequencies (counts) of responses to a particular question across both surveys.

The questions asked in each survey were identical. However, they were coded slightly differently and therefore the column names are slightly different as follows! I have managed to plot data from my two data frames (ar and bn) on the same bar plot with the raw data i.e. without having to merge the data frames. However, I seem unable to plot stacked bar plots side-by-side.

I used ggplot2 with the following code:

    ggplot(bn, aes(A8_HHH_hig, fill=A6_Sex_HHH))
+ geom_bar(position="stack", alpha=0.5) 
+ geom_bar(data=ar, aes(A9_HHHedulevl, fill=A7_HHsex), position="stack", alpha=0.5)

Which produces this: enter image description here

As you'll notice I am attempting to plot the split between male and female respondents based on their highest level of schooling for the two data frames. (Note the sex of the respondent is also coded differently in each data frame i.e. male/m and female/f.)

I'd really like these two stacked bar plots to be plotted on the same grid, side by side so it is easy for me to compare values. However, I'm not entirely sure if I can use the position="dodge" option here, as the values come from different dataframes.

Does anyone know if this is possible?! Or perhaps another way of comparing these values, visually?

I've attached some reproducible code if anyone has the time to have a look!

thanks

dput(ar)

structure(list(District = c("Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi"), A9_HHHedulevl = structure(c(9L, 
9L, 9L, 9L, 8L, 9L, 5L, 9L, 9L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 2L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 3L, 9L, 3L, 9L, 9L, 9L, 9L, 
7L, 7L, 8L, 6L, 9L, 9L, 8L, 9L, 9L, 8L, 6L, 9L, 9L, 9L, 9L, 8L, 
6L, 9L, 9L, 9L, 6L, 9L, 9L, 1L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 4L, 9L, 6L, 9L, 9L, 9L, 9L, 6L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 8L, 6L, 8L, 9L, 9L, 9L, 6L, 6L, 3L, 6L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 9L, 9L, 9L, 
3L), .Label = c("Adult Education", "Junior Secondary", "koranic", 
"NCE", "None", "Polytechnic", "Senior Primary", "Senior Secondary", 
"University"), class = "factor"), A7_HHsex = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L), .Label = c("female", "male"), class = "factor")), .Names = c("District", 
"A9_HHHedulevl", "A7_HHsex"), row.names = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 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, 46L, 47L, 
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 
61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 
74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 
87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 
100L, 101L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 
111L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 
123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 
134L), class = "data.frame", na.action = structure(131:135, .Names = c("135", 
"136", "137", "138", "139"), class = "omit"))

dput(bn)

structure(list(District = c("Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa"), A8_HHH_hig = structure(c(7L, 7L, 7L, 12L, 7L, 7L, 
12L, 4L, 4L, 4L, 4L, 4L, 9L, 7L, 7L, 10L, 4L, 1L, 4L, 7L, 10L, 
12L, 12L, 12L, 7L, 12L, 9L, 6L, 4L, 11L, 4L, 4L, 4L, 10L, 12L, 
12L, 12L, 12L, 7L, 10L, 9L, 11L, 7L, 7L, 7L, 7L, 9L, 7L, 7L, 
7L, 7L, 9L, 7L, 12L, 12L, 7L, 12L, 11L, 7L, 7L, 12L, 12L, 12L, 
12L, 12L, 12L, 7L, 12L, 10L, 10L, 12L, 8L, 4L, 4L, 12L, 12L, 
4L, 12L, 12L, 12L, 7L, 7L, 9L, 2L, 9L, 12L, 2L, 5L, 12L, 7L, 
10L, 10L, 12L, 10L, 10L, 4L, 10L, 1L, 5L, 7L, 1L, 10L, 10L, 10L, 
10L, 10L, 10L, 3L, 10L, 10L, 4L, 10L, 10L, 10L, 10L, 10L, 4L, 
10L, 10L, 10L, 3L, 10L, 9L, 4L, 4L, 4L, 4L, 12L, 12L, 12L, 12L, 
3L, 7L, 7L, 5L, 7L, 7L, 12L, 12L, 7L, 10L, 7L, 7L, 7L, 12L, 12L, 
7L, 7L, 12L, 12L, 12L, 12L, 12L, 7L, 12L, 12L, 12L, 12L, 12L, 
10L, 10L, 12L, 12L, 9L, 12L, 12L, 7L, 6L, 12L, 12L, 7L, 12L, 
10L, 5L, 12L, 12L, 7L, 11L, 12L, 12L, 12L, 5L, 7L, 7L, 12L, 12L, 
7L, 7L, 7L, 12L, 7L, 7L, 12L, 12L, 12L, 1L), .Label = c("Adult Education", 
"Junior Primary", "Junior Secondary", "Koranic", "NCE", "None", 
"Polytechnic", "Prelim / JMB", "Senior Primary", "Senior Secondary", 
"Technical College", "University"), class = "factor"), A6_Sex_HHH = structure(c(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, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 
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, 1L, 1L, 2L, 1L, 2L, 2L, 
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 1L), .Label = c("F", "M"), class = "factor")), .Names = c("District", 
"A8_HHH_hig", "A6_Sex_HHH"), row.names = c(NA, 196L), class = "data.frame")

This is an example of the kind of thing I am looking to produce:

enter image description here

structure(list(sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L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L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 9L, 
6L, 9L, 8L, 8L, 9L, 11L, 3L, 3L, 9L, 9L, 3L, 9L, 9L, 9L, 
6L, 6L, 7L, 10L, 7L, 9L, 10L, 4L, 9L, 6L, 8L, 8L, 9L, 8L, 
8L, 3L, 8L, 1L, 4L, 6L, 1L, 8L, 8L, 8L, 8L, 8L, 8L, 2L, 8L, 
8L, 3L, 8L, 8L, 8L, 8L, 8L, 3L, 8L, 8L, 8L, 2L, 8L, 7L, 3L, 
3L, 3L, 3L, 9L, 9L, 9L, 9L, 2L, 6L, 6L, 4L, 6L, 6L, 9L, 9L, 
6L, 8L, 6L, 6L, 6L, 9L, 9L, 6L, 6L, 9L, 9L, 9L, 9L, 9L, 6L, 
9L, 9L, 9L, 9L, 9L, 8L, 8L, 9L, 9L, 7L, 9L, 9L, 6L, 5L, 9L, 
9L, 6L, 9L, 8L, 4L, 9L, 9L, 6L, 12L, 9L, 9L, 9L, 4L, 6L, 
6L, 9L, 9L, 6L, 6L, 6L, 9L, 6L, 6L, 9L, 9L, 9L, 1L), .Label = c("Adult Education", 
"Junior Secondary", "Koranic", "NCE", "None", "Polytechnic", 
"Senior Primary", "Senior Secondary", "University", "Junior Primary", 
"Prelim / JMB", "Technical College"), class = "factor"), 
district = c("Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Angwan Rimi", "Angwan Rimi", "Angwan Rimi", "Angwan Rimi", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", "Barnawa", 
"Barnawa", "Barnawa", "Barnawa", "Barnawa")), .Names = c("sex", 
"education", "district"), row.names = c("1", "2", "3", "4", "5", 
"6", "7", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", 
"41", "42", "43", "44", "46", "47", "48", "49", "50", "51", "52", 
"53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", 
"64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", 
"75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", 
"86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", 
"97", "98", "99", "100", "101", "102", "103", "104", "105", "106", 
"107", "108", "109", "110", "111", "113", "114", "115", "116", 
"117", "118", "119", "120", "121", "122", "123", "124", "125", 
"126", "127", "128", "129", "130", "131", "132", "133", "134", 
"135", "136", "137", "138", "139", "1361", "1371", "1381", "1391", 
"140", "141", "142", "143", "144", "145", "146", "147", "148", 
"149", "150", "151", "152", "153", "154", "155", "156", "157", 
"158", "159", "160", "161", "162", "163", "164", "165", "166", 
"167", "168", "169", "170", "171", "172", "173", "174", "175", 
"176", "177", "178", "179", "180", "181", "182", "183", "184", 
"185", "186", "187", "188", "189", "190", "191", "192", "193", 
"194", "195", "196", "197", "198", "199", "200", "201", "202", 
"203", "204", "205", "206", "207", "208", "209", "210", "211", 
"212", "213", "214", "215", "216", "217", "218", "219", "220", 
"221", "222", "223", "224", "225", "226", "227", "228", "229", 
"230", "231", "232", "233", "234", "235", "236", "237", "238", 
"239", "240", "241", "242", "243", "244", "245", "246", "247", 
"248", "249", "250", "251", "252", "253", "254", "255", "256", 
"257", "258", "259", "260", "261", "262", "263", "264", "265", 
"266", "267", "268", "269", "270", "271", "272", "273", "274", 
"275", "276", "277", "278", "279", "280", "281", "282", "283", 
"284", "285", "286", "287", "288", "289", "290", "291", "292", 
"293", "294", "295", "296", "297", "298", "299", "300", "301", 
"302", "303", "304", "305", "306", "307", "308", "309", "310", 
"311", "312", "313", "314", "315", "316", "317", "318", "319", 
"320", "321", "322", "323", "324", "325", "326", "327", "328", 
"329", "330", "331"), class = "data.frame")
like image 535
marty_c Avatar asked Feb 15 '23 14:02

marty_c


1 Answers

Here is a workaround for this problem because directly in one ggplot2 plot you can't combine dodging and stacking.

For this plot I used the data frame all_data that is made @DrewSteen answer - data frame should contain values for all District levels.

The idea is to plot on x axis combination of District and A8_HHH_hig. This is achieved with function interaction() inside the aes() call. Then this is converted to numeric to ensure that later we can add labels at positions we want. For the fill= we again use combination - now between sex and District. To get labels for the x axis use scale_x_continuous() and set break points at 2,4,6,...,26 (values between two bars that are of the same education level, maximal value is calculated as number of levels of A8_HHH_hig * levels of Sex) and as labels provide levels of A8_HHH_hig.

ggplot(all_data,aes(as.numeric(interaction(District,A8_HHH_hig)),
                        fill=interaction(sex,District)))+
       geom_bar(binwidth=1)+
       theme(axis.text.x=element_text(angle=-45, hjust=0))+
     scale_x_continuous("Education",breaks=seq(2,26,2),
          labels=levels(all_data$A8_HHH_hig),expand=c(0,0.5),limits=c(1,27))+
     scale_fill_discrete("",
          labels=c("Female Angwan","Male  Angwan","Female Barnawa","Male Barnawa"))

enter image description here

like image 108
Didzis Elferts Avatar answered Feb 24 '23 01:02

Didzis Elferts