在ggplot2中带有分面的qqline [英] qqline in ggplot2 with facets

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

问题描述

问题表明如何使在一个GGPLOT2一个qqline qqplot,但答案似乎只适用于绘制整个数据集在一个图表中。



我想要一种快速比较这些图表的数据子集的方法。也就是说,我想用图面上的qqlines创建qqplots。所以在下面的例子中,所有9个地块都会有线条,每个地块都有自己的截距和坡度。

  df1 =数据.frame(x = rnorm(1000,10),
y = sample(LETTERS [1:3],100,replace = TRUE),
z = sample(letters [1:3],100,replace = TRUE))

ggplot(DF1,AES(样品= X))+
stat_qq()+
facet_grid(Y〜Z)

解决方案

您可以试试这个:

  library(plyr)

#创建一些数据
set.seed(123)
df1 < - data.frame(vals = rnorm(1000,10),
y = sample(LETTERS [1:3],1000,replace = TRUE),
z = sample(字母[1:3],1000,replace = TRUE))

#calculate每个组的正常理论分位数
df2 < - ddply(.data = df1,.variables =。(y,z),function(dat){
q < -qqnorm(dat $ vals,plot = FALSE)
dat $ xq< -q $ x
dat
}


#绘制与理论位数
ggplot样本值(数据= DF2,AES(X = XQ,Y =瓦尔斯))+
geom_point()+
geom_smooth(方法= LM, se = FALSE)+
xlab(理论)+
ylab(Sample)+
facet_grid(y〜z)
pre>


This question showed how to make a qqplot with a qqline in ggplot2, but the answer only seems to work when plotting the entire dataset in a single graph.

I want a way to quickly compare these plots for subsets of my data. That is, I want to make qqplots with qqlines on a graph with facets. So in the following example, there would be lines for all 9 plots, each with their own intercept and slope.

df1 = data.frame(x = rnorm(1000, 10),
                 y = sample(LETTERS[1:3], 100, replace = TRUE),
                 z = sample(letters[1:3], 100, replace = TRUE))

ggplot(df1, aes(sample = x)) +
  stat_qq() +
  facet_grid(y ~ z)

解决方案

You may try this:

library(plyr)

# create some data
set.seed(123)
df1 <- data.frame(vals = rnorm(1000, 10),
                  y = sample(LETTERS[1:3], 1000, replace = TRUE),
                  z = sample(letters[1:3], 1000, replace = TRUE))

# calculate the normal theoretical quantiles per group
df2 <- ddply(.data = df1, .variables = .(y, z), function(dat){
             q <- qqnorm(dat$vals, plot = FALSE)
             dat$xq <- q$x
             dat
}
)

# plot the sample values against the theoretical quantiles
ggplot(data = df2, aes(x = xq, y = vals)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  xlab("Theoretical") +
  ylab("Sample") +
  facet_grid(y ~ z)

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

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