首页 > 解决方案 > 如何在一张图上绘制多个密度?

问题描述

我正在尝试将不对称拉普拉斯分布的密度图绘制到一个图上。

我已经定义了两种密度,一种用于不对称参数 = 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:")

标签: rplot

解决方案


您可以使用 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()

在此处输入图像描述


推荐阅读