Tuesday, February 9, 2016

R: Plot multiple lines in one figure

Install library: ggplot2

data<-read.table("data.txt",header=TRUE,sep="\t")
df<-data.frame(data$C1,data$Benign,data$LiBenign,data$Unknown,data$ND,data$SuPath,data$Path,data$LiPath)


Method 1:

require(ggplot2)

g <- ggplot(df,aes(data$C1))
g<-g+geom_line(aes(y=data$Benign),colour="black")
g<-g+geom_line(aes(y=data$LiBenign),colour="black")
g<-g+geom_line(aes(y=data$Unknown),colour="black")
g<-g+geom_line(aes(y=data$ND),colour="black")
g<-g+geom_line(aes(y=data$SuPath),colour="black")
g<-g+geom_line(aes(y=data$Path),colour="red")
g<-g+geom_line(aes(y=data$LiPath),colour="magenta")

g


Method 2:

plot(data$C1,data$Benign,col="black",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="",ylab="")
par(new=TRUE)
plot(data$C2,data$LiBenign,col="black",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="",ylab="")
par(new=TRUE)
plot(data$C3,data$Unknown,col="black",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="",ylab="")
par(new=TRUE)
plot(data$C4,data$ND,col="black",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="",ylab="")
par(new=TRUE)
plot(data$C5,data$SuPath,col="green",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="",ylab="")
par(new=TRUE)
plot(data$C6,data$Path,col="red",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="",ylab="")
par(new=TRUE)
plot(data$C7,data$LiPath,col="magenta",xlim=c(0.0,7.0),ylim=c(0.0,40.0),xlab="geneX",ylab="-logP") 


No comments:

Post a Comment