2015-11-06 41 views
0

enter image description here添加因子變量到連續多情節

我已經使用代碼

plot(kitedata_2[, 3:6]) 

創建此情節我想的一個因素添加到情節,可以使用不同的顏色來區分點。即將每個測量分爲男性和女性的值。

任何想法?

+1

您可以嘗試爲您的調用添加諸如'col = ifelse(kitedata_2 $ sex ==「female」,「red」,「blue」)'。或者提供一個可重複的例子。 – Heroka

+0

您可以隨時在配對功能中使用面板選項。 – dc3

回答

0

ifelse功能是你在這種情況下的朋友。

sample_data <- mtcars[,1:4] 
colors <- ifelse(mtcars$am, "blue", "pink") 
plot(sample_data, col = colors) 

enter image description here

對於您要三種以上的顏色的情況下,從memisccases功能將執行類似的功能:

library(memisc) 
sample_data <- mtcars[,c(1, 3, 5)] 
colors <- cases(
    mtcars$cyl == 4 -> "red", 
    mtcars$cyl == 6 -> "blue", 
    mtcars$cyl == 8 -> "green" 
) 
plot(sample_data, col = colors) 

enter image description here

但是,您的最強大和最靈活的工具是使用ggplot2包。這是基於使用ggplot2變量完成着色的一種方法:

library(ggplot2) 
qplot(x = mpg, y = drat, col = factor(cyl), geom = "point", data = mtcars) 

enter image description here

學習如何使用ggplot2包將是一個有價值的長期投資。