Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.