如何更改ggplot中的流变层顺序?

问题描述 投票:0回答:1

如何更改定格图层的顺序?这是示例

dat <- tibble (acc = rep(c(0,1), 200),
               rt = rnorm(400, 0.5, 0.1))

dat %>% ggplot(aes(x = rt, fill = factor(acc))) + 
  geom_density(aes(y= ..count..*0.03), alpha = 0.6)

enter image description here

此代码绘制此图像。此处,绿色(1)层位于红色(0)层上方。如何将红色(0)层放在绿色(1)之上?

r ggplot2 layer
1个回答
3
投票

您可以重新排列factor的级别:

dat %>% ggplot(aes(x = rt, 
                   fill = factor(acc, levels = c(1,0)))) + 
  geom_density(aes(y= ..count..*0.03), alpha = 0.6)

您将获得所需的顺序,但需要添加颜色调整。

© www.soinside.com 2019 - 2024. All rights reserved.