2016-04-30 106 views
2

我有這個數據幀叫做mydf。我的下面的代碼繪製了Y軸效率圖的混合組合。我想要做的是將每個樣本組合(每條線)的X軸測量替換爲測量列表示的值。因此,對於efficiency1,我希望它可以用measurement1列中的值精確表示,而不是代碼中的一般1至7測量值,我希望對它們各自的測量列進行所有效率級別的測量。有人可以幫助我實現這個目標嗎?如何在R中正確繪製動態漸進繪圖

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"), 
     efficiency1 = 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), measurement1 = c(1, 1.2, 
     1, 1.3, 1.3, 1), measurement2 = c(2, 2.1, 2, 2.2, 2.3, 2), 
     measurement3 = c(3, 3.1, 3, 3.2, 3.3, 3), measurement4 = c(4, 
     4.1, 4, 4.2, 4.3, 4.1), measurement5 = c(5.1, 5.1, 4, 4.2, 
     4.3, 4.1), measurement6 = c(5.1, 6.1, 6, 6.2, 6.3, 6.1), 
     measurement7 = c(7.1, 7.1, 7, 7.2, 6.3, 7.1)), .Names = c("sample_A", 
    "sample_B", "efficiency1", "efficiency2", "efficiency3", "efficiency4", 
    "efficiency5", "efficiency6", "efficiency7", "measurement1", 
    "measurement2", "measurement3", "measurement4", "measurement5", 
    "measurement6", "measurement7"), 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); 
     }; 
    }); 
}; 
+0

製作'xticks'您的變量,你應該做的。 –

+0

@RomanLuštrik我怎麼把相應的列,我有點困惑。你能拿出來嗎? – MAPK

回答

1

這是我會怎麼做,你可以玩漂亮的標籤(請參見功能?pretty)。我改變的部分有他們周圍的空間。考慮到這不是C,所以;不是必需的。放置一些空格和命名參數可以使代碼更具可讀性。

effCis <- grep('^efficiency',names(mydf)); 
find.measurements <- grep("^measurement", 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; 
yticks <- seq(ylim[1L],ylim[2L],0.1); 

xticks <- seq(from = min(mydf[, find.measurements]), to = max(mydf[, find.measurements]), length.out = 7) 

plot(NA,xlim=c(min(xticks), max(xticks)), 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(side = 1, at = xticks) 

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); 
    }; 
    }); 
}; 

enter image description here

+0

謝謝,但測量(X)軸應該擴展到7.2,這是我們在'measurement7'欄中的最大值。 – MAPK

+0

我試着改變'effCis < - grep('^ efficiency',names(mydf));'到'effCis.forX < - grep('^ measurement',names(mydf));'但是沒有奏效。任何建議? – MAPK

+0

@MAPK看我的編輯。 –