如何更改ggplot中美学图层的顺序? [英] How to change the order of aesthetic layers in ggplot?

查看:183
本文介绍了如何更改ggplot中美学图层的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I change the order of aestetics layers? Here's and example

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)

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

This code plots this image. Here, the green (1) layer is above the red (0) layer. How can I place the red (0) layer on top of the green (1)?

我尝试过

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

但这会导致切换位置的颜色!

but this results in switching colors and positions!

推荐答案

您可以重新排列factor的级别并添加颜色调整:

You could re-order the levels of your factor and add the color adjustment:

dat %>% ggplot(aes(x = rt, 
                   fill = factor(acc, levels = c(1,0)))) + 
  geom_density(aes(y= ..count..*0.03), alpha = 0.6)+
scale_fill_manual(values = c("1" = "#00BFC4", "0" = "#F8766D"))

这篇关于如何更改ggplot中美学图层的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆