I have a question that is more directed towards what's the most efficient way in order to create a bar chart with percentage labels and the intended layout. I have a data frame containing several columns which includes the column "economy". That column does have the five values "Very good", "Good", "Bad", "Very bad" and "Don't know". Here is the reproducible data:
structure(c(3L, 3L, 3L, 3L, 2L, 3L, 4L, 4L, 4L, 4L, 3L, 2L, 2L,
2L, 3L, 2L, 4L, 4L, 2L, 3L, 4L, 3L, 4L, 4L, 3L, 2L, 2L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 3L, 2L, 4L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L,
2L, 4L, 4L, 3L, 2L), .Label = c("Very good", "Good", "Bad", "Very bad",
"Don't know"), class = "factor")
I used this code with the desired outcome:
lebanon %>%
filter(!is.na(economy), economy != "Don't know") %>%
count(economy) %>%
mutate(prop = n / sum(n)) %>%
ggplot(aes(economy, y = prop, fill = economy)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkgreen", "green4", "red3", "red4")) +
scale_y_continuous(labels = scales::percent) +
geom_text(aes(label = scales::percent(prop, suffix = "")),
position=position_dodge(width=0.9), vjust=-0.5, size = 5) +
labs(x = "", y = "", fill = "") +
theme_minimal() +
theme(axis.text.x = element_text(size = 15),
axis.text.y = element_text(size = 15),
legend.text = element_text(size = 15))
Getting this plot:
I'm wondering know if this is the most efficient way in order to recalculate the counts to percentage with the desired layout. I used the count function and mutate, but I also know that there might be other ways of handling this problem with stat(prop) and ..count.. function. The problem is that when I use stat(prop) or fill = "prop", it doesn't take the scale_fill_manual function.
So my question is what's the most efficient way to get my desired bar chart (the one above) without too many intermediate steps for the calculation of percentages. Sorry in advance if my question is not clearly formulated. :)
Greetings
factor
, not adata.frame
and thefactor
does not contain any observations with value "Very good", yet your graph does. – Limey