1

I use two vectors in a function to obtain x values that I need to plot. I want to add the values of the vectors in the title. I've been using this code that works well but, as the length of the vectors change, I need to modify the code and add, lets say, p[2] or q[3].

p=3
q=c(1,2)
x= c(1:10)
plot(x,main=paste("Process X","\n Values p=",p," ; q=",q[1],",",q[2]))

I've realized that using just the name of the vector (as for p in the example) with a vector p of length=2, I get 2 titles superposed (one for each value of the vector).

I also used substitute() before paste function. In that case, I used just vector names without the brackets and get c(1,2) in the title which is ok for me but I don't know why substitute() don't allow nor "\n" to split lines nor the bold format for the title.

Is there a way to get just the values of the vectors, independently of the length, and generate just one simple title?

Thanks for your help

Thomas
  • 43,637
  • 12
  • 109
  • 140
Edu Marín
  • 89
  • 1
  • 5

1 Answers1

0

I thought of one, but it's not too smart.

Firstly, create a vector of strings, which are paste of the value and some separation, like "," or "|":

for(i in seq(length(q)){
    q[i] <- paste(q[i], ",")
}

If you don't want any separation in the end, simply don't paste last element with any separation.

Secondly, paste all the strings, with the following code from the answer Concatenate a vector of strings/character:

str_q <- paste(q, collapse = "")

Finally, paste the string with your title.

Edward
  • 554
  • 8
  • 15