0

I am using ggplot2 to create a scatter plot of 2 variables. I want to have these printed out on the caption portion of ggplot:

  • linear regression equation
  • r2 value
  • p-value

I am using brackets, new lines and stored values to concatenate everything together. I have attempted using expression(), parse() and bquote() functions but it only prints out the variable name and not the stored values. This is the graph I have now. Everything looks great other than the R^2 part. Brackets seem to cause a lot of problems but I want to keep them (looks better in my opinion).This is my ggplot script. I am only concerned about the caption section at the end.

  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please do not post picture of code because then we have to retype everything to try it. Edit your question to include the code as text. – MrFlick Apr 07 '21 at 00:04
  • What does not look good about the R^2 part? – M.Viking Apr 07 '21 at 00:13

1 Answers1

1

Difficult to work with the code you have provided as an example (see comment re: reproducible example), but I had my students complete a similar exercise for their homework recently, and can provide an example which you can likely generalize from. My approach is to use the TeX() function from the latex2exp package.

A psychologist is interested in whether she can predict GPA in graduate school from students' earlier scores on the Graduate Record Exam (GRE).

Setup the Toy Data and Regression Model

GPA <- c(3.70,3.18,2.90,2.93,3.02,2.65,3.70,3.77,3.41,2.38,
         3.54,3.12,3.21,3.35,2.60,3.25,3.48,2.74,2.90,3.28)
GRE <- c(637,562,520,624,500,500,700,680,655,525,
         593,656,592,689,550,536,629,541,588,619)

gpa.gre <- data.frame(GPA, GRE)
mod <- lm(GPA ~ GRE, data = gpa.gre)
mod.sum <- summary(mod)
print(cofs <- round(mod$coefficients, digits = 4))
aY <- cofs[[1]]
bY <- cofs[[2]]
print(Rsqr <- round(cor(GPA,GRE)^2, digits = 2))

Generate the Plot

require(ggplot2)
require(latex2exp)

p <- ggplot(data = gpa.gre, aes(x = GRE, y = GPA)) +
  geom_smooth(formula = 'y ~ x', color ="grey40", method = "lm", 
              linetype = 1, lwd = 0.80, se = TRUE, alpha = 0.20) +
  geom_point(color = "grey10", size = 1) +
  labs(y = "Grade Point Average", x = "GRE Score") +
  coord_cartesian(ylim = c(2.28, 3.82), xlim = c(498, 702), clip = "off") +
  scale_y_continuous(breaks = seq(2.30, 3.80, 0.25)) +
  scale_x_continuous(breaks = seq(500, 700, 50)) +
  theme_classic() + 
  theme(axis.title.x = element_text(margin = unit(c(3.5,0,0,0), "mm"), size = 11.5),
        axis.title.y = element_text(margin = unit(c(0,3.5,0,0), "mm"), size = 11.5),
        axis.text = element_text(size = 10),
        plot.margin = unit(c(0.25,4,1,0.25), "cm"))

# Use TeX function to use LaTeX

str_note <- TeX("\\textit{Note. ***p} < .001")
str_eq <- TeX("$\\hat{\\textit{y}} = 0.4682 + 0.0045 \\textit{x}$")
str_rsq <- TeX("$\\textit{R}^2 = .54***$")

# Create annotations

p + annotate("text", x = 728, y = 3.70, label = str_eq, size = 3.5,
             hjust = 0, na.rm = TRUE) +
  annotate("text", x = 728, y = 3.57, label = str_rsq, size = 3.5,
           hjust = 0, na.rm = TRUE) +
  annotate("text", x = 490, y = 1.80, label = str_note, size = 3.5,
           hjust = 0, na.rm = TRUE) 

Get Result

ggsave(filename = '~/Documents/gregpa.png', # your favourite file path here 
       width = unit(5, "in"), # width of plot
       height = unit(4, "in"),  # height of plot
       dpi = 400) # resolution in dots per inch

enter image description here

Speleo4Life
  • 132
  • 8