Ask Your Question
2

How can I type lengthy text in a legend using automatic line spacing and include mathematical symbols in R?

asked 2023-06-13 03:14:09 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-13 03:40:02 +0000

david gravatar image

To type lengthy text in a legend using automatic line spacing and include mathematical symbols in R, you can use the "bquote" function in combination with "paste" and "collapse" functions. Here is an example code snippet:

# create a vector of legend text and mathematical symbols
legend.text <- c(paste("Group 1 (mean = ", round(mean(group1), 2), ")"),
                 paste("Group 2 (mean = ", round(mean(group2), 2), ")"))

# create a legend with automatic line spacing and mathematical symbols
legend("topright", legend = bquote(.(legend.text)), ncol = 1, 
       pch = c(16, 17), lty = c(1, 1), cex = 1)

In this code, the "bquote" function is used to convert the legend text and mathematical symbols into an expression that can be evaluated by R. The "paste" function is used to concatenate text and numerical values, and the "round" function is used to round the mean values to two decimal places. The "collapse" function is used to collapse the resulting vector of expression into a single line of legend text. The resulting legend is created using the "legend" function with appropriate arguments for positioning, symbols, and text parameters.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-13 03:14:09 +0000

Seen: 12 times

Last updated: Jun 13 '23