【问题标题】:sort boxplot at factor (iris, R, ggplot) [duplicate]按因子排序箱线图(iris,R,ggplot)[重复]
【发布时间】:2020-10-17 12:37:38
【问题描述】:

我想对箱线图进行排序。
'left is high box' and 'right is not high'

ggplot(data = iris ,aes(x = Species,y=Sepal.Width)) +
  geom_violin(fill='gray')

我做了这个代码,setosa:left 和 virginica:right。
但是每个物种的 max(Sepal.Width),Versicolor 最少。
我想将 setosa-virginica-versicolor 引用排序为 max(Sepal.Width)。

【问题讨论】:

标签: r ggplot2


【解决方案1】:

也许你正在寻找这个。您可以使用 reorder() 并启用选项 FUN。在这种情况下,如果我理解正确,您可以使用max。我提供了两个选项:

library(ggplot2)
library(dplyr)
#Code 1
ggplot(data = iris ,aes(x = reorder(Species,Sepal.Width,FUN = max),y=Sepal.Width)) +
  geom_violin(fill='gray')+xlab('Species')

输出:

或者这个:

#Code 2
ggplot(data = iris ,aes(x = reorder(Species,-Sepal.Width,FUN = max),y=Sepal.Width)) +
  geom_violin(fill='gray')+xlab('Species')

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多