在 ggplot2/geom_density 中,我想用“YELLOW”填充 wt >4 的區域,但它失敗了(某些區域不在 >4 中,填充顏色也是黃色。如附圖所示)。有人可以幫忙嗎?謝謝!
library(tidyverse)
mtcars %>% ggplot(aes(x=wt))
geom_density(aes(fill=if_else(wt<4,'red','yellow')))

uj5u.com熱心網友回復:
密度圖顯示分布,因此添加了尾部。這使得它看起來低于 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()

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/420815.html
標籤:
