Reorder() not correctly reordering a factor variable in ggplot

前端 未结 3 1354
挽巷
挽巷 2021-01-01 19:30

I\'m baffled as to why the boxplots are not ordering in this plot:

set.seed(200)
x <- data.frame(country=c(rep(\'UK\', 10), 
                          rep         


        
相关标签:
3条回答
  • 2021-01-01 20:09

    Your code should works fine. Probably you had some package loaded with a function that masked the base reorder function, or perhaps a user-defined reorder function, that doesn't work the same way.

    You can check for such name-clashes with conflicts(). Detaching the package, rm(reorder), or restarting R and trying again without defining/attaching the conflicting definition will solve the problem.

    0 讨论(0)
  • 2021-01-01 20:18

    Because you did not make it an ordered factor. Try

    ggplot(x, aes(reorder(country, wing, median, order=TRUE), wing)) + geom_boxplot()
    

    enter image description here

    0 讨论(0)
  • 2021-01-01 20:18
    ggplot(x, aes(reorder(country, wing, FUN = median), wing)) + geom_boxplot()
    
    0 讨论(0)
提交回复
热议问题