How to bold custom equations in ggplot2 stat_poly_eq function?

Viewed 259

I need to bold the equations in stat_poly_eq() in ggplot2. How do I do this along with atop function?

Also, is there anyway to specify the coordinates of the stat_poly_eq?

x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = my.formula, 
    aes(label = paste("atop(", ..eq.label.., ",", ..rr.label.., ")")),
    label.y = 0.9,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()

enter image description here

1 Answers

I don't know if you can; you can underline the equation, e.g.

x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = x ~ y, 
    aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
    label.y = 0.9,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()

example_1.png

However, everything I tried (e.g. bold(), bolditalic()) didn't work when combined with atop(). Perhaps it's related to this statement in the docs:

Note that bold, italic and bolditalic do not apply to symbols, and hence not to the Greek symbols such as mu which are displayed in the symbol font. They also do not apply to numeric constants.

Even if you "bold" everything with e.g. theme_classic(base_family = "Arial Bold"), the equation and R2 value aren't changed.

--

In terms of positioning the labels, you can move them around with label.x and label.y, e.g. (label.y = 1 puts the label at the top of the plot):

library(tidyverse)
library(ggpmisc)
x <- c(1:50)
y <- rnorm(50,4,1)
z <- rep(c("J","F","H","I","J","K","L","M","N","O"), each  = 5)
df <- data.frame(x,y,z)
ggplot(mapping = aes(x = x, y = y, color = z), data = df) +
  geom_point() + 
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="black") + 
  stat_summary(fun = mean, color = "black", geom ="point", size = 3,show.legend = FALSE) + 
  geom_smooth(method="lm", formula = y ~ x ) + 
  stat_poly_eq(
    formula = x ~ y, 
    aes(label = paste("atop(underline(", ..eq.label.., "),", ..rr.label.., ")")),
    label.y = 1,
    label.x = 0.2,
    parse = TRUE, 
    size = 2.5
    #, col = "black"
  )+
  facet_grid(.~z, scales = "free") + 
  theme_classic()

example_2.png

Related