R + xyplot +带有多个引用的键

时间:2013-10-06 14:17:06

标签: r key lattice

我的情节类似于:

w=rnorm(9)
z=rnorm(9)
A=as.factor(c(rep(c("A1","A2","A3"),3)))
B=as.factor(c(rep("B1",3),rep("B2",3),rep("B3",3)))
C=as.factor(c("C1","C1","C2","C2","C3","C3","C1","C2","C3"))

xyplot(w~z,type="p",cex=1.4,
       panel=function(x, y, ...) {
         panel.xyplot(x=z[1], y=w[1],pch=15,col="red",...);
         panel.xyplot(x=z[2], y=w[2],pch=15,col="green",...);
         panel.xyplot(x=z[3], y=w[3],pch=15,col="blue",...);
         panel.xyplot(x=z[4], y=w[4],pch=16,col="red",...);
         panel.xyplot(x=z[5], y=w[5],pch=16,col="green",...);
         panel.xyplot(x=z[6], y=w[6],pch=16,col="blue",...);
         panel.xyplot(x=z[7], y=w[7],pch=17,col="red",...);
         panel.xyplot(x=z[8], y=w[8],pch=17,col="green",...);
         panel.xyplot(x=z[9], y=w[9],pch=17,col="blue",...);
         ltext(x=x, y=y+0.1, labels=C)
})

现在,我一直在努力尝试获得这样一个关键字:

enter image description here

我尝试使用键功能,使用图例功能,尝试在同一个图形中创建多个键...我迷路了!!

1 个答案:

答案 0 :(得分:1)

我知道你在lattice中这样做了,但是ggplot让这件事变得非常简单。

my.data<-data.frame(w,z,A,B,C)
ggplot(my.data,aes(x=w,y=z,colour=A,shape=B,label=C)) + 
  geom_point(size=3) + 
  geom_text(hjust=-0.2,vjust=-0.2)

enter image description here

我很难弄清楚如何获得标签指南。但后来我意识到,如果你有标签,为什么还需要指南?