Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw line from Label to point in R

Tags:

r

label

lines

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.

https://drive.google.com/file/d/0B50V8cCfhJDQWF94SG85NXVZY1k/view?usp=sharing

For that I am using textxy() in the calibrate package

However when multiple peaks occur things get more complicated:

https://drive.google.com/file/d/0B50V8cCfhJDQUmF5VERhUV8zNmc/view?usp=sharing

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

like image 367
Cameron Avatar asked Oct 19 '22 11:10

Cameron


1 Answers

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)

enter image description here

## rotate labels by 90 degree
plot(p, ylim=c(0, 30000))
labelPeaks(p, underline=FALSE, srt=90, adj=c(0, 0.5), col="red")

enter image description here

## label peaks above 5000
plot(p, ylim=c(0, 30000))
labelPeaks(p, index=intensity(p) > 5000)

enter image description here

Please see ?labelPeaks for more details.

like image 158
sgibb Avatar answered Oct 30 '22 20:10

sgibb