0

I have some data:

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

I use the following function to get the confident interval for a given lambda:

       int.ech = function(vector,conf.level=0.95,na.rm=T){
if (length(vector)==0) { cat("Erreur ! Le vecteur ",substitute(vector),"est vide.\n")}
      else { s = var(vector,na.rm=na.rm)
      n = length(vector)-sum(is.na(vector))
      ddl = n - 1 ; proba = (1-conf.level)*100 ; proba = (100-proba/2)/100
      t_student = qt(proba, ddl) # quantile
      intervalle = t_student * sqrt(s/n)
      moyenne = mean(vector,na.rm=na.rm) ; return(intervalle) }
    }

Then, I plot the data with 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')

I have a text which I want to put on legend of this graph :

texte <- c(paste("window =",win,"km",sep= " "),paste("r_infection =", radius*1000,"m",sep=" "),paste("gamma =",gamma,"km/km^2",sep=" "))

The problem is when I want to execute the legend function :

legend("topleft", legend = texte)

I have this error :

Error in strwidth(legend, units = "user", cex = cex, font = text.font) : plot.new has not been called yet

For information, I get the same answer when I just execute :

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

Thank you for your help.

R.New
  • 1
  • 1
  • Hi. Can you edit your post and include the output of `dput(head(donnees, 10))` at the end of your post? – markus Jul 26 '18 at 10:19
  • @r-new, for the error message `plot.new has not been called yet`, do see this [post](https://stackoverflow.com/questions/7154133/plot-new-has-not-been-called-yet). Try, `plot.new()` after you see the error message and if you don't see a plot window, then invoke `quartz()`. If the problem is still unresolved then edit your question to provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – mnm Jul 26 '18 at 11:54
  • `legend` is for base R plots. You can try `ggplot2::annotate()`. In addition please revise your question to be reproducible your provided data and the specified names and function are corrupt – Roman Jul 26 '18 at 13:11
  • Thank you for your ansewrs. Jimbou, I corrected the corrupted sutffs. Thank you to have alerted me. It's not really the function annotate(). But it could work. However, I don't see any text on my graph if I add, for example, `annotate("text",x = 5, y = 200,label = texte)` – R.New Jul 26 '18 at 15:07

0 Answers0