Reputation: 857
The data below represents the percentage of education (3 levels: 1 Low, 2 Mid, 3 High) in three different countries. I use the following code to plot them:
ggplot(df, aes(education)) +
geom_bar(aes(weight = percentage)) +
facet_wrap(~country, ncol=3)
However, the bars are next to each other. I try to use the option fill
as in the examples, but it is not working out for some strange reason. My aim is to fill one bar for each country whereby it includes the % of the three different levels of education. I would also like to rank the countries in the graph in the order based on education level 1. The lowest percentage
in category 1 of education
is the first bar on the left in the graph - going gradually to the highest percentage
of category 1 in the education variable. Could someone guide me please?
country percentage education
1 15 1
1 65 2
1 20 3
2 20 1
2 8 2
2 42 3
3 21 1
3 59 2
3 21 3
structure(list(country = structure(c(1, 1, 1, 2, 2, 2, 3, 3,
3), format.stata = "%9.0g"), percentage = structure(c(15, 65,
20, 20, 28, 42, 21, 59, 21), format.stata = "%9.0g"), education = structure(c(1,
2, 3, 1, 2, 3, 1, 2, 3), format.stata = "%9.0g")), row.names = c(NA,
-9L), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 0
Views: 257
Reputation: 65
It is not completely clear what you mean by "highest share of education". Do you mean any type of education? Then it would be country 3 with 101 % of education. Or do you mean highest share of high education? Then it would be country 1 with 65 % of level 3 education.
Please clarify.
You could have a look at the rank function (?rank), and make a new column using something like this: rank(df$percentage[which(test$education==3)])
(I would add this as a comment but I don't have enough reputation to add comments yet).
Upvotes: 2
Reputation: 363
Use geom_col instead of geom_bar and set country as factor.
df$country <- as.factor(df$country)
ggplot(df, aes(x=education, y=percentage)) +
geom_col(aes(fill=country))
Upvotes: 2