JRstuff
JRstuff

Reputation: 41

Preventing a comma from starting a new line in expression() in R

I am creating a quick plot and want to show the parameters for my lines in terms of betas in the legend.

Ideally, the legend would have a red line, and then say beta(0) = 2, beta(1) = 4, beta(2) = 6. Then, there would be a blue line with a similar list of betas, etc. Instead, when I use:

legend(2, 2, legend = expression(beta[0] == 2, beta[1] == 4, beta[2] == 6), lty = 1, col = "red")

I get a legend with 3 lines and each one has a corresponding beta next to it - like so: enter image description here

Is there a way to get those all on 1 line with commas between them so it doesn't look like they should be multiplied?

RESPONSE to Arun's answer:

Concatenating does not work. It puts everything on the same line but does not read "beta" as the Greek letter and does not have commas between them: enter image description here

SOLUTION:

After playing with Arun's answer, I figured it out:

legend(2, 2, legend = expression(paste(beta[0] == 2, ", ", beta[1] == 4, ", ", beta[2] == 6)), lty = 1, col = "red")

enter image description here

It won't let me mark it as solved but I will in 2 days when I am able.

Upvotes: 3

Views: 105

Answers (2)

JRstuff
JRstuff

Reputation: 41

Okay - I figured it out with help from Arun kumar mahesh's comment.

legend(2, 2, legend = expression(paste(beta[0] == 2, ", ", beta[1] == 4, ", ", beta[2] == 6)), lty = 1, col = "red")

does the trick

Upvotes: 1

Arun kumar mahesh
Arun kumar mahesh

Reputation: 2359

Concatenate the legend using paste function to get in a single line

legend(2, 2, legend = expression(paste("beta[0] == 2", "beta[1] == 4", "beta[2] = 6"),sep =","), lty = 1, col = "red")

Upvotes: 1

Related Questions