Ich brauche etwas ähnliches zu tun, was in dieser ausgezeichneten Frage gezeigt hat:QQ-Plot mit ggplot2 :: stat_qq, Farben, mehr Gruppen mit QQ Linien
Q-Q plot with ggplot2::stat_qq, colours, single group
aber leider gibt es einen kleinen Unterschied, die Blockierung mich. Anders als die ursprüngliche Frage möchte ich die Quantilberechnungen nach Gruppe trennen, aber ich möchte auch eine QQ-Zeile für jede Gruppe hinzufügen. Im Anschluss an den Code des OP, kann ich die Quantil-Quantil Plots von Gruppe erstellen:
library(dplyr)
library(ggplot2)
library(broom) ## for augment()
set.seed(1001)
N <- 1000
G <- 10
dd <- data_frame(x = runif(N),
group = factor(sample(LETTERS[1:G], size=N, replace=TRUE)),
y = rnorm(N) + 2*x + as.numeric(group))
m1 <- lm(y~x, data=dd)
dda <- cbind(augment(m1), group=dd$group)
sample_var <- "y"
group_var <- "group"
p <- ggplot(dda)+stat_qq(aes_string(sample=sample_var, colour=group_var))
p
Wie kann ich die Quantil-Quantil Linien für jede Gruppe hinzufügen? HINWEIS: Idealerweise möchte ich zur Laufzeit die Beispielspalte und die Gruppenspalte angeben. Deshalb habe ich aes_string
verwendet.
EDIT besser mein Problem zu klären, ich Code hinzufügen Quantil-Quantil Linien zu berechnen, wenn es nur eine Gruppe ist. Ich muss den Code auf mehrere Gruppen verallgemeinern.
library(dplyr)
library(ggplot2)
library(broom) ## for augment()
# this section of the code is the same as before, EXCEPT G = 1, because for
# now the code only works for 1 group
set.seed(1001)
N <- 1000
G <- 1
dd <- data_frame(x = runif(N),
group = factor(sample(LETTERS[1:G], size=N, replace=TRUE)),
y = rnorm(N) + 2*x + as.numeric(group))
m1 <- lm(y~x, data=dd)
dda <- cbind(augment(m1), group=dd$group)
sample_var <- "y"
group_var <- "group"
# code to compute the slope and the intercept of the qq-line: basically,
# I would need to compute the slopes and the intercepts of the qq-lines
# for each group
vec <- dda[, sample_var]
y <- quantile(vec[!is.na(vec)], c(0.25, 0.75))
x <- qnorm(c(0.25, 0.75))
slope <- diff(y)/diff(x)
int <- y[1] - slope * x[1]
# now plot with ggplot2
p <- ggplot(dda)+stat_qq(aes_string(sample=sample_var, colour=group_var))+geom_abline(slope = slope, intercept = int)
p
@BenBolker da Sie hatte ein ähnliches Problem, vielleicht können Sie einige Hinweise anbieten? :) – DeltaIV
meinst du 'p + stat_qq (aes_string (sample = sample_var, farbe = group_var), geom =" linie ")'? – mtoto
@ mtoto nein! Das verbindet die Plots in jeder Gruppe nur mit einer Linie. Ich möchte Quantil-Quantil-Linien zeichnen. Ich füge Code hinzu, der für eine Gruppe funktioniert, damit Sie eine Idee haben, was ich für mehr als eine Gruppe tun muss. – DeltaIV