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);
};
});
};
Related
Below is the example data which is list containing different data frames. I want to get one data frame out of it based on following two conditions.
First:
For each data frame in the list starting column 1 keep rbind()ing columns that have exact same column name as the previous one. The moment a different column name is encounter, drop that and all the columns till the last one.
For example: If column 1 is named Banana, then column 2 is named Banana, but column 3 is Orange and then again column 4 is Banana. Then column 1 and 2 will rbind() and column 3 and 4 will be dropped.
Another example: If column 1 is named Banana then column 2 is named Orange, but column 3 is named Banana, then only column 1 will survive as starting column 2 the column name is different and I don't care about column 3 name even though it's same as column 1.
Second:
After I run the list of data frame through above condition, then I want to combine all the data frames in the list to get one data frame which I think can be achieved using following code.
Here, lst2 is output of first condition.
do.call(rowr::cbind.fill, c(lst2, list(fill = 0)))
Above code credit #akrun. Any suggestions will be helpful.
Sample Data
list(A = structure(list(`A-DIODE` = c(1.2, 0.4), `A-DIODE` = c(1.3,
0.6)), row.names = c(NA, -2L), class = "data.frame"), B = structure(list(
`B-DIODE` = c(1.4, 0.8), `B-ACC1` = c(1.5, 1), `B-ACC2` = c(1.6,
1.2), `B-ANA0` = c(1.7, 1.4), `B-ANA1` = c(1.8, 1.6), `B-BRICKID` = c(1.9,
1.8), `B-CC0` = c(2L, 2L), `B-CC1` = c(2.1, 2.2), `B-DIGDN` = c(2.2,
2.4), `B-DIGDP` = c(2.3, 2.6), `B-DN1` = c(2.4, 2.8), `B-DN2` = c(2.5,
3), `B-DP1` = c(2.6, 3.2), `B-DP2` = c(2.7, 3.4), `B-SCL` = c(2.8,
3.6), `B-SDA` = c(2.9, 3.8), `B-USB0DN` = 3:4, `B-USB0DP` = c(3.1,
4.2), `B-USB1DN` = c(3.2, 4.4), `B-USB1DP` = c(3.3, 4.6),
`B-ACC1` = c(3.4, 4.8), `B-ACC2` = c(3.5, 5), `B-ANA0` = c(3.6,
5.2), `B-ANA1` = c(3.7, 5.4), `B-BRICKID` = c(3.8, 5.6),
`B-CC0` = c(3.9, 5.8), `B-CC1` = c(4L, 6L), `B-DIGDN` = c(4.1,
6.2), `B-DIGDP` = c(4.2, 6.4), `B-DN1` = c(4.3, 6.6), `B-DN2` = c(4.4,
6.8), `B-DP1` = c(4.5, 7), `B-DP2` = c(4.6, 7.2), `B-SCL` = c(4.7,
7.4), `B-SDA` = c(4.8, 7.6), `B-USB0DN` = c(4.9, 7.8), `B-USB0DP` = c(5L,
8L), `B-USB1DN` = c(5.1, 8.2), `B-USB1DP` = c(5.2, 8.4),
`B-NA` = c(5.3, 8.6), `B-ACC2PWRLKG_0v4` = c(5.4, 8.8), `B-ACC2PWRLKG_0v4` = c(5.5,
9), `B-P_IN_Leak` = c(5.6, 9.2)), row.names = c(NA, -2L), class = "data.frame"))
Update 1
After #ØysteinS answer I realized that there should be a third condition too:
Third:
If there is only a single column in one of the data frame in the list, then only that column be added to the parent data frame.
This should do the job:
data <- list(A = structure(list(`A-DIODE` = c(1.2, 0.4), `A-DIODE` = c(1.3,
0.6)), row.names = c(NA, -2L), class = "data.frame"), B = structure(list(
`B-DIODE` = c(1.4, 0.8), `B-ACC1` = c(1.5, 1), `B-ACC2` = c(1.6,
1.2), `B-ANA0` = c(1.7, 1.4), `B-ANA1` = c(1.8, 1.6), `B-BRICKID` = c(1.9,
1.8), `B-CC0` = c(2L, 2L), `B-CC1` = c(2.1, 2.2), `B-DIGDN` = c(2.2,
2.4), `B-DIGDP` = c(2.3, 2.6), `B-DN1` = c(2.4, 2.8), `B-DN2` = c(2.5,
3), `B-DP1` = c(2.6, 3.2), `B-DP2` = c(2.7, 3.4), `B-SCL` = c(2.8,
3.6), `B-SDA` = c(2.9, 3.8), `B-USB0DN` = 3:4, `B-USB0DP` = c(3.1,
4.2), `B-USB1DN` = c(3.2, 4.4), `B-USB1DP` = c(3.3, 4.6),
`B-ACC1` = c(3.4, 4.8), `B-ACC2` = c(3.5, 5), `B-ANA0` = c(3.6,
5.2), `B-ANA1` = c(3.7, 5.4), `B-BRICKID` = c(3.8, 5.6),
`B-CC0` = c(3.9, 5.8), `B-CC1` = c(4L, 6L), `B-DIGDN` = c(4.1,
6.2), `B-DIGDP` = c(4.2, 6.4), `B-DN1` = c(4.3, 6.6), `B-DN2` = c(4.4,
6.8), `B-DP1` = c(4.5, 7), `B-DP2` = c(4.6, 7.2), `B-SCL` = c(4.7,
7.4), `B-SDA` = c(4.8, 7.6), `B-USB0DN` = c(4.9, 7.8), `B-USB0DP` = c(5L,
8L), `B-USB1DN` = c(5.1, 8.2), `B-USB1DP` = c(5.2, 8.4),
`B-NA` = c(5.3, 8.6), `B-ACC2PWRLKG_0v4` = c(5.4, 8.8), `B-ACC2PWRLKG_0v4` = c(5.5,
9), `B-P_IN_Leak` = c(5.6, 9.2)), row.names = c(NA, -2L), class = "data.frame"))
# Use lapply to apply the same function to each data frame in the list.
combined_frames <- lapply(data, function(df){
first_name <- names(df)[[1]]
result <- df[, 1, drop = FALSE]
# Keep adding if name is the same as the first
if (ncol(df) != 1) {
for(i in seq(2, length(names(df)), by = 1)){
if(names(df)[[i]] == names(df)[[1]]){
result <- rbind(result, df[, i, drop = FALSE])
} else {
# Otherwise, break out of loop
break
}
}
}
return(result)
})
# Yes, your suggested code seems to work as expected for the last task
do.call(rowr::cbind.fill, c(combined_frames, list(fill = 0)))
#> A.DIODE B.DIODE
#> 1 1.2 1.4
#> 2 0.4 0.8
#> 3 1.3 0.0
#> 4 0.6 0.0
One easy option would be to loop through the list, get the run-length-id of the column names, extract only those equal to 1, unlist, convert to data.frame with the first column name and then with cbind.fill bind the list of data.frame`s together
library(data.table)
lst1 <- lapply(data, function(x)
setNames(data.frame(unlist(x[rleid(names(x)) == 1])), names(x)[1]))
do.call(rowr::cbind.fill, c(lst1, list(fill = 0)))
# A.DIODE B.DIODE
#1 1.2 1.4
#2 0.4 0.8
#3 1.3 0.0
#4 0.6 0.0
The data I have is a list of data frames. I want to loop through each of the data frame to find:
If there are columns with duplicate column names. If yes, then I
want to merge them by using rbind() in a parent data frame
called output and remove all other columns of such data frames.
I also want to check if there is any data frame that doesn't have duplicate
columns. If yes, then remove all the columns except the first one. Then
cbind() with output such that if rows are more or less than what was
created by (1) then zero should be added.
I tried using lappy(), but my logic to get above two isn't working at one go. Any suggestion will help.
output <- lapply(data, function(x) {
})
Input Data List Containing Data Frames
list(A = structure(list(`A-DIODE` = c(1.2, 0.4), `A-DIODE` = c(1.3,
0.6)), row.names = c(NA, -2L), class = "data.frame"), B = structure(list(
`B-DIODE` = c(1.4, 0.8), `B-ACC1` = c(1.5, 1), `B-ACC2` = c(1.6,
1.2), `B-ANA0` = c(1.7, 1.4), `B-ANA1` = c(1.8, 1.6), `B-BRICKID` = c(1.9,
1.8), `B-CC0` = c(2L, 2L), `B-CC1` = c(2.1, 2.2), `B-DIGDN` = c(2.2,
2.4), `B-DIGDP` = c(2.3, 2.6), `B-DN1` = c(2.4, 2.8), `B-DN2` = c(2.5,
3), `B-DP1` = c(2.6, 3.2), `B-DP2` = c(2.7, 3.4), `B-SCL` = c(2.8,
3.6), `B-SDA` = c(2.9, 3.8), `B-USB0DN` = 3:4, `B-USB0DP` = c(3.1,
4.2), `B-USB1DN` = c(3.2, 4.4), `B-USB1DP` = c(3.3, 4.6),
`B-ACC1` = c(3.4, 4.8), `B-ACC2` = c(3.5, 5), `B-ANA0` = c(3.6,
5.2), `B-ANA1` = c(3.7, 5.4), `B-BRICKID` = c(3.8, 5.6),
`B-CC0` = c(3.9, 5.8), `B-CC1` = c(4L, 6L), `B-DIGDN` = c(4.1,
6.2), `B-DIGDP` = c(4.2, 6.4), `B-DN1` = c(4.3, 6.6), `B-DN2` = c(4.4,
6.8), `B-DP1` = c(4.5, 7), `B-DP2` = c(4.6, 7.2), `B-SCL` = c(4.7,
7.4), `B-SDA` = c(4.8, 7.6), `B-USB0DN` = c(4.9, 7.8), `B-USB0DP` = c(5L,
8L), `B-USB1DN` = c(5.1, 8.2), `B-USB1DP` = c(5.2, 8.4),
`B-NA` = c(5.3, 8.6), `B-ACC2PWRLKG_0v4` = c(5.4, 8.8), `B-ACC2PWRLKG_0v4` = c(5.5,
9), `B-P_IN_Leak` = c(5.6, 9.2)), row.names = c(NA, -2L), class = "data.frame"))
Desired Output
> A
A-DIODE
1.2
0.4
1.3
0.6
> B
B-DIODE
1.4
0.8
> Output
A-DIODE B-DIODE
1.2 1.4
0.4 0.8
1.3 0
0.6 0
Loop through the list, create a condition with if/else that checks the length of the unique column names and returns the unlisted single data.frame when there is only a single unique column or else return the first column. Finally, with cbind.fill (from rowr) bind the list of data.frame columns together, specifying the fill as 0
lst2 <- lapply(lst1, function(x) if(length(unique(names(x))) ==1)
setNames(data.frame(unlist(x)), names(x)[1]) else x[1])
do.call(rowr::cbind.fill, c(lst2, list(fill = 0)))
# A.DIODE B.DIODE
#1 1.2 1.4
#2 0.4 0.8
#3 1.3 0.0
#4 0.6 0.0
I have a list of data frames and want to change part of column name of each of the sub data frame this list holds.
list(A = structure(list(`A-DIODE` = c(1.2, 0.4), `A-DIODE` = c(1.3,
0.6)), row.names = c(NA, -2L), class = "data.frame"), B = structure(list(
`B-DIODE` = c(1.4, 0.8), `B-ACC1` = c(1.5, 1), `B-ACC2` = c(1.6,
1.2), `B-ANA0` = c(1.7, 1.4), `B-ANA1` = c(1.8, 1.6), `B-BRICKID` = c(1.9,
1.8), `B-CC0` = c(2L, 2L), `B-CC1` = c(2.1, 2.2), `B-DIGDN` = c(2.2,
2.4), `B-DIGDP` = c(2.3, 2.6), `B-DN1` = c(2.4, 2.8), `B-DN2` = c(2.5,
3), `B-DP1` = c(2.6, 3.2), `B-DP2` = c(2.7, 3.4), `B-SCL` = c(2.8,
3.6), `B-SDA` = c(2.9, 3.8), `B-USB0DN` = 3:4, `B-USB0DP` = c(3.1,
4.2), `B-USB1DN` = c(3.2, 4.4), `B-USB1DP` = c(3.3, 4.6),
`B-ACC1` = c(3.4, 4.8), `B-ACC2` = c(3.5, 5), `B-ANA0` = c(3.6,
5.2), `B-ANA1` = c(3.7, 5.4), `B-BRICKID` = c(3.8, 5.6),
`B-CC0` = c(3.9, 5.8), `B-CC1` = c(4L, 6L), `B-DIGDN` = c(4.1,
6.2), `B-DIGDP` = c(4.2, 6.4), `B-DN1` = c(4.3, 6.6), `B-DN2` = c(4.4,
6.8), `B-DP1` = c(4.5, 7), `B-DP2` = c(4.6, 7.2), `B-SCL` = c(4.7,
7.4), `B-SDA` = c(4.8, 7.6), `B-USB0DN` = c(4.9, 7.8), `B-USB0DP` = c(5L,
8L), `B-USB1DN` = c(5.1, 8.2), `B-USB1DP` = c(5.2, 8.4),
`B-NA` = c(5.3, 8.6), `B-ACC2PWRLKG_0v4` = c(5.4, 8.8), `B-ACC2PWRLKG_0v4` = c(5.5,
9), `B-P_IN_Leak` = c(5.6, 9.2)), row.names = c(NA, -2L), class = "data.frame"))
I want to change A- in above data frame in a list to Z- and then B- in another data frame in the list to P-.
I tired below code but it doesn't seem to work. Any suggestions on how I can do this?
names(data$`A-DIODE`) <- "Z-DIODE"
names(data$`B-DIODE`) <- "P-DIODE"
....
....
You could use lapply() with a vectorized function from the stringi package to do the replacement.
library(stringi)
lapply(x0, function(x) {
out <- stri_replace_all_regex(names(x), c("^A-", "^B-"), c("Z-", "P-"), vectorize_all = FALSE)
names(x) <- out
x
})
You can use the lapply function. lapply will allow you to change the names in each of dataframe in the list.
In your above example, lets name your list as data. A and B are the dataframes nested in your list. To change the column names for particular names in the dataframes in the list
data=lapply(data,function(x){
names(x)[which(names(x)=="A-DIODE")]="Z-DIODE"
names(x)[which(names(x)=="B-DIODE")]="P-DIODE"
x
}
)
This question is an extension of this particular question. I have this particular data.table. I'm using data.table, mc2d, and e1071 libraries
library("data.table")
library("mc2d")
library("e1071")
col <- c("COST","TIME")
dt <- structure(
list(
ID = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),COST_PR_L = c(NA, 0.4, 0.31, 0.4, 0.5, 0.17, 1, 0.5, 0.5, 0.5),COST_PR_U = c(7.5, 2, 2.67, 1.67, 2.4,2, 1.5, 2, 2, 1.67),COST_PO_L = c(NA, 0.33, 0.25, 0.44,0.5, 0.25, 1, 0.5, 0.5, 0.5),COST_PO_U = c(3, 1.43, 3.33,1.8, 2.4, 3.6, 1.45, 2, 1.5, 1.67), TIME_PR_L = c(NA, 0.5,0.4, 0.5, 0.5, NA, 0.67, 0.5, 0.5, 0.5), TIME_PR_U = c(2,2.5, 3, 1.5, 2, NA, 1.5, 2, 1.67, 2), TIME_PO_L = c(NA,0.4, 0.25, 0.56, 0.5, NA, 0.6, 0.5, 0.5, 0.5), TIME_PO_U = c(2,2, 5, 1.67, 2.5, NA, 1.5, 2, 1.67, 2)
),.Names = c("ID","COST_PR_L", "COST_PR_U","COST_PO_L","COST_PO_U","TIME_PR_L","TIME_PR_U","TIME_PO_L","TIME_PO_U"),class = c("data.table","data.frame"),row.names = c(NA,-10L))
When I run this particular operation on it,
dt[, unlist(lapply(col, function(xx) {
y = colnames(dt)[grepl(pattern = xx, x = colnames(dt))]
vars1 = y[grepl(pattern = "PR", x = y)]
vars2 = y[grepl(pattern = "PO", x = y)]
mn = get(vars1[1])
mx = get(vars1[2])
sk1 = ifelse(mn !=0 && mx !=0,skewness(rpert(1000, min = mn , mode = 1, max= mx )),-1)
mn = get(vars2[1])
mx = get(vars2[2])
sk2 = ifelse(mn !=0 && mx !=0,skewness(rpert(1000, min = mn , mode = 1, max= mx )),-1)
return(list(sk1, sk2))
}), recursive = FALSE)
, by = "ID"]
I get the following error
Error in [.data.table(dt, , unlist(lapply(col, function(xx) { :
Column 1 of result for group 2 is type 'double' but expecting type
'logical'. Column types must be consistent for each group.
However, If I remove the unlist in the code, It seems to calculate the answer. What is unlist doing that is messing it up?
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, i.e. 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.