2017-05-02 3 views
1

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 

enter image description here

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 

enter image description here

+0

@BenBolker da Sie hatte ein ähnliches Problem, vielleicht können Sie einige Hinweise anbieten? :) – DeltaIV

+1

meinst du 'p + stat_qq (aes_string (sample = sample_var, farbe = group_var), geom =" linie ")'? – mtoto

+0

@ 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

Antwort

3

den Code Drehen des qqlines in eine Funktion zu berechnen und dann lapply mit einer separaten data.frame für Ihre qqlines ist ein Ansatz zu schaffen.

library(dplyr) 
library(ggplot2) 
library(broom) ## for augment() 

set.seed(1001) 
N <- 1000 
G <- 3 
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 

qqlines <- function(vec, group) { 
    x <- qnorm(c(0.25, 0.75))  
    y <- quantile(vec[!is.na(vec)], c(0.25, 0.75)) 
    slope <- diff(y)/diff(x) 
    int <- y[1] - slope * x[1] 
    data.frame(slope, int, group) 
} 


slopedf <- do.call(rbind,lapply(unique(dda$group), function(grp) qqlines(dda[dda$group == grp,sample_var], grp))) 



# now plot with ggplot2 
p <- ggplot(dda)+stat_qq(aes_string(sample=sample_var, colour=group_var)) + 
    geom_abline(data = slopedf, aes(slope = slope, intercept = int, colour = group)) 
p 

enter image description here

+0

Schön! Ich hätte ein paar Unterstützungsvektoren 'used_groups <- unique (dda $ group)' und 'samples <- dda [dda $ group == grp, sample_var]' benutzt, um den Code etwas lesbarer zu machen, aber wirklich gut sowieso. Es wird einfach sein, dies in eine wiederverwendbare Funktion zu verwandeln. +1! – DeltaIV

+1

Froh, dass es nützlich ist. Du hast recht, mein Code braucht ein bisschen Arbeit. Aber in meiner Entschuldigung war es nach meiner Schlafenszeit, als ich es fertig war :-) –

Verwandte Themen