I am currently trying to generate an automated script to create a labeled line graph. This line graphs will have 1 or more maxima that if they are above a certain threshold must be labeled. Currently I can label the maxima just fine.
For that I am using textxy() in the calibrate package
However when multiple peaks occur things get more complicated:
It quickly becomes difficult to see where the label is assigned to. So what I would like is a line from the label to the top (or just below) of the peak.
I've looked around all day today with no luck. I tried everything in Intelligent point label placement in R
wordcloud, but that unfortunately doesn't allow you to offset the labels, and fails if you have only one label.
identify, is much too slow. I need to be able to automate this to do thousands of images a day.
pointLabel, thigmophobe.labels both didn't work as they don't draw a line, and I am not dealing with lots of labels anyway.
I also tried manually drawing an arrow between the label and the point, but that got very time consuming.
Does anyone know of any package or easy way to do this? Is this not possible to automate?
Thanks! Cameron
You can try the labelPeaks
function provided by the MALDIquant
package. Its algorithm to place the peak labels is taken from Ian Fellows' wordcloud::wordlayout
.
library("MALDIquant")
data("fiedler2009subset")
## a simplified preprocessing to provide some example data
s <- trim(fiedler2009subset[[1]], c(3000, 3500))
r <- removeBaseline(s)
p <- detectPeaks(r)
## plot the peaks and label them
plot(p, ylim=c(0, 30000))
labelPeaks(p, avoidOverlap=TRUE, underline=FALSE, digits=1)
## rotate labels by 90 degree
plot(p, ylim=c(0, 30000))
labelPeaks(p, underline=FALSE, srt=90, adj=c(0, 0.5), col="red")
## label peaks above 5000
plot(p, ylim=c(0, 30000))
labelPeaks(p, index=intensity(p) > 5000)
Please see ?labelPeaks
for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With