How can I show values on my plot with plot_summs as well?
- User
How can I show values on my plot with plot_summs as well?
- Assistant
To show the values (coefficients) on your
plot_summsplot, you can use theshow.valuesargument and customize their appearance further withggplot2functions. Here’s how to do it effectively:Basic Approach
The simplest way is to set
show.values = TRUEinplot_summs(), which will display the coefficient values next to the points. You can also control the number of decimal places with thedigitsargument.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()orgeom_text_repel()(fromggrepel).Step-by-Step Customization
- Extract the plot data from
plot_summs. - Add the values manually using
geom_text()orgeom_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 usegeom_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
labelinaes():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 = TRUEfor a quick solution. - Use
geom_text()orgeom_text_repel()for more control. - Format values with
sprintf()orround()for better readability. - Combine with significance highlighting for a polished plot.
This should give you a clean and informative plot with values displayed clearly! 🚀
- Extract the plot data from
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.