I have a line graph that is flat and has sharp peaks (mass spectrometry data). I would like to plot my data and label the peaks with their x-axis value. I know it is possible to have a function to find local maxima and then to use these values in a label command but I am hoping there is a simpler way to do this. An example of what i would like to be able to do is in the Cooks Distance Plot on this webpage: personalityresearch.net/r/r.guide.html#linear ideally i could also specify a cutoff for values to be labelled. any suggestions are appreciated, best, zach cp -- View this message in context: n4.nabble.com/Line-Graph-Labels-tp1748218p1748218.html Sent from the R help mailing list archive at Nabble.com.
You can adapt the following example da <- data.frame(y=rnorm(50), x=1:50) plot(y~x, data=da) abline(h=c(-2,2), lty=3) with(da, text(x[abs(y)>2], y[abs(y)>2], label=x[abs(y)>2], pos=2)) Walmes. ----- ..ooo0 ................................................................................................... ..(....)... 0ooo... Walmes Zeviani ...\..(.....(.....)... Master in Statistics and Agricultural Experimentation ....\_)..... )../.... walmeszeviani at hotmail.com, Lavras - MG, Brasil ............ (_/............................................................................................ -- View this message in context: n4.nabble.com/Line-Graph-Labels-tp1748218p1748431.html Sent from the R help mailing list archive at Nabble.com.
Thanks Walmes, that does indeed work well for labeling all points greater than some specified value. But the problem is that while my peaks are very sharp there is more than one point along the line as it slopes up. This method will label those points as well , making the data look cluttered. Ideally there would be a command with an understanding of local maximum so that only the points at the top of the peak are labeled. thank you for suggestion, though; i definitely learned something new. best zach cp -- View this message in context: n4.nabble.com/Line-Graph-Labels-tp1748218p1748497.html Sent from the R help mailing list archive at Nabble.com.
You can set the number of extreme points to be labeled instead of define a cutoff. Look: da <- data.frame(y=rnorm(50), x=1:50) plot(y~x, data=da) abline(h=c(-2,2), lty=3) with(da, text(x[abs(y)>2], y[abs(y)>2], label=x[abs(y)>2], pos=2)) da <- da[order(da$y),] plot(y~x, data=da) # five small and big numbers num <- 5 with(da, points(x[c(1:num, nrow(da):(nrow(da)-num))], y[c(1:num, nrow(da):(nrow(da)-num))], pch=3, col=2)) with(da, text(x[c(1:num, nrow(da):(nrow(da)-num))], y[c(1:num, nrow(da):(nrow(da)-num))], label=x[c(1:5,nrow(da):(nrow(da)-5))], pos=rep(c(1,3), each=num))) Hope that helps. Walmes. ----- ..ooo0 ................................................................................................... ..(....)... 0ooo... Walmes Zeviani ...\..(.....(.....)... Master in Statistics and Agricultural Experimentation ....\_)..... )../.... walmeszeviani at hotmail.com, Lavras - MG, Brasil ............ (_/............................................................................................ -- View this message in context: n4.nabble.com/Line-Graph-Labels-tp1748218p1748558.html Sent from the R help mailing list archive at Nabble.com.
thanks again walmes. but the new problem would be that not all of the peaks are the same intensity. therefore the top five datapoints from my highest peak have greater intensity values than the highest point in the second-highest peak. but this is once again helpful. i found that there is a library msProcess (not in the base CRAN packages) made specially for mass spec data analysis. included in it is a peak-finder that looks for local maxima. the data has to be imported in a particular way and theres not too much documentation but thats what I am going to try. thanks again for your prompt reply, best, zach cp -- View this message in context: n4.nabble.com/Line-Graph-Labels-tp1748218p1748586.html Sent from the R help mailing list archive at Nabble.com.