I have the following code:
set.seed(2022)
x <- sample(c("Freshman", "Sophomore",
"Junior", "Senior"),
300, prob = c(.25, .3, .2, .25), replace = TRUE)
tab <- table(x)
barplot(tab)
This produces the following table and graph:
x
Freshman Junior Senior Sophomore
73 69 77 81
I want the bars in the bar graph to be in the following order:
ord <- c("Freshman", "Sophomore", "Junior", "Senior")
What is the simplest way to change the order of the bars, without using packages?
CodePudding user response:
x <- factor(x, levels = ord)
tab <- table(x)
barplot(tab)