简体   繁体   中英

Legend in multiple plot in R

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
...

R中的图形

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM