2015-11-06 68 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包将是一个有价值的长期投资。