变化的线条颜色和多个(即组)图与R&#39s ggplot

时间:2012-03-13 13:04:54

标签: r ggplot2

我第一次使用ggplot的qplot函数,我无法让它工作(但我很沮丧地接近):

fit1<-structure(list(yval = c(2.2, 2.7, 3.1, 3.5, 3.7, 4.1, 4.2, 4.4, 
4.4, 4.5, 4.5, 4.7, 4.8, 4.8, 4.9, 4.9, 4.9, 4.9, 5, 5, 5, 5.1, 
5.1, 5.1, 5.1, 5.1, 5.1, 5.1, 5.2, 5.2, 5.2, 5.3, 5.3, 5.4, 5.4, 
5.5, 5.6, 5.6, 5.7, 5.8, 5.8, 5.8, 5.8, 5.9, 6, 6, 6, 6.1, 6.1, 
0.77, 1, 1.9, 2.5, 2.9, 3.1, 3.3, 3.3, 3.2, 3.2, 3.2, 3.1, 2.7, 
2.6, 2.5, 2.2, 2, 1.9, 1.7, 1.7, 1.6, 1.6, 1.1, 1.1, 1.1, 1.1, 
0.99, 0.95, 0.94, 0.82, 0.81, 0.8, 0.78, 0.77, 0.77, 0.77, 0.77, 
0.78, 0.79, 0.79, 0.8, 0.8, 0.8, 0.81, 0.82, 0.84, 0.84, 0.85, 
0.87, 0.87, 0.7), zval = c(0.00067, 7e-04, 0.00071, 0.00072, 
0.00072, 0.00072, 0.00072, 0.00071, 7e-04, 7e-04, 7e-04, 0.00068, 
0.00067, 0.00067, 0.00066, 0.00065, 0.00065, 0.00065, 0.00065, 
0.00065, 0.00065, 0.00067, 0.00067, 0.00067, 0.00068, 0.00068, 
0.00069, 0.00069, 0.00071, 0.00072, 0.00072, 0.00072, 0.00072, 
0.00071, 0.00069, 0.00067, 0.00064, 0.00062, 0.00062, 0.00061, 
0.00061, 0.00061, 0.00062, 0.00063, 0.00067, 0.00069, 0.00071, 
8e-04, 0.00081, 0.92, 0.0031, 0.0022, 0.0016, 0.0012, 0.001, 
0.00074, 0.00067, 0.00066, 0.00066, 0.00066, 0.00067, 0.00078, 
0.00081, 0.00083, 0.00086, 0.00085, 0.00082, 0.00075, 0.00072, 
7e-04, 0.00067, 0.00067, 7e-04, 0.00072, 0.00093, 0.0014, 0.0018, 
0.0018, 0.0056, 0.0068, 0.0079, 0.01, 0.019, 0.024, 0.035, 0.043, 
0.062, 0.071, 0.083, 0.096, 0.097, 0.097, 0.11, 0.12, 0.14, 0.15, 
0.15, 0.17, 0.18, 1), xval = c(0.038, 0.36, 0.61, 0.88, 1, 1.4, 
1.7, 1.9, 2, 2, 2.1, 2.6, 2.8, 2.8, 3.2, 3.4, 3.5, 3.7, 3.7, 
3.8, 3.8, 4.6, 4.6, 4.7, 4.8, 5, 5.1, 5.1, 5.8, 5.9, 6, 6.2, 
6.8, 7, 7.4, 7.7, 8.2, 8.4, 8.7, 9, 9, 9, 9.2, 9.4, 9.8, 9.9, 
10, 10, 10, 45, 0.038, 0.36, 0.61, 0.88, 1, 1.4, 1.7, 1.9, 2, 
2, 2.1, 2.6, 2.8, 2.8, 3.2, 3.4, 3.5, 3.7, 3.7, 3.8, 3.8, 4.6, 
4.6, 4.7, 4.8, 5, 5.1, 5.1, 5.8, 5.9, 6, 6.2, 6.8, 7, 7.4, 7.7, 
8.2, 8.4, 8.7, 9, 9, 9, 9.2, 9.4, 9.8, 9.9, 10, 10, 10, 45), 
grp = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2)), .Names = c("yval", "zval", "xval", 
"grp"), row.names = c(NA, -100L), class = "data.frame")     
myfun2<-function(x) ifelse(x<0.05,1,0)
    p1<-qplot(y=fit1$yval,x=fit1$xval,xlim=c(0,10),ylim=c(1/4,10),geom="path",colour=myfun2(fit1$zval),group=fit1$grp)+opts(legend.position="none")
    p1

现在这几乎是我想要的,除了颜色。我想要颜色 来自

coli<-qualiPalette(max(fit1$grp)*2,name=c("Paired")) 

with coli[1] for grp==1 and myfun2==0
with coli[2] for grp==1 and myfun2==1
with coli[3] for grp==2 and myfun2==0
with coli[4] for grp==2 and myfun2==1

就像现在一样,如果myfun2 == 0,我的颜色是大肠杆菌[1],如果myfun2 == 1,我的颜色是大肠杆菌[2]。

提前致谢

1 个答案:

答案 0 :(得分:3)

我会在fit1中为颜色映射创建一个额外的因子。

e.g。

fit1$c = paste(myfun2(fit1$zval), fit1$grp)

ggplot(fit1, aes(xval, yval, colour = c, group = grp )) + geom_path() + 
  xlim(c(0,10)) + ylim(c(1/4,10)) + opts(legend.position = "none")