r - ggplot2 中的 qqline 与 facets

标签 r ggplot2

This问题显示了如何在 ggplot2 中使用 qqline 制作 qqplot,但答案似乎仅在将整个数据集绘制在单个图形中时才有效。

我想要一种方法来快速比较我的数据子集的这些图。也就是说,我想在带有刻面的图形上制作带有 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)

facet data

最佳答案

你可以试试这个:

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)

enter image description here

关于r - ggplot2 中的 qqline 与 facets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19599745/

相关文章:

r - 在 ggplot2 中居中对齐图例标题和图例键,用于长图例标题

r - 无法预测R中的线性模型

r - 按颜色组在 packcircle 函数中生成的分组图

r - 将 ls 中的多个 ggplots 保存到 R 中的一个和单独的文件中

r - 将空图添加到 facet,并与另一个 facet 结合

r - ggplot2:为 x 轴上的每个元素以及整个样本创建条形图

r - cutree 和 cluster 分支之间的分歧

r - R中的点和线图例

r - 我自己的 R 中的 K-means 算法

r - Knitr:使用 scales 包在 ggplot 中获取计数