0

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 

Barplot

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?

  • Does this answer your question? [Re-ordering bars in R's barplot()](https://stackoverflow.com/questions/37480949/re-ordering-bars-in-rs-barplot) – user438383 Sep 03 '22 at 08:15

1 Answers1

1
x <- factor(x, levels = ord)
tab <- table(x)
barplot(tab)

enter image description here

Julien
  • 1,613
  • 1
  • 10
  • 26
  • Thank you so much for your help! I kept on trying to add levels to the table, and it wasn't working. Since I'm new to R, I didn't realize that I needed to add the levels to the data before I made it into a table. – PlasmaRaptor360 Sep 03 '22 at 16:17