rplotexpressionmathematical-expressions

How to Display LaTeX-style Expressions in R Plot Titles Using expression()


I am working on a simulation study in R where I am plotting bias and variance curves for different estimators. I want the titles of the subplots to include properly formatted mathematical expressions, specifically \beta_0 and \beta_1, using expression() in the main argument of the plot() function.

Here is the relevant portion of my code for plotting:

# Define plotting parameters
pch_values <- c(3, 4, 18, 20)  # Symbols for different cases
colors <- c("red", "blue", "gray60")  # Colors for different noises
plot_titles <- c("Bias Curves", "Variance Curves")
coeff_labels <- c(expression(beta[0]), expression(beta[1]))


par(mfrow = c(2, 2), mar = c(4, 4, 2, 1))  # 2x2 layout

for (plot_type in c("bias", "variance")) {
  for (coef_idx in seq_along(coeff_labels)) {
    coef_name <- ifelse(coef_idx == 1, "beta_0", "beta_1")
    
    # Compute y-axis range
    ylim_range <- range(sapply(results_coef, function(noise_list) {
      sapply(noise_list, function(case_list) {
        y_values <- abs(case_list[[plot_type]][[coef_name]])  # Absolute for bias
        return(y_values)
      })
    }), na.rm = TRUE)
    
    # Initialize the plot
    plot(NULL, log = "x", xlim = range(sample_sizes), ylim = ylim_range,
         xlab = "Sample Size (T)", ylab = str_to_title(plot_type),
         main = paste(plot_titles[ifelse(plot_type == "bias", 1, 2)], " (", coeff_labels[coef_idx], ") ", sep = ""))
    
    
    for (noise_idx in seq_along(noise_types)) {
      noise <- noise_types[noise_idx]
      case_counter <- 1
      
      for (scale in unique(unlist(lapply(cases, `[[`, noise)))) {
        y_values <- abs(results_coef[[noise]][[as.character(scale)]][[plot_type]][[coef_name]])  # Absolute for bias
        
        lines(sample_sizes, y_values, col = colors[noise_idx], lty = noise_idx, lwd = 1)
        points(sample_sizes, y_values, col = colors[noise_idx], pch = pch_values[case_counter])
        case_counter <- case_counter + 1
      }
    }
    
    # Add legends
    legend("topright", legend = str_to_title(noise_types), col = colors,
           lty = 1:length(noise_types), lwd = 1, bty = "n", inset = c(.2, 0))
    legend("topright", legend = paste("Case", seq_along(pch_values)),
           pch = pch_values, col = "black", bty = "n")
  }
}

However, instead of displaying the mathematical expressions \beta_0 and \beta_1 in the plot titles, the main text shows beta[0] and beta[1] as plain text.

Question:

How can I correctly format the plot titles to include both text (e.g., "Bias Curves") and LaTeX-style mathematical expressions like \beta_0​ and \beta_1​ in the main argument of plot()?

Any suggestions or alternative approaches would be greatly appreciated.

Edit:

Here is a minimal working example:

# Define plotting parameters
coeff_labels <- c(expression(beta[0]), expression(beta[1]))
plot_titles <- c("Bias Curves", "Variance Curves")

# Attempting to plot with combined text and expressions
plot(NULL, xlim = c(1, 10), ylim = c(1, 10), 
     main = paste(plot_titles[1], " (", coeff_labels[1], ")", sep = ""))

Although I am not sure it properly conveys the problem.


Solution

  • Your coeff_labels variables are expressions which would be formatted properly on their own, but you are combining them with strings using c(), so everything is converted to strings and it doesn't work.

    If you were willing to hard code the labels, it would be easy. Use

    # Attempting to plot with combined text and expressions
    plot(NULL, xlim = c(1, 10), ylim = c(1, 10), 
     main = expression(paste("Bias Curves (", beta[0], ")")))
    

    So the thing you need to do is to construct an expression like that out of variables. There are lots of ways to do it; substitute might be easiest. For example,

    plot(NULL, xlim = c(1, 10), ylim = c(1, 10), 
     main = substitute(paste(title, " ", (label)), 
              list(title = plot_titles[1], 
                   label = coeff_labels[[1]])))