当前位置: 动力学知识库 > 问答 > 编程问答 >

How to plot dynamically progressive plot properly in R

问题描述:

I have this dataframe called mydf. My code below plots the hybrid combination of plot for the efficiency in Y axis. What I want to do is replace the measurement in X axis for each sample combination (each line) to be represented by the measurement columns. So for efficiency1 I want it to be represented precisely by the values in measurement1 column instead of general 1 to 7 measurement I have in the code and I want to do this for all efficiency levels with their respective measurement columns. Can someone please help me achieve this goal.

 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")

Code I have:

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

};

});

};

网友答案:

Here's how I would do it, you can play around with pretty labels (see function ?pretty). The parts I changed have spaces around them. Consider that this is not C so ; are not necessary. Putting some spaces and naming arguments makes the code perhaps more readable.

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: