r - 如何通过ggplot中每个方面的x轴值对箱线图进行排序?
问题描述
我想按每个方面(此处的类)中的 x 轴值(此处为 hwy)对箱线图进行排序。我尝试了两种方法,但都失败了:
library(tidyverse); library(forcats)
mpg %>%
ggplot(aes(x = hwy, y = fct_reorder(trans, hwy, median))) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
mpg %>%
group_by(class) %>%
mutate(trans = fct_reorder(trans, hwy, median)) %>%
ungroup() %>%
ggplot(aes(x = hwy, y = trans)) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
我在这里想念什么?
解决方案
谢谢Tung,那个链接给了我线索!tidytext 中的 reorder_within 函数在这里很有用:
mpg %>%
ggplot(aes(x = hwy, y = tidytext::reorder_within(trans, hwy, class, median))) +
geom_boxplot() +
facet_wrap(~class, scales = "free_y")
...但现在唯一的问题是文本 _class 附加到图表上的每个 y 值?有没有办法解决这个问题?
推荐阅读
- node.js - TypeOrm 迁移 - 错误:找不到模块
- servlets - 在 servlet 过滤器中创建错误页面会导致错误“已获得写入器”
- git - 重置/恢复 git 存储库为空
- requirejs - 间歇性未捕获类型错误:无法读取未定义的 RequireJS 优化器 Owl Carousel 的属性“fn”
- ssl - 具有 Cloudflare 域的 Ngrok
- reactjs - Keycloak承诺没有解决?
- python - sqlalchemy中嵌套连接的正确方法
- c - 如何将字符串分解为特定字符
- python - 尽管 sys.exit() 代码不同,Python 脚本仍返回代码 244
- c# - 如何在 C# 中重用字段值?