我在ggplot2包和geom_density()函数中苦苦挣扎。问题是我不知道如何正确设置曲线的颜色。这是接近我的可复制代码:
all <- c(0.8661736, 0.8449721, 0.8586580, 0.8799846, 0.8616140, 0.8531594, 0.8729073, 0.8773759, 0.8642717, 0.8860595, 0.8841521, 0.8744709, 0.8809492, 0.8856219, 0.8633216, 0.8714409, 0.8835213, 0.8758926, 0.9095114, 0.9161364, 0.8894546, 0.8971624, 0.8942579, 0.8959117, 0.9106744, 0.9030894, 0.9007237, 0.9393489, 0.9420211, 0.9402343, 0.9189285, 0.9239876, 0.8917408, 0.6717553, 0.8830668, 0.7871568, 0.9071131, 0.7856184, 0.8116340, 0.8402714, 0.7690780, 0.8068024, 0.7277800, 0.4696930, 0.6245790, 0.8408001, 0.8625015, 0.8351262, 0.8825528, 0.8700201, 0.8227148, 0.7785036, 0.6172831, 0.8965587, 0.7348172, 0.7686254, 0.8259128, 0.7729904, 0.7915221, 0.8816634, 0.7394554, 0.6030000, 0.8935580, 0.8131855, 0.8964130, 0.9244451, 0.8686830, 0.8481270, 0.8614184, 0.8763432, 0.8589709, 0.8763704, 0.8064642, 0.8291552, 0.8132073, 0.8498027, 0.8734915, 0.8229706, 0.6669572, 0.4651795, 0.7175404, 0.8581294, 0.7833490, 0.5165467, 0.7759389, 0.8992295, 0.9144038, 0.8862598, 0.8369712, 0.8788088, 0.8932699, 0.8619486, 0.8154185, 0.8721308, 0.8634526, 0.8221695, 0.8770629, 0.8987442, 0.8949527, 0.8978255, 0.9029272, 0.8841347, 0.9072113, 0.9015006, 0.9065452, 0.8955123, 0.9104379, 0.8924010, 0.9068708, 0.8995275, 0.8956891, 0.8947945, 0.8958321, 0.7796944, 0.8937484, 0.9015196, 0.9175847, 0.8813532, 0.8783552, 0.9051779, 0.8997694, 0.8841438, 0.9004223, 0.8887506, 0.8979227, 0.8860958, 0.8892225, 0.8760619, 0.8777520, 0.8734815, 0.8799818, 0.8734606, 0.8733281, 0.8628469, 0.8436529)
soil <- c(0.8661736, 0.8449721, 0.8586580, 0.8799846, 0.8616140, 0.8531594, 0.8729073, 0.8773759, 0.8642717, 0.8860595, 0.8841521, 0.8744709, 0.8809492, 0.8856219, 0.8633216, 0.8714409, 0.8835213, 0.8758926, 0.9095114, 0.9161364, 0.8894546, 0.8971624, 0.8942579, 0.8959117, 0.9106744, 0.9030894, 0.9007237, 0.9393489, 0.9420211, 0.9402343, 0.9189285, 0.9239876, 0.8917408)
leaf <- c(0.6695863, 0.8830668, 0.7871568, 0.9071131, 0.7856184, 0.8116340, 0.8402714, 0.7690780, 0.8068024, 0.7277800, 0.4696930, 0.6245790, 0.8408001, 0.8625015, 0.8351262, 0.8825528, 0.8700201, 0.8227148, 0.7785036, 0.6172831, 0.8965587, 0.7348172, 0.7686254, 0.8259128, 0.7729904, 0.7915221, 0.8816634, 0.7394554, 0.6030000, 0.8935580, 0.8131855, 0.8964130, 0.9244451, 0.8686830, 0.8481270, 0.8614184, 0.8763432, 0.8589709, 0.8763704, 0.8064642, 0.8291552, 0.8132073, 0.8498027, 0.8734915, 0.8229706, 0.6669572, 0.4651795, 0.7175404, 0.8581294, 0.7833490, 0.5165467)
root <- c(0.7759389, 0.8992295, 0.9144038, 0.8862598, 0.8369712, 0.8788088, 0.8932699, 0.8619486, 0.8154185, 0.8721308, 0.8634526, 0.8221695, 0.8770629, 0.8987442, 0.8949527, 0.8978255, 0.9029272, 0.8841347, 0.9072113, 0.9015006, 0.9065452, 0.8955123, 0.9104379, 0.8924010, 0.9068708, 0.8995275, 0.8956891, 0.8947945, 0.8958321, 0.7796944, 0.8937484, 0.9015196, 0.9175847, 0.8813532, 0.8783552, 0.9051779, 0.8997694, 0.8841438, 0.9004223, 0.8887506, 0.8979227, 0.8860958, 0.8892225, 0.8760619, 0.8777520, 0.8734815, 0.8799818, 0.8734606, 0.8733281, 0.8628469, 0.8436529)
ggplot() +
geom_density(data=as.data.frame(all), aes(x=all, color='all', fill='all'), alpha = 0.2) +
geom_density(data=as.data.frame(soil), aes(x=soil, color='soil', fill='soil'), alpha = 0.2) +
geom_density(data=as.data.frame(leaf), aes(x=leaf, color='leaf', fill='leaf'), alpha = 0.2) +
geom_density(data=as.data.frame(root), aes(x=root, color='root', fill='root'), alpha = 0.2) +
scale_color_manual(name = 'Area', values=c('black', 'darkred', 'darkgreen', 'darkblue')) +
scale_fill_manual(name = 'Area', values=c('black', 'darkred', 'darkgreen', 'darkblue')) +
xlim(0.5,1) +
ggtitle('Density of Simpson Indices (1-D) per sample') +
xlab('Simpson Indices (1-D)') +
ylab('Density')
如您所见,我在同一张图上绘制了多个数据集,并尝试手动设置每条曲线的颜色。问题在于颜色不符合我在代码中给出的顺序(请参见图片)。我看不到代码有什么问题,但我期望“土壤”曲线为红色,“叶子”曲线为绿色,“根”曲线为蓝色。
我在这里想念什么?
谢谢。
nb:ggplot发送回警告:
Warning messages:
1: Removed 2 rows containing non-finite values (stat_density).
2: Removed 2 rows containing non-finite values (stat_density).
nb2:我还尝试了将我的4个向量融合到一个数据框中(第二个分类列中填充了“所有,叶,土壤,根”),结果相同。