'how to use geom_pointrange to plot data in descending order?

I have been asked to do this, "make a dotplot of the average age for each combination of sex, passengerClass and survival. Use geom_pointrange, order the dots by decreasing age and make sure the labels are on the y-axis.". This is my try and the plot it produce doesn't seem to be right.

# the dataset given to me
TitanicSurvival %>% group_by(survived,passengerClass,sex) %>% 
  summarize(age=mean(age, na.rm=T)) %>%
  unite(survived, passengerClass, sex, col="group", remove = FALSE) -> age
glimpse(age)

# my attempt to plot
age %>% 
    unite(sex, passengerClass, survived, col="group", remove = FALSE) %>%
    ggplot(aes(x = age, y= group)) + 
    geom_dotplot(binwidth = 1) +
    geom_pointrange(aes(xmin = sort(age), xmax = sort(age)), orientation = "y")

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source