2016-10-01 112 views
1

我正在嘗試使用ggplot2包進行繪圖「理論分位數」X「標準化殘差」。標準化殘差x ggplot2包中的理論分位數圖

我有一個lm()模型,我用這個情節

library(ggplot2) 

model<-lm(mpg~cyl+disp+hp+drat+wt, data=mtcars) 

p2<-ggplot(model, aes(qqnorm(.stdresid)[[1]], .stdresid))+geom_point(na.rm = TRUE) 
p2<-p2+geom_abline(aes(qqline(.stdresid)))+xlab("Theoretical Quantiles")+ylab("Standardized Residuals") 
p2<-p2+ggtitle("Normal Q-Q")+theme_bw() 
p2 

此代碼,我在這裏https://rpubs.com/therimalaya/43190創立做出的情節,而是返回一個錯誤

Error: Aesthetics must be either length 1 or the same as the data (3110): x 

,我不能改變xlabylab的名稱。我如何解決它?

+0

能否請您做這個重現? –

+0

@ Hack-R我該怎麼做? – Roland

+0

在問題中提供必要的代碼和數據以重現問題。我知道你提供了一個鏈接,但他們希望它在問題中,甚至使用鏈接有點不清楚,因爲你必須尋找你從另一個函數中獲取代碼的位置,找到相應的模型並重命名對象等。 http://stackoverflow.com/help/mcve我這次繼續爲你修復它。 –

回答

1
model<-lm(mpg~cyl+disp+hp+drat+wt, data=mtcars) 

library(ggplot2) 

p2 <- ggplot(model, aes(qqnorm(.stdresid)[[1]], .stdresid))+geom_point(na.rm = TRUE) 
p2 <- p2+geom_abline()+xlab("Theoretical Quantiles")+ylab("Standardized Residuals") 
p2 <- p2+ggtitle("Normal Q-Q")+theme_bw() 
p2 

enter image description here

+0

的副本真棒,你知道什麼是在abline函數中的問題? – Roland

+0

@Roland樂於助人。 「aes」的說法不合適,顯然完全沒有道理。這個例子是錯誤的,可能是因爲它們是從早先的包含'aes'調用的行復制而來的,只是部分刪除了這個參數。我建議通過電子郵件發送作者,以便他們可以修復他們的示例,儘管此時已有2年的歷史。 –