1
ab <- 1:4
plot(ab, main = paste("value",ab))

The above produces

enter image description here

Whereas I want the label to show exactly like value 1:4. I know I can do this by tweaking the paste function something like paste0("value ",ab[1],":", ab[4]). But there must be a simpler way and also sometimes ab is just a single number, so I want something consistent that works for both cases. I also tried with expression and bquote, but can't figure out a way. Thanks.

1 Answer 1

2

You can use range(ab) and a second paste (with collapse=':') to get this result. For the case where ab is a single value, I had to use ifelse.

EDIT: a slightly shorter solution using unique instead of ifelse to treat for the case with a single value

ab <- 1:4
plot(ab, main = paste("value", paste(unique(range(ab)), collapse = ':')))

enter image description here

If ab = 1:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, this works. But, I am still wondering if there are any other ways to treat 1:n as a string and make the code sleeker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.