Reorder() 未正确重新排序 ggplot 中的因子变量

标签 r ggplot2

我很困惑为什么箱线图在此图中没有排序:

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

如何根据最高-最低中位数(从左到右)对箱线图进行排序?

最佳答案

因为你没有将其设为有序因子。尝试一下

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

enter image description here

关于Reorder() 未正确重新排序 ggplot 中的因子变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16622979/

相关文章:

r - 使用gganimate导出gif

r - 跨不同 ggplot2 map 的一致 alpha 比例

r - 在R错误中使用ggplot函数:找不到函数ggplot

regex - 如何获取R中两个字符第一次出现之间的模式?

python - rpy2 转换矩阵 -> DataFrame

r - Quantstrat : argument "price" is missing, 中的问题,没有默认值

r - 安装ggp​​lot “subscript out of bounds” 错误

r - 使用 ggplot 绘制带有图例的图像

r - 如何使用隐藏列对 DataTable 进行排序

Rmarkdown 文档不能用 python block 编译