2014-02-17 55 views
1

我有一些數據,其中一個數據是性別,其中性別等於1表示女性,0表示男性。R中的點顏色

假設「reg」是我的線性模型。

當我做plot(studres(reg)~reg$fitted)它給了我想要的陰謀,但我想它給男性錯誤塗上藍色。我確信有一個簡單的方法可以做到這一點,但我仍然是R的新手,所以我會很感激任何幫助。

謝謝。

回答

3

plot(reg$fitted, studres(reg), col = dat$gender)

假設數據的形式爲:

> dat 
    y x gender 
1 0 0 female 
2 10 5 female 
3 15 10 male 
4 20 15 female 
5 24 20 male 
6 11 25 male 
7 30 30 female 
8 34 35 male 
+0

非常感謝Underminer – N4v

4

使用col選項。

此外,請嘗試?plot以獲得一些工作示例。

set.seed(12) 

mydata <- data.frame(
x = rnorm(50), 
y = rnorm(50), 
male = sample(c(0,1), 50, replace=T) 
) 

pointCol <- vector() 
pointCol[mydata$male==1] <- "blue" 

pointCol [MYDATA $雄性== 0] < - 「紅色」

plot(mydata$x,mydata$y,col=pointCol,pch=16) 

legend("topright", pch=16, c("male","female"), col=c("blue", "red"), title = 
"Keys") 

enter image description here

+0

非常感謝,企鵝。 – N4v