r - 如何在一张图上绘制多个密度?
问题描述
我正在尝试将不对称拉普拉斯分布的密度图绘制到一个图上。
我已经定义了两种密度,一种用于不对称参数 = 0.5,另一种用于参数 = 0.25。
我的绘图语句正确绘制了一张图。
我想把两者放在同一张图上,也许还有第三张?
library(ald)
sseq = seq(-8,8,0.01)
dens = dALD(y=sseq,mu=0,sigma=1,p=0.25)
dens2= dALD(y=sseq,mu=0,sigma=1,p=0.5)
plot(sseq,dens,type="l",lwd=2,col="red",xlab="u",ylab=parse(text="f[p](u)"), main="ALD Density function")
legend("topright", legend=c("ALD for p=0.5"),lty=c(1),
lwd=c(1),col=c("red"),title="Values for different quantiles:")
解决方案
您可以使用 ggplot2 轻松做到这一点:
library(ggplot2)
ggplot(data.frame(sseq, dens, dens2)) +
geom_line(aes(sseq, dens, color = 'ALD for p=0.5')) +
geom_line(aes(sseq, dens2, color = 'ALD for p=0.25')) +
labs(x="u",y=parse(text="f[p](u)"),
title="ALD Density function") +
scale_color_discrete(name="Values for different quantiles:") +
theme_minimal()
推荐阅读
- r - %>% .$column_name 等效于 R 基管道 |>
- c++ - 如何使 sf::Vector2f transform(float t) 速度更快?
- linux - 无法使用 linux 终端删除 linux 目录
- swift - Swift:“'Any'类型的值没有成员'map'”,数组返回为Any类型
- css - 为什么插入符号会触发 css lint 错误?
- python - Python:检查列表中的每个文件是否存在于目录中
- python - Unstacked DataFrame太大,导致int32溢出
- java - 从图库意图中选择后,图像未显示在图像视图中
- identityserver4 - 如何从 Identity Server 应用程序执行外部身份验证请求?
- computer-vision - CVAT coco 注释 json - iscrowd 选项