简体   繁体   English

带有geom_text ggplot2的标签

[英]labels with geom_text ggplot2

Hy guys, I have a df and with this code Hy伙计们,我有一个df和这个代码

ggplot(logger) + geom_line(aes(Date.time, T, group=ID, colour=ID))+
  ggtitle("Temperature in piezometers") +
  xlab("Time") +
  ylab("Temperature")

I can made this graph: 我可以制作这张图: 在此输入图像描述

That's fine, but is there a way to add ID labels directly on the graph just next to the end of the lines? 这没关系,但有没有办法直接在线条末尾的图表上添加ID标签? I mean, replace the legend with a direct on graph labeling. 我的意思是,用直接的图形标签替换图例。 I guess it should be done with geom_text function, but all my attempts failed. 我想应该用geom_text函数完成,但我的所有尝试都失败了。

Create a new data.frame with the positions of the labels 使用标签的位置创建新的data.frame

剧情

library(ggplot2)
set.seed(12345)
dataset <- do.call(
  rbind, 
  lapply(seq_len(5), function(i){
    data.frame(
      ID = i,
      Time = 1:100,
      Temp = as.vector(arima.sim(model = list(ar = .8), n = 100))
    )
  }) 
)
dataset$ID <- factor(dataset$ID)
Right <- subset(dataset, Time == max(Time))
ggplot(dataset, aes(x = Time, y = Temp, colour = ID)) + geom_line() + geom_text(data = Right, aes(label = ID))

You can also use annotate . 您也可以使用annotate This gives you a bit more flexibility, though you would have to specify where you want the annotation to be. 这为您提供了更多的灵活性,但您必须指定注释的位置。

See documentation: http://docs.ggplot2.org/current/annotate.html 请参阅文档: http//docs.ggplot2.org/current/annotate.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM