在ggplot2/geom_density中,如何用不同的颜色填充密度区域

In ggplot2/geom_density, how to fill densiyt area with differenc colors

在ggplot2/geom_density中,我想用'YELLOW'填充wt>4的区域,但是失败了(有些区域不在>4中,填充颜色也是黄色。如附图).有人可以帮忙吗?谢谢!

library(tidyverse)
mtcars %>% ggplot(aes(x=wt))+
  geom_density(aes(fill=if_else(wt<4,'red','yellow')))

密度图显示分布,因此添加尾部。这使得 4 以下的区域看起来像是黄色的。使用不添加它们的直方图怎么样?

为了比较,

library(tidyverse)

mtcars %>% ggplot(aes(x=wt)) +
  geom_histogram(aes(fill=if_else(wt < 4,'darkred','blue')), bins = 50) +
  geom_density(aes(fill=if_else(wt < 4,'red','yellow')), alpha = 0.6) +
  scale_colour_identity() +
  scale_fill_identity()