分位数-分位数图:比较多个建模变量与一个守恒变量ggplot2 R

时间:2018-11-25 19:10:26

标签: r ggplot2

样本数据

set.seed(10)
   dat <- data.frame(Observed = rnorm(20), 
                    sim1= rnorm(20), sim2 = rnorm(20),
                    sim3 = rnorm(20),sim4 = rnorm(20),
                    sim5 = rnorm(20),sim6 = rnorm(20)
                   )

stat_qqgeom_qq似乎旨在比较一个或多个向量,例如sim1-6向量达到理论分布。我看不到如何使用它来比较多个不同的向量,例如将sim1sim6Observed进行比较?

但是,我有一个模型(sim1-sim6)的输出,并想检查建模输出是否与Observed进行比较。

如何在ggplot2中完成此操作? y-axissim1-sim6,而x-axisObserved

记住:我正在比较simObserved中的每一个。

欢迎提出所有建议。

编辑:BASE R中的解决方案

set.seed(10)
   dat1 <- data.frame(Observed = rnorm(20), sim1= rnorm(20), sim2 = rnorm(20),sim3 = rnorm(20),sim4 = rnorm(20),sim5 = rnorm(20),sim6 = rnorm(20))
   #
   # create a QQ-plot of Sims as a function of Observed
   qqplot(dat1$Observed, dat$sim1, xlim = range(dat1), ylim = range(dat1), 
          xlab = "Observed", ylab = "Sims....")
   # create a diagonal line
   abline(a = 0, b = 1)
   # add the points of sims
   points(sort(dat1$Observed), sort(dat1$sim2), col = "red")
   points(sort(dat1$Observed), sort(dat1$sim3), col = "blue")
   points(sort(dat1$Observed), sort(dat1$sim4), col = "green")
   points(sort(dat1$Observed), sort(dat1$sim5), col = "magenta")
   points(sort(dat1$Observed), sort(dat1$sim6), col = "purple")

enter image description here

我的问题:

如何在ggplot2中完成同一任务?

1 个答案:

答案 0 :(得分:2)

你的意思是这样吗?

library(ggplot2)
library(reshape2)
data = melt(dat, id.vars = "Observed")
ggplot(data) +geom_qq(aes(sample=value)) + 
  geom_qq_line(aes(sample=Observed)) +
  facet_wrap(~variable)

enter image description here

更新

    data = melt(dat, id.vars = "Observed")
ggplot(data) +geom_qq(aes(sample=value, color = variable)) + 
  geom_qq_line(aes(sample=Observed))

enter image description here