r - 在更改 geom_bar 顺序时保持一致的颜色?
问题描述
在根据另一个值订购 geom_bar() 时,我正在努力保持图表之间的颜色一致性。
期望:
- A & B 颜色在图表之间保持一致
- 每个地块的 A & B 降序排列
实际的:
- A & B 在图表之间交换颜色
library(tidyverse)
dat <- data.frame(x = c("A","B"), y = c(1,2), z = c(4,3))
# Y
dat %>%
mutate(x = fct_reorder(x, y,`.desc` = TRUE)) %>%
ggplot(aes(x = x, y = y, fill = x)) +
geom_bar(stat = "identity")
# Z
dat %>%
mutate(x = fct_reorder(x, z,`.desc` = TRUE)) %>%
ggplot(aes(x = x, y = z, fill = x)) +
geom_bar(stat = "identity")
由reprex 包(v0.3.0)于 2020 年 8 月 31 日创建
解决方案
这种方法最终对我有用:
dat %>%
arrange(desc(z)) %>%
ggplot(aes(x = reorder(x, desc(z)), y = z, fill = x)) +
geom_bar(stat = "identity")
推荐阅读
- android - Android : XMPP MUCLight Smack 监听群聊消息
- scala - 使用 sparkSession,当我读取 parquet 文件时,我得到 type not yet supported 错误消息
- blueprism - 环境问题。将 URL 传递给 BluePrism 中的 Obj 和 Process Studio 时的变量
- ios - 在 iOS Objective C 中处理位置权限
- facebook - 获取在 Facebook 上标记用户的所有评论
- django - 加快“在多边形中查找点”-geodjango 中的查询
- android - 我可以使用 dispatchTouchEvent 对特定布局使用触摸吗?
- android - 尽管使用了 AsyncTask,但 Android 主线程/UI 线程被阻止(阅读类似的帖子,但我对这些不满意)
- selenium - 使用 ResponseCode 时在 null 上调用成员函数 getInternalRequest()
- jenkins - java.lang.NoSuchMethodError:在步骤中找不到这样的 DSL 方法“$”