我用来自不同数据帧的多个geom对象制作了一个ggplot。我有一个模型输出,该输出为数据框格式,具有深度(y轴值)列,2.5%,50%和97.5%的置信区间范围。此处缩短了数据集:
head(model_output)
Depth 2.5% 50% 97.5%
1 32.00000 767.000 888.5 977.025
2 33.30303 1085.925 1207.0 1303.025
3 34.60606 1356.000 1431.0 1522.000
4 35.90909 1362.975 1444.0 1531.000
5 37.21212 1369.975 1453.0 1548.000
6 38.51515 1376.000 1460.5 1563.025
我还具有许多样本的概率密度函数,这里显示了三个。
head(sample1)
positions ageGrid densities
1 32 686 1.175521e-05
2 32 687 1.543893e-05
3 32 688 1.812884e-05
4 32 689 1.963192e-05
5 32 690 2.125031e-05
6 32 691 2.125031e-05
head(sample2)
positions ageGrid densities
1 75 1538 1.084429e-05
2 75 1539 1.617676e-05
3 75 1540 1.963299e-05
4 75 1541 2.681821e-05
5 75 1542 3.031380e-05
6 75 1543 3.534482e-05
head(sample3)
positions ageGrid densities
1 75 2349 1.204547e-05
2 75 2350 1.673264e-05
3 75 2351 2.306552e-05
4 75 2352 3.154642e-05
5 75 2353 4.280118e-05
6 75 2354 5.759857e-05
我可以将所有这些与下面的代码一起绘制。置信区间为geom_ribbon和geom_line对象,三个样本pdf为geom_ridgeline对象。
ggplot(data = model_output, aes(x = `50%`, y = Depth))+
geom_ribbon(data = model_output, aes(xmin = `2.5%`, xmax = `97.5%`), fill = "grey50", colour = "grey50", alpha = 0.5)+
geom_line(data = model_output, aes(x = `50%`, y = Depth), colour = "black", size = 1)+
scale_y_reverse()+
scale_x_reverse()+
theme_bw()+
xlab("x axis")+
ylab("y axis")+
labs(title = "title", subtitle = "subtitle")+
geom_ridgeline(data = sample1,
aes(x = ageGrid, y = positions, height = densities*1000, group = 'sample1'),
fill = "steelblue", colour = "steelblue", alpha = 0.6)+
geom_ridgeline(data = sample2,
aes(x = ageGrid, y = positions, height = densities*1000, group = 'sample2'),
fill = "darkorange2", colour = "darkorange2", alpha = 0.6)+
geom_ridgeline(data = sample3,
aes(x = ageGrid, y = positions, height = densities*1000, group = 'sample3'),
fill = "steelblue", colour = "steelblue", alpha = 0.6)
我发现这个图很好,正是我想要的图。但是我也想要一个图例,它显示了我在此处应用的几何图形和填充颜色:
我已经尝试了以下问题/解答How to add legend to ggplot manually? - R,Missing legend with ggplot2 and geom_line,Adding manual legend in ggplot,Adding manual legend to a ggplot,并提出了以下代码,但给出以下代码无效我想要的传说...
ggplot(data = model_output, aes(x = `50%`, y = Depth))+
geom_ribbon(data = model_output, aes(xmin = `2.5%`, xmax = `97.5%`, fill = "grey50", colour = "grey50"), alpha = 0.5)+
geom_line(data = model_output, aes(x = `50%`, y = Depth, colour = "black"), size = 1)+
scale_y_reverse()+
scale_x_reverse()+
theme_bw()+
xlab("x axis")+
ylab("y axis")+
labs(title = "title", subtitle = "subtitle")+
scale_colour_manual(values = c("grey50", "black", "steelblue", "darkorange"),
labels = c("95% CI", "median (50% quartile)", "sample_type1", "sample_type2"))+
geom_ridgeline(data = sample1,
aes(x = ageGrid, y = positions, height = densities*1000, group = 'sample1',
fill = "steelblue", colour = "steelblue", alpha = 0.6),
show.legend = TRUE)+
geom_ridgeline(data = sample2,
aes(x = ageGrid, y = positions, height = densities*1000, group = 'sample2',
fill = "darkorange2", colour = "darkorange2", alpha = 0.6), show.legend = TRUE)+
geom_ridgeline(data = sample3,
aes(x = ageGrid, y = positions, height = densities*1000, group = 'sample3',
fill = "steelblue", colour = "steelblue", alpha = 0.6))
我是否在错误的位置使用彩色对象(是否在es参数中?)?我尝试融合数据集,但是对于像我这样的初学者来说有点复杂...