首页 文章

重新排序()没有正确地重新排序ggplot中的因子变量

提问于
浏览
15

我很困惑为什么箱形图没有在这个情节中订购:

set.seed(200)
x <- data.frame(country=c(rep('UK', 10), 
                          rep("USA", 10), 
                          rep("Ireland", 5)),
                wing=c(rnorm(25)))

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

enter image description here

如何根据最高 - 最低中位数(从左到右)订购箱图?

3 回答

  • -1

    因为你没有使它成为有序因子 . 尝试

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

    enter image description here

  • 6

    你的代码应该工作正常 . 可能你有一些软件包加载了一个函数,该函数掩盖了基本 reorder 函数,或者可能是用户定义的 reorder 函数,它的工作方式不同 .

    您可以使用 conflicts() 检查此类名称冲突 . 分离包, rm(reorder) 或重新启动R并重新尝试而不定义/附加冲突定义将解决问题 .

  • 0
    ggplot(x, aes(reorder(country, wing, FUN = median), wing)) + geom_boxplot()
    

相关问题