2016-07-05 23 views
1

這裏是我的[R計劃: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

我應該寫什麼碼?

+0

http://www.statmethods.net/graphs/line.html會幫助你。我認爲這是一個非常簡單的問題。谷歌搜索就夠了! – KrunalParmar

回答

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

+0

非常感謝您的慷慨解答。這正是我想要的! –

相關問題