Legend in multiple plot in R

吃可爱长大的小学妹 提交于 2019-12-08 11:12:55

问题


According to the comments from others, this post has been separated into several smaller questions from the previous version of this OP.

In the graph below, will you help me to (Newbie to R)

  • Custom legends according to the data they represent like filled for variable 1, circle points for variable 2 and line for variable 3 and their colors.
  • same letter size for the legend and axis-names.

The graph below is produced with the data in pdf device with following layout.

m <- matrix(c(1,2,3,3,4,5),nrow = 3,ncol = 2,byrow = TRUE)
layout(mat = m,heights = c(0.47,0.06,0.47))
par(mar=c(4,4.2,3,4.2))

#Codes for Fig A and B
...

#Margin for legend
par(mar = c(0.2,0.2,0.1,0.1))
    # Code for legend
...

#Codes for Fig C and D
...


回答1:


Using doubleYScale from latticeExtra and the data in the long format (see my previous answer), you can simplify the work:

  1. No need to create a custom layout to superpose many plots
  2. No need to create the legend manually

The idea is to create 2 separates objects and then merge them using doubleYScale. The latter will create the second axes. I hope I get your ploygon idea since it is not very clear why do you invert it in your OP.

library(latticeExtra)
obj1 <- xyplot(Variable~TimeVariable|Type,type='l',
               groups=time,               scales=list(x=list(relation='free'),
                                                      y=list(relation='free')),
               auto.key=list(columns = 3,lines = TRUE,points=FALSE) ,

       data = subset(dat.l,time !=1))
obj2 <- xyplot(Variable~TimeVariable|Type,
               data = subset(dat.l,time ==1),type='l',
               scales=list(x=list(alternating=2),
                           auto.key=list(columns = 3,lines = TRUE,points=FALSE),
                           y=list(relation='free')),
               panel=function(x,y,...){
         panel.xyplot(x,y,...)
         panel.polygon(x,y,col='violetred4',border=NA,alpha=0.3)
               })


doubleYScale(obj1, obj2, add.axis = TRUE,style1 = 0, style2 = 1)



回答2:


Try the following:

1) For the legend part

The data can be found on https://www.dropbox.com/s/4kgq8tyvuvq22ym/stackfig1_2.csv

The code I used is as follows:

data <- read.csv("stackfig1_2.csv")
library(Hmisc)

label1=c(0,100,200,300)
plot(data$TimeVariable2C,data$Variable2C,axes=FALSE,ylab="",xlab="",xlim=c(0,24),
     ylim=c(0,2.4),xaxs="i",yaxs="i",pch=19)
lines(data$TimeVariable3C,data$Variable3C)
axis(2,tick=T,at=seq(0.0,2.4,by=0.6),label= seq(0.0,2.4,by=0.6))
axis(1,tick=T,at=seq(0,24,by=6),label=seq(0,24,by=6))
mtext("(C)",side=1,outer=F,line=-10,adj=0.8)
minor.tick(nx=5,ny=5)

par(new=TRUE)
plot(data$TimeVariable1C,data$Variable1C,axes=FALSE,xlab="",ylab="",type="l",
     ylim=c(800,0),xaxs="i",yaxs="i")
axis(3,xlim=c(0,24),tick=TRUE,at= seq(0,24,by=6),label=seq(0,24,by=6),col.axis="violetred4",col="violetred4")
axis(4,tick=TRUE,at= label1,label=label1,col.axis="violetred4",col="violetred4")
polygon(data$TimeVariable1C,data$Variable1C,col='violetred4',border=NA)


legend("top", legend = c("Variable A","Variable B","Variable C"), col = c("black","violetred4","black"),
       ncol = 2, lwd =c("","",2),pch=c(19,15,NA),cex=1)

The output is as follows:

2) To make the font size same use the parameter cex and make it same everywhere.



来源:https://stackoverflow.com/questions/17128073/legend-in-multiple-plot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!