R 如何在ggplot数据图上放置文本图例?

R 如何在ggplot数据图上放置文本图例?,r,ggplot2,R,Ggplot2,我有一些数据: donnees <- structure(list(mean.time = c(66.2181493259296, 38.4214009587611,28.3780846407761, 23.6036479741174, 21.1194982724492, 18.2304201307749, 17.255935716945, 15.1999929855212, 14.4373235262462), interval.time = c(2.02676652919753, 1.

我有一些数据:

donnees <- structure(list(mean.time = c(66.2181493259296, 38.4214009587611,28.3780846407761, 23.6036479741174, 21.1194982724492, 18.2304201307749, 17.255935716945, 15.1999929855212, 14.4373235262462), interval.time = c(2.02676652919753, 1.11352244913202, 0.852970451889973, 0.659463003712615, 0.637078718678222, 0.555560539640353, 0.523901049738113, 0.459577876757762, 0.42846867586966), mean.speed = c(93.3820017657387, 161.279607915939, 218.506927701215,257.296969741989, 295.210612887155, 344.350019217133, 364.011355824999, 409.234700329235, 437.398681209406), interval.speed = c(2.88657084170069, 5.02519369247631, 6.79320499564379, 7.66765106354858, 9.45539113245263, 10.9383151684182, 12.2017419883015, 13.715012760365, 16.6173823082674), mean.dist = c(5059.20542230044, 5025.32849954932, 4979.53881596498, 4994.56758567708, 4980.47651740827, 4991.06590346422, 4984.66564161804, 4932.67139724921, 4991.32367321949), interval.distance = c(44.8556012621449, 41.8511306706474, 42.6986600944198, 43.1898193770464, 42.7477524465553, 43.4197913676737, 42.9860392430236, 41.6610917291015, 42.4239685871405), lambda = 1:9), .Names = c("mean.time", "interval.time", "mean.speed", "interval.speed", "mean.dist", "interval.distance", "lambda"), row.names = c(NA, 9L), class = "data.frame")
然后,我用ggplot绘制数据:

      ggplot(donnees, aes(x = lambda, y = donnees$mean.speed))+
    geom_point() + geom_path(color = "blue") +  
geom_errorbar(aes(ymin = donnees$mean.speed - donnees$interval.speed, ymax = donnees$mean.speed + donnees$interval.speed)) + 
labs(title = 'Distibution of the infection speed (m/min) once the malware have reached the edge in function of lambda (users/km)', x = "lambda : users density (users/km)", y = "Infection speed (m/min)") + 
labs(linetype='custom title')
我有一个文本,我想放在这个图表的图例上:

texte <- c(paste("window =",win,"km",sep= " "),paste("r_infection =", radius*1000,"m",sep=" "),paste("gamma =",gamma,"km/km^2",sep=" "))
我有一个错误:

strwidth(legend,units=“user”,cex=cex,font=text.font)中出错:尚未调用plot.new

作为参考,我在执行时得到了相同的答案:

ggplot(donnees, aes(x = lambda, y = meanspeed)) + legend("topleft", legend = texte)

谢谢你的帮助。

嗨。您是否可以编辑您的帖子并在帖子末尾包含
dput(head(donnees,10))
的输出?@r-new,对于错误消息
plot.new尚未调用,请务必查看此消息。看到错误消息后,如果没有看到打印窗口,请尝试,
plot.new()
,然后调用
quartz()
。如果问题仍未解决,请编辑您的问题以提供一个。
图例
用于基本R图。您可以尝试
ggplot2::annotate()
。此外,请修改您的问题,使其可复制您提供的数据,指定的名称和功能已损坏。谢谢您的ansewrs。Jimbou,我纠正了损坏的Sutfs。谢谢你提醒我。它实际上不是函数annotate()。但它可以工作。但是,如果我添加,例如,
annotate(“text”,x=5,y=200,label=texte),我在图表上看不到任何文本。
legend("topleft", legend = texte)
ggplot(donnees, aes(x = lambda, y = meanspeed)) + legend("topleft", legend = texte)