带有geom_text ggplot2的标签

时间:2013-08-12 13:43:47

标签: r ggplot2 label

嗨伙计们, 我有一个df和这个代码

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

我可以制作这张图: enter image description here

没关系,但有没有办法直接在线条末尾的图表上添加ID标签?我的意思是,用直接的图形标签替换图例。 我想应该用geom_text函数完成,但我的所有尝试都失败了。

2 个答案:

答案 0 :(得分:2)

使用标签的位置

创建一个新的data.frame

the plot

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

答案 1 :(得分:1)

您也可以使用annotate。这为您提供了更多的灵活性,但您必须指定注释的位置。

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