I am trying to visualize qualitative grade based data, and I would like to order the grades from worst to best. The current ordering seems to be lexicographic one. How can I change this? Unfortunately I do not know how to share a working example: the column G3.x
is full of letter based qualitative grades. When I tried the code with an example test_data <- c("Excellent", "Very Good", "Good", "Sufficient", "Weak", "Poor")
I get errors: Error: data must be a data frame, or other object coercible by fortify(), not a character vector
library(ggplot2)
library(scales)
ggplot(data = math_and_portuguese, aes(x = G3.x)) +
geom_bar(aes(y = (..count..)/sum(..count..)), position = "dodge", fill = "cornflowerblue", color="black") +
geom_text(aes(y = round((..count..)/sum(..count..),2),
label=paste0(round(prop.table(..count..) * 100, 2), '%'),
size = 3, hjust = 0.5, vjust = -1, position = "stack"),
stat = 'count',
position = position_dodge(.9),
size = 3)+
labs(x = 'G3 Mathematics grade', y = 'Percent')
p1
The ordering of any discrete item is done in the following way:
ggplot2
orders by the levels of the factorggplot2
orders alphanumericallyIn this case, the solution is to set the levels of math_and_portuguese$G3.x
discretely by converting to a factor and specifying the levels you want:
math_and_portuguese$G3.x <- factor(math_and_portuguese$G3.x,
levels=c("Excellent", "Very Good", "Good", "Sufficient", "Weak", "Poor"))
Run that first, then the ordering of your axis should reflect the levels of the factor.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.