Reputation: 37
I have a dataframe generated from a weighted survey of a list of countries and their subgroups
Sampling Weights | Country | Gender | Percentage | n |
---|---|---|---|---|
400 | A | Male | 20 | 100 |
600 | A | Female | 80 | 70 |
300 | B | Male | 30 | 45 |
500 | B | Female | 70 | 70 |
100 | C | Male | 40 | 100 |
50 | C | Female | 60 | 70 |
Using ggplot, I want to create a bar chart that has the n values of the subgroup in the x axis ticks. The x axis would be the countries with the male/female subgroups plus sample size and the y axis would be the percentage.
It would be too tedious to rename each subgroup name and add the sample size in parentheses so I was wondering if there was a faster way to do so while also being able to wrap the text around.
So
Male (n = 100) Female (n = 70) Country A
Keep in mind some of these Country names are actually long in the real data set so the labels would get crowded.
Upvotes: 1
Views: 514
Reputation: 66815
library(dplyr); library(ggplot2)
df1 %>%
ggplot(aes(Gender, Percentage)) +
geom_col() +
geom_text(aes(y = 0, label = paste("n = ", n)), vjust = 1.5) +
scale_x_discrete(name = NULL) +
facet_wrap(~Country, strip.position = "bottom") +
theme(strip.placement = "outside", strip.background = element_blank())
Or:
df1 %>%
mutate(Gender2 = paste0(Gender, " (n = ", n, ")")) %>%
ggplot(aes(Gender2, Percentage)) +
geom_col() +
scale_x_discrete(name = NULL) +
facet_wrap(~Country, strip.position = "bottom", scales = "free_x") +
theme(strip.placement = "outside", strip.background = element_blank())
Upvotes: 0