R:如何在R中使用相同的坐标系绘制两个图形以进行直观比较?

时间:2016-07-05 11:26:16

标签: r

这是我的R计划:

 #The code of the first picture   
 a<-c(278,410,37.9,100,300,71,195,51.05,59.4,145,900,718,220,130,220,138,
      135,206,260,150,510,310,70,17,120,110,254.6,289.3,190)
        b<-log10(a)
        b.n<-length(b)
        b.location<-mean(b)
        b.var<-(b.n-1)/b.n*var(b)
        b.scale<-sqrt(3*b.var)/pi
        library(stats4)
        ll.logis<-function(location=b.location,scale=b.scale){-sum(dlogis(b,location,scale,log=TRUE))}
        fit.mle<-mle(ll.logis,method="Nelder-Mead")
        fit.location<-coef(fit.mle)[1]
        fit.scale<-coef(fit.mle)[2]
        plot(b, rank(b)/length(b),pch=16,xlab="Lg toxicity 
        data(μg/L)",pch=16,xlab="Lg toxicity data(μg/L)",ylab="Cumulative probability",lwd=3,font.lab=2,font.axis=2)
        f <- function(x) plogis(x, fit.location, fit.scale)
        plot(f, add=TRUE, xlim=extendrange(b,f=0.5))
 #The code of the second picture is   
 c<-c(1300,541,441,35,278,167,276,159,126,60.8,160,9740,3480,264.6,379,170,251.3,
      155.84,187.01,2800,66.5,420,840,40,1380,469,260,50,370)
        d<-log10(c)
        d.n<-length(d)
        d.location<-mean(d)
        d.var<-(d.n-1)/d.n*var(d)
        d.scale<-sqrt(3*d.var)/pi
        library(stats4)
        ll.logis<-function(location=d.location,scale=d.scale){-sum(dlogis(d,location,scale,log=TRUE))}
        fit.mle<-mle(ll.logis,method="Nelder-Mead")
        fit.location<-coef(fit.mle)[1]
        fit.scale<-coef(fit.mle)[2]
        plot(d, rank(d)/length(d),pch=25,col="blue",xlab="Lg toxicity data(μg/L)",ylab="Cumulative probability",lwd=3,font.lab=2,font.axis=2)
        k <- function(c) plogis(c, fit.location, fit.scale)
        plot(k, add=TRUE, xlim=extendrange(b,f=0.5))

在这两个代码部分之后,我可以得到两张图片(A和B)。 但是现在,我只是想使用相同的坐标系绘制两个图形以进行直观比较,如图C所示。

图片是这样的: enter image description here

我应该写什么代码?

1 个答案:

答案 0 :(得分:0)

这里有一个解决方案。清理了一下你的代码。您只能定义一次图形函数。

#The code of the first picture   
library(stats4)
f <- function(c) plogis(c, fit.location, fit.scale)

a<-c(278,410,37.9,100,300,71,195,51.05,59.4,145,900,718,220,130,220,138,
     135,206,260,150,510,310,70,17,120,110,254.6,289.3,190)
b<-log10(a)
b.n<-length(b)
b.location<-mean(b)
b.var<-(b.n-1)/b.n*var(b)
b.scale<-sqrt(3*b.var)/pi


#The code of the second picture is   
c<-c(1300,541,441,35,278,167,276,159,126,60.8,160,9740,3480,264.6,379,170,251.3,
     155.84,187.01,2800,66.5,420,840,40,1380,469,260,50,370)
d<-log10(c)
d.n<-length(d)
d.location<-mean(d)
d.var<-(d.n-1)/d.n*var(d)
d.scale<-sqrt(3*d.var)/pi


# Plotting first picture
plot(b, rank(b)/length(b), ylim=c(0,1), xlim=round((range(c(b, d)))))
# calculate parameters for first function
ll.logis<-function(location=b.location,scale=b.scale){-sum(dlogis(b,location,scale,log=TRUE))}
fit.mle<-mle(ll.logis,method="Nelder-Mead")
fit.location<-coef(fit.mle)[1]
fit.scale<-coef(fit.mle)[2]
plot(f, add=TRUE, xlim=extendrange(b,f= 0.5))
# Plotting second
par(new=TRUE)
plot(d, rank(d)/length(d), col=2, pch=2, xaxt="n", yaxt="n", xlab="", ylab="")
# calculate parameters for dataset 2
ll.logis<-function(location=d.location,scale=d.scale){-sum(dlogis(d,location,scale,log=TRUE))}
fit.mle<-mle(ll.logis,method="Nelder-Mead")
fit.location<-coef(fit.mle)[1]
fit.scale<-coef(fit.mle)[2]
plot(f, add=TRUE, xlim=extendrange(d,f=0.5), col=2)

enter image description here