0

I want to annotate each line with factor variable (chain.length) like shown on the picture. How would I do it?

ggplot

Here is the code for picture above.

  ggplot(mz_rt, aes(rt, mz, size = acex, colour = as.factor(double.bonds))) +
  geom_line(aes(group = as.factor(chain.len)), colour = "grey", size = 0.3) +
  geom_point() +
  scale_radius(breaks = seq(from = 0, to = 1, by = 0.2)) +
  theme(legend.position="bottom") +
  theme_light()
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Rim Gubaev
  • 1
  • 1
  • 1
  • 1
  • Like these? https://stackoverflow.com/a/49602796/ & https://stackoverflow.com/a/50807185/ – Tung Nov 05 '18 at 16:21

1 Answers1

3

Create an annotation layer by annotate function (https://ggplot2.tidyverse.org/reference/annotate.html). You need to specify positioning aesthetics.

library(tidyverse)
library(ggplot2)
data(iris)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    geom_point(size = 1.5 ,alpha = 0.7) +
    annotate("segment", x = 5, xend = 5.5, y = 2.2, yend = 2.4, colour = "blue") +
    annotate("text", x = 5, y = 2.15, label = "Some text")

enter image description here

TechWizard
  • 346
  • 1
  • 7
  • Is it possible in automatically? the line on my graph joins data points on their factor variable I only want to add this factor variable into the plot for each line. – Rim Gubaev Nov 05 '18 at 16:53
  • I think you're looking for ggrepel. Please see this example: https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html. – TechWizard Nov 05 '18 at 17:02
  • It looks almost as wanted, but still it annotates a certain points not the lines. – Rim Gubaev Nov 05 '18 at 17:18
  • @RimGubaev I automated, by simply filtering each profile to retain only one arbitrary point to be used for the annotation as anchor `myData %>% group_by(profile_id) %>% filter(max(measurement) == measurement) %>% select(timestamp, measurement, comment)` – Holger Brandl Apr 29 '22 at 09:17