[英]Maintain consistent colors while changing geom_bar order?
在根据另一个值订购 geom_bar() 时,我正在努力保持图表之间的颜色一致性。
期望:
实际的:
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 package (v0.3.0) 创建于 2020-08-31
这种方法最终对我有用:
dat %>%
arrange(desc(z)) %>%
ggplot(aes(x = reorder(x, desc(z)), y = z, fill = x)) +
geom_bar(stat = "identity")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.