如何在ggplot处注释行?

时间:2018-11-05 16:06:40

标签: r ggplot2

我想用因子变量(chain.length)注释每行,如图所示。我该怎么办?

ggplot

这是上面图片的代码。

  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()

1 个答案:

答案 0 :(得分:0)

通过注释功能(https://ggplot2.tidyverse.org/reference/annotate.html)创建注释层。您需要指定定位美学。

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