简体   繁体   中英

How to plot asymptote of a curve in R?

I have this data called mydf where I have hybrid sample comparison for efficiency. There are seven different efficiency columns for the intermixing of sampleA and sampleB. I want to see the plot for these seven efficiencies to see at which efficiency level will they significantly drop compared to the first few columns.

mydf<-structure(list(sample_A = structure(c(1L, 2L, 2L, 2L, 3L, 4L), .Label = c("2568", 
"2669", "2670", "2671", "2946", "LPH-001-10_AK1", "LPH-001-12_AK2", 
"LPH-001-9"), class = "factor"), sample_B = structure(c(1L, 2L, 
3L, 4L, 3L, 4L), .Label = c("2568", "2669", "2670", "2671", "2946", 
"LPH-001-10_AK1", "LPH-001-12_AK2", "LPH-001-9"), class = "factor"), 
    efficiency = c(1.02, 0.964, 0.415, 0.422, 0.98, 0.986), efficiency2 = c(1, 
    0.944, 0.395, 0.402, 0.96, 0.966), efficiency3 = c(0.9, 0.844, 
    0.295, 0.302, 0.86, 0.866), efficiency4 = c(0.32, 0.264, 
    -0.285, -0.278, 0.28, 0.286), efficiency5 = c(0.02, -0.0360000000000001, 
    -0.585, -0.578, -0.0200000000000001, -0.0140000000000001), 
    efficiency6 = c(0.12, 0.0639999999999999, -0.485, -0.478, 
    0.08, 0.086), efficiency7 = c(0.02, -0.036, -0.585, -0.578, 
    -0.02, -0.014)), .Names = c("sample_A", "sample_B", "efficiency", 
"efficiency2", "efficiency3", "efficiency4", "efficiency5", "efficiency6", 
"efficiency7"), row.names = c(NA, 6L), class = "data.frame")

Here's one way to plot your data:

mydf <- structure(list(sample_A=structure(c(1L,2L,2L,2L,3L,4L),.Label=c('2568','2669','2670','2671','2946','LPH-001-10_AK1','LPH-001-12_AK2','LPH-001-9'),class='factor'),sample_B=structure(c(1L,2L,3L,4L,3L,4L),.Label=c('2568','2669','2670','2671','2946','LPH-001-10_AK1','LPH-001-12_AK2','LPH-001-9'),class='factor'),efficiency=c(1.02,0.964,0.415,0.422,0.98,0.986),efficiency2=c(1,0.944,0.395,0.402,0.96,0.966),efficiency3=c(0.9,0.844,0.295,0.302,0.86,0.866),efficiency4=c(0.32,0.264,-0.285,-0.278,0.28,0.286),efficiency5=c(0.02,-0.0360000000000001,-0.585,-0.578,-0.0200000000000001,-0.0140000000000001),efficiency6=c(0.12,0.0639999999999999,-0.485,-0.478,0.08,0.086),efficiency7=c(0.02,-0.036,-0.585,-0.578,-0.02,-0.014)),.Names=c('sample_A','sample_B','efficiency','efficiency2','efficiency3','efficiency4','efficiency5','efficiency6','efficiency7'),row.names=c(NA,6L),class='data.frame');
effCis <- grep('^efficiency',names(mydf));
xlim <- c(1,length(effCis));
ylim <- range(mydf[,effCis],na.rm=T);
ylim[1L] <- floor(ylim[1L]/0.1)*0.1;
ylim[2L] <- ceiling(ylim[2L]/0.1)*0.1;
xticks <- seq_along(effCis);
yticks <- seq(ylim[1L],ylim[2L],0.1);
plot(NA,xlim=xlim,ylim=ylim,xlab='measurement',ylab='efficiency',xaxs='i',yaxs='i',axes=F);
abline(v=xticks,col='lightgrey');
abline(h=yticks,col='lightgrey');
abline(h=0,lwd=2);
axis(1L,xticks,xticks,font=2L,cex.axis=0.7);
axis(2L,yticks,sprintf('%.1f',yticks),las=1L,font=2L,cex.axis=0.7);
hybrid.col <- data.frame(hybrid=seq_len(nrow(mydf)),col=c('red','green','blue','gold','cyan','magenta'),stringsAsFactors=F);
splineN <- 200L;
for (ri in seq_len(nrow(hybrid.col))) {
    hybrid <- hybrid.col$hybrid[ri];
    col <- hybrid.col$col[ri];
    x <- xticks;
    y <- c(as.matrix(mydf[hybrid,effCis]));
    points(x,y,pch=16L,col=col,xpd=NA);
    with(spline(x,y,splineN),{
        lines(x,y,col=col,lwd=2,xpd=NA);
        localwin <- which(x>2 & x<3);
        tp <- which.min(abs(diff(y[localwin])));
        if (length(tp)>0L) points(x[localwin[tp]],y[localwin[tp]],col=col,pch=4L);
        localwin <- which(x>2 & x<5);
        tp <- which.min(diff(y[localwin]));
        if (length(tp)>0L) {
            m <- diff(y[localwin[seq(tp,len=2L)]])/diff(x[localwin[seq(tp,len=2L)]]);
            if (is.finite(m)) abline(y[localwin[tp]]-m*x[localwin[tp]],m,col=col,lty=2L);
        };
    });
};
legend(5.5,0.95,paste0(mydf$sample_A,' / ',mydf$sample_B),fill=hybrid.col$col,cex=0.7,title='hybrid');

情节


I wasn't 100% sure what you meant by the asymptote. I initially thought maybe you wanted the local maxima of the curves just prior to where they begin to drop, which is why I marked the local maxima with points (symbol X, ie pch=4L ). But then I realized maybe you meant the tangent line along the drop, so I added lines tangent to the points of steepest slope.

This is the definition of asymptote:

a straight line approached by a given curve as one of the variables in the equation of the curve approaches infinity.

I don't think that's applicable here; plotting this data does not involve taking anything to infinity. I think you want either the local maxima or tangent lines.

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