ggplot(logger) + geom_line(aes(Date.time, T, group=ID, colour=ID))+
ggtitle("Temperature in piezometers") +
xlab("Time") +
ylab("Temperature")
我可以制作这张图:
没关系,但有没有办法直接在线条末尾的图表上添加ID标签?我的意思是,用直接的图形标签替换图例。
我想应该用geom_text
函数完成,但我的所有尝试都失败了。
答案 0 :(得分:2)
使用标签的位置
创建一个新的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))
答案 1 :(得分:1)
您也可以使用annotate
。这为您提供了更多的灵活性,但您必须指定注释的位置。