2017-02-10 92 views
1

我一直在試圖繪製一條紅色線的正常Q-Q曲線,在R中用ggplot2。我一直無法添加圖例(乳膠數學)來解釋紅線ggplot2 legend,geom_abline和stat_gg

下面是基本的數字代碼:提前

ggplot(stdres_df, aes(sample=stdres)) + 
    stat_qq(color="black") + 
    geom_abline(slope = 1, 
       intercept = 0, color ="red") 

感謝。

+0

你能告訴到目前爲止,你已經嘗試了什麼? – joran

+0

這是我最後不成功的嘗試: 'code' ggplot(stdres_df,AES(樣品= stdres))+ stat_qq(顏色= 「黑色」)+ geom_abline(AES(線型= 「行」),斜率= 1, 截距= 0,顏色=「紅色」) scale_color_manual(「」, labels = c(「line」=「Test」)) – msx

+0

如果您提供最小數據集,這將有所幫助。 http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lizzie

回答

1

要獲得圖例,您需要將內容映射到aes()的調用中。在這種情況下,不存在用於映射到顏色的分組變量,但可以將顏色映射到要用於紅線的名稱。

該行默認爲紅色,因爲ggplot使用hcl(15, 100, 65)(淺紅色)作爲其默認調色板中的第一種顏色。但是,您可以使用scale_colour_manual將顏色設置爲任何您想要的顏色,如下例所示。例如:

set.seed(2) 
df <- data.frame(y = rnorm(200)) 

ggplot(df, aes(sample = y)) + 
    stat_qq() + 
    geom_abline(aes(slope=1, intercept=0, colour="Test"), size=1) + 
    coord_equal(xlim=range(df$y)) + 
    labs(colour="") + 
    scale_colour_manual(values="red") 

enter image description here

0

是這樣的?

ggplot() + 
stat_qq(aes(sample=1:100), distribution = qt,dparams = list(df=5)) + 
geom_abline(aes(linetype = "line"), slope = 1, intercept = 0, color ="red") + 
geom_text(aes(3, 0, label = "TEXT HERE"))