ggplot2中的qqline with facets

Nic*_*ick 8 r ggplot2

这个问题展示了如何在ggplot2中创建一个qqline的qqplot,但是当在单个图中绘制整个数据集时,答案似乎才有效.

我想要一种方法来快速比较这些数据子集的图.也就是说,我想在带有facet的图形上使用qqlines创建qqplots.因此,在下面的示例中,将有所有9个图的线,每个图都有自己的截距和斜率.

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)
Run Code Online (Sandbox Code Playgroud)

方面数据

Hen*_*rik 8

你可以试试这个:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述