带有 ggplot2 的多面 qqplots [英] Faceted qqplots with ggplot2

查看:39
本文介绍了带有 ggplot2 的多面 qqplots的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据:

datapoints1 = data.frame(categ=c(rep(1, n), rep(2, n)), vals1=c(rt(n, 1, 2), rnorm(n, 3, 4)))
datapoints2 = data.frame(categ=c(rep(1, n), rep(2, n)), vals2=c(rt(n, 5, 6), rnorm(n, 7, 8)))

使用 ggplot2,如何使用 facet 功能在单个命令中创建两个 QQplot,即一个带有两个 t 样本,另一个有两个高斯样本?

Using ggplot2, how can I use the facet functionality to create in a single command two QQplots, i.e. one with the two t samples, the other with the two Gaussian samples?

推荐答案

首先,合并两个数据框:

First, combine both data frames:

dat <- cbind(datapoints1, vals2 = datapoints2[ , 2])

然后,对数据进行排序:

Then, sort the data:

dat_sort <- do.call("rbind", lapply(unique(dat$categ), FUN = function(x) {data.frame(categ = x, vals1 = sort(dat$vals1[dat$categ == x]), vals2 = sort(dat$vals2[dat$categ == x]))}))

如果两个样本向量的长度相同,这很简单:

It is simple if both sample vectors are of the same length:

ggplot() + 
 geom_point(data = dat_sort, aes(x = vals1, y = vals2)) +
 facet_wrap( ~ categ, scales = "free")

n = 1000 的示例:

这篇关于带有 ggplot2 的多面 qqplots的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆