J.Zhang J.Zhang - 2 months ago 10
R Question

R:How can I fill color in the gray curve?

I am trying to color grey between the grey lines.Here is my code:

x<-c(1300,541,441,35,278,167,276,159,126,60.8,160,5000,718,9740,135,3480,264.6,379,170,251.3,155.84,187.01,850)
y<-log10(x)
y.n<-length(y)
y.location<-mean(y)
y.var<-(y.n-1)/y.n*var(y)
y.scale<-sqrt(3*y.var)/pi
library(stats4)
ll.logis<-function(location=y.location,scale=y.scale){-sum(dlogis(y,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(y, rank(y)/length(y),pch=16,xlim=c(0,4.5),ylim=c(0,1),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(y,f=0.5))

b1=c(0.0001,0.0020,0.0050,0.0080,0.0100,0.0200,0.0300,0.0500,0.0700,0.0850
,0.1000 ,0.1300, 0.1600 ,0.1700, 0.2000, 0.2500, 0.3500 ,0.4500 ,0.5000 ,0.5500,0.6000 ,0.6500, 0.7000 ,0.7500 ,0.8000 ,0.8300 ,0.8500,0.8600,0.8800 ,0.8900,0.9000 ,0.9100 ,0.9200 ,0.9500 ,0.9600 ,0.9800 ,0.9900 ,0.9910 ,0.9920 ,0.9950,0.9990 ,0.9991 ,0.9995)
b2=c(-1.4228338, -0.1708867 , 0.2137525 , 0.4090538 ,0.5057927 ,0.7923687
,0.9622791 ,1.1791750 ,1.3219360 ,1.4036990 ,1.4736460 , 1.5976540
, 1.6889170, 1.7171390 , 1.7847650 ,1.8896900 ,2.0615320 , 2.1993580
, 2.2625660 ,2.3254820 , 2.3846840 ,2.4468840, 2.5120280 ,2.5789810
,2.6562430 ,2.7015100 ,2.7439390 ,2.7640660 , 2.8073580, 2.8315670
, 2.8567300 , 2.8866860 , 2.9179340 , 3.0325500 ,3.0837900 , 3.2433900
,3.3984480 ,3.4207850, 3.4461270 , 3.5462790 ,3.8801320 , 3.9029680
,4.0260600)
b3=c(0.6077848, 1.2408373, 1.4356009,1.5396786 ,1.5877812, 1.7440615 ,1.8340440
, 1.9512210 ,2.0327160 ,2.0821100 ,2.1223410, 2.1957080, 2.2551370, 2.2739320
, 2.3215060, 2.3980790, 2.5267570 ,2.6448090 ,2.7075680 ,2.7700660, 2.8368790
,2.9095640 ,2.9900380 ,3.0786590 ,3.1825480 ,3.2642270 ,3.3131620 ,3.3455740
, 3.4121960 ,3.4459760 ,3.4867300 ,3.5315330, 3.5822670 ,3.7827450 ,3.8943750
, 4.1626610 ,4.4506750, 4.4947990 ,4.5424880, 4.7422540, 5.4155060, 5.4598650
, 5.7045520)
par(new=TRUE)
lines(b2,b1,lty=1,col='gray')
lines(b3,b1,lty=1,col='gray')


Iam so sorry i don not know how to upload data file, so the data is shown in the code,

enter image description here
After this i can get a curve like this, but how can i color grey between the grey lines?

Answer

You can use (tested already)

polygon(c(b2,rev(b3)), c(b1,rev(b1)), col = "gray")

polygon will line up points in order to get a closed polygon, that is why we reverse the order for b3 and b1. When col is set, the polygon is filled with colour.

Comments