Skip to content
The Chat Archive
mistral-medium-2505· Mistral AI LMArena Human Preference 140K

How can I show values on my plot with plot_summs as well?

  1. User

    How can I show values on my plot with plot_summs as well?

  2. Assistant

    To show the values (coefficients) on your plot_summs plot, you can use the show.values argument and customize their appearance further with ggplot2 functions. Here’s how to do it effectively:

    Basic Approach

    The simplest way is to set show.values = TRUE in plot_summs(), which will display the coefficient values next to the points. You can also control the number of decimal places with the digits argument.

    jtools::plot_summs(
      model_reg_ratio,
      scale = TRUE,
      exp = TRUE,
      show.values = TRUE,  # Show coefficient values
      digits = 2           # Number of decimal places
    ) +
      labs(x = "Multiplicative Change in Trust Ratio (trustEU/trustUK)") +
      scale_x_continuous(breaks = seq(0.5, 2, 0.5), limits = c(0.4, 2.1))
    

    Customizing Value Appearance

    If you want more control over the text (e.g., font size, position, or formatting), you can manually add the values using geom_text() or geom_text_repel() (from ggrepel).

    Step-by-Step Customization

    1. Extract the plot data from plot_summs.
    2. Add the values manually using geom_text() or geom_text_repel() for better placement.
    library(jtools)
    library(ggplot2)
    library(ggrepel)  # For better text placement
    
    # Create the plot and store it
    p <- jtools::plot_summs(
      model_reg_ratio,
      scale = TRUE,
      exp = TRUE,
      show.values = FALSE  # We'll add them manually
    ) +
      labs(x = "Multiplicative Change in Trust Ratio (trustEU/trustUK)") +
      scale_x_continuous(breaks = seq(0.5, 2, 0.5), limits = c(0.4, 2.1))
    
    # Extract the data used in the plot
    plot_data <- p$data
    
    # Add the values manually with geom_text_repel (avoids overlap)
    p +
      geom_text_repel(
        data = plot_data,
        aes(
          x = est,  # Use the estimate for x-position
          y = term, # Use the term for y-position
          label = round(est, 2)  # Rounded coefficient value
        ),
        nudge_x = 0.1,  # Adjust horizontal position
        box.padding = 0.5,  # Adjust spacing
        segment.color = "grey50",  # Line color
        size = 3  # Text size
      )
    

    Alternative: Using geom_text() (Simpler but may overlap)

    If you don’t want to use ggrepel, you can use geom_text() directly, but text may overlap if points are close.

    p +
      geom_text(
        data = plot_data,
        aes(
          x = est,
          y = term,
          label = round(est, 2)
        ),
        hjust = -0.1,  # Adjust horizontal alignment
        size = 3
      )
    

    Formatting the Values

    If you want to format the values (e.g., as percentages or with specific decimal places), you can modify the label in aes():

    p +
      geom_text_repel(
        data = plot_data,
        aes(
          x = est,
          y = term,
          label = sprintf("%.2f", est)  # Format as 2 decimal places
        ),
        nudge_x = 0.1,
        size = 3
      )
    

    Final Example with Highlighting Significant Coefficients

    Combining this with your earlier question (highlighting significant coefficients):

    p <- jtools::plot_summs(
      model_reg_ratio,
      scale = TRUE,
      exp = TRUE,
      show.values = FALSE
    ) +
      labs(x = "Multiplicative Change in Trust Ratio (trustEU/trustUK)") +
      scale_x_continuous(breaks = seq(0.5, 2, 0.5), limits = c(0.4, 2.1))
    
    # Determine significance (confidence interval does not include 1)
    p$data <- p$data %>%
      mutate(
        significance = ifelse(conf.low > 1 | conf.high < 1, "Significant", "Not Significant"),
        est_label = round(est, 2)  # Rounded estimate for display
      )
    
    # Add values and color by significance
    p +
      geom_text_repel(
        aes(
          x = est,
          y = term,
          label = est_label,
          color = significance  # Color text by significance
        ),
        nudge_x = 0.1,
        size = 3
      ) +
      scale_color_manual(
        values = c("Significant" = "black", "Not Significant" = "grey50")
      )
    

    Key Takeaways

    • Use show.values = TRUE for a quick solution.
    • Use geom_text() or geom_text_repel() for more control.
    • Format values with sprintf() or round() for better readability.
    • Combine with significance highlighting for a polished plot.

    This should give you a clean and informative plot with values displayed clearly! 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.