我无法弄清楚如何将正确的密钥添加到下面的图中。请帮忙。键应该有文本c("Totalcatch","Effort","Catchrate")
后面跟着适当颜色的行(对于Effort和Catchrate),以及相应的pch(一个矩形用于totalcatch,15和16用于其他两个变量。)
library(latticeExtra)
Catchrate<-c(0.05, 0.18, 0.25, 0.31, 0.32, 0.34, 0.35)
totalcatch<-c(20, 30, 20, 30, 40, 50, 60)
Effort<-c(0, 1000, 2000, 3000, 4000, 5000, 6000)
year<-(2009:2015)
p1=barchart(totalcatch~as.factor(year),horizontal=FALSE,ylim=c(0,70),xlim=c(0,7.5),scales=list(y=list(axs="i"),x=list(at=1:7,labels=year)))
p2=xyplot(Effort~as.factor(year),type="b",col=2,scales=list(y=list(axs="i",draw=TRUE)),pch=15,par.settings=simpleTheme(col=2),ylab=list(label="Effort",col=2))
p3=xyplot(Catchrate~as.factor(year),type="b",pch=16,col=1,scales=list(y=list(draw=FALSE,axs="i",limit=c(0,0.4))))+layer(panel.axis(side="left",half=FALSE,tck=1,ticks=TRUE,at=seq(0.05,0.35,by=0.05),label=seq(0.05,0.35,by=0.05),line.col=1,text.col=1))
doubleYScale(doubleYScale(p1,p2,add.ylab2=TRUE,style2=4),p3)+layer(panel.text(0.8,40,label="Catchrate",srt=90))
如果你可以在这个过程中简化我的代码,它也会很棒。
答案 0 :(得分:0)
这似乎在某种程度上起作用。但如果有人有更简单的解决方案,请发帖。
library(latticeExtra)
Catchrate<-c(0.05, 0.18, 0.25, 0.31, 0.32, 0.34, 0.35)
totalcatch<-c(20, 30, 20, 30, 40, 50, 60)
Effort<-c(0, 1000, 2000, 3000, 4000, 5000, 6000)
year<-(2009:2015)
key=list(text=list("Totalcatch",col="#0080ff"),rectangle=list(col="#0080ff"),text=list("Catchrate"),lines=list(col=1,type="b",pch=16),text=list("Effort",col=2),lines=list(col=2,type="b",pch=15),corner=c(0,1))
p1=barchart(totalcatch~as.factor(year),horizontal=FALSE,ylim=c(0,70),xlim=c(0,7.5),scales=list(y=list(axs="i"),x=list(at=1:7,labels=year)),key=key,col="#0080ff",ylab="Totalcatch")
p2=xyplot(Effort~as.factor(year),type="b",col=2,scales=list(y=list(axs="i",draw=TRUE)),pch=15,par.settings=simpleTheme(col=2),ylab=list(label="Effort",col=2))
p3=xyplot(Catchrate~as.factor(year),type="b",pch=16,col=1,scales=list(y=list(draw=FALSE,axs="i",limit=c(0,0.4))))+layer(panel.axis(side="left",half=FALSE,tck=1,ticks=TRUE,at=seq(0.05,0.35,by=0.05),label=seq(0.05,0.35,by=0.05),line.col=1,text.col=1))
doubleYScale(doubleYScale(p1,p2,add.ylab2=TRUE,style2=4),p3)+layer(panel.text(0.8,40,label="Catchrate",srt=90))