Reputation: 1
How do you go about changing the order or boxplots so that they are ordered greatest to least by their means.
This is my attempt of reordering the boxplots by their means:
url <- "http://www.cse.lehigh.edu/~brian/course/2020/datascience/student-teacher-ratios.csv"
df_ratios <- read.csv(url, header=T)
s2 <- with(df_ratios, reorder(region, -student_ratio, mean))
with(df_ratios, boxplot(student_ratio~s2))
Upvotes: 0
Views: 104
Reputation: 206197
Your problem is that your data has missing values. So the mean for all columns is NA. In order to ignore the missing values when calculating mean, you can doo
df_ratios <- read.csv(url, header=T)
s2 <- with(df_ratios, reorder(region, -student_ratio, mean, na.rm=TRUE))
with(df_ratios, boxplot(student_ratio~s2))
Upvotes: 2
Reputation: 1284
A solution using the ggplots2 and forcats packages, assuming you mean that they should be ordered according by the median.
library(ggplot2)
library(forcats)
ggplot(df_ratios, aes(x = fct_reorder(region, student_ratio, .fun = median), y = student_ratio)) + geom_boxplot()
Upvotes: 1