Reputation: 101
I am trying to create two plots which should display frequency in a decreasing order.
#preparing the data to resemble actual data
test <- data.frame(HairEyeColor) %>%
mutate(combi = paste(Hair,Eye)) %>%
group_by(Sex) %>%
mutate(prop = Freq / sum(Freq)) %>%
ungroup()
test$combi <- factor(test$combi)
freq_test_count <- test %>%
setorder(Freq)
#creating the plot
freq_test_plot <- freq_test_count %>%
ggplot(aes(x = reorder(combi,prop),y = prop, label = Freq)) +
geom_col(show.legend = FALSE) +
geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) +
facet_wrap(~Sex, scales = "free") +
labs(y = "Proportion",
x = NULL) +
coord_flip()
When i plot freq_test_plot, it shows the plot but the output is not in decreasing order
I am not sure what should I do so that I can see terms in decreasing order of frequency.
Upvotes: 0
Views: 506
Reputation: 460
Are you wanting the values to be sorted on male or female?
library(tidyverse)
#preparing the data to resemble actual data
test <- data.frame(HairEyeColor) %>%
mutate(combi = paste(Hair,Eye)) %>%
group_by(Sex) %>%
mutate(prop = Freq / sum(Freq)) %>%
ungroup()
test$combi <- factor(test$combi)
test$combi<- factor(test$combi, levels = unique(test$combi)[order(test$Freq)],)
#creating the plot
ggplot(test,aes(x = combi,y = prop, label = Freq))+
geom_col(show.legend = FALSE)+
geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) +
facet_wrap(~Sex, scales = "free")+
labs(y = "Proportion",
x = NULL) +
coord_flip()
updated to include full code from question.
Upvotes: 0
Reputation: 145745
Another work-around is to make male and female specific levels for the factor. Here I add a space " "
to the front of the Male Hair/Eye labels. This lets you define an ordering that takes sex into account:
test <- data.frame(HairEyeColor) %>%
mutate(combi = paste(Hair,Eye)) %>%
group_by(Sex) %>%
mutate(prop = Freq / sum(Freq)) %>%
ungroup() %>%
mutate(combi = factor(test$combi),
sex_combi = factor(paste(ifelse(Sex == "Male", " ", ""), Hair, Eye)),
sex_combi = reorder(sex_combi, prop))
#creating the plot
ggplot(test, aes(x = sex_combi,y = prop, label = Freq)) +
geom_col(show.legend = FALSE) +
geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) +
facet_wrap(~Sex, scales = "free") +
labs(y = "Proportion",
x = NULL) +
coord_flip()
But as I mentioned in the comments, I think this is a misleading plot.
Upvotes: 1
Reputation: 32538
A workaround is to create two different plots and arrange them in grid. But you should be cautious because, like Gregor mentioned, it could definitely be misleading.
library(grid)
p1 = freq_test_count[freq_test_count$Sex == "Male",] %>%
ggplot(aes(x = reorder(combi,prop),y = prop, label = Freq)) +
geom_col(show.legend = FALSE) +
geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) +
facet_wrap(~Sex, scales = "free") +
labs(y = "Proportion",
x = NULL) +
coord_flip()
p2 = freq_test_count[freq_test_count$Sex == "Female",] %>%
ggplot(aes(x = reorder(combi,prop),y = prop, label = Freq)) +
geom_col(show.legend = FALSE) +
geom_text(check_overlap = TRUE, nudge_y = 0.005, size = 3) +
facet_wrap(~Sex, scales = "free") +
labs(y = "Proportion",
x = NULL) +
coord_flip()
graphics.off()
grid.newpage()
grid.draw(ggarrange(p1, p2, ncol = 2))
Upvotes: 2