我正在通过类绘制变量的分布,并且我在图表vlines中包含了2个分布的平均值,我希望使用相同的颜色方案对其进行着色。但是,vlines显示的颜色相同。
对于可重现的示例,我使用ROSE包中的hacide数据集。代码如下:
library(data.table)
library(ggplot2)
library(ROSE)
data(hacide)
train <- hacide.train
train[cls == 0, x1_mean := mean(x1)]
train[cls == 1, x1_mean := mean(x1)]
ggplot(train, aes(x = x1, fill = cls, color = cls) )+
geom_histogram(aes(y=..density..), position="identity",alpha = 0.4) + guides(color = FALSE) +
geom_density (alpha = 0.5)+ ggtitle("Distribution of Predictor x1 by Class") +
geom_vline(data = train, aes(xintercept = x1_mean)) +
xlab("Predictor x1") + labs(fill = "Class")