Search code examples
rknitrggpmisctikzdevice

Using `round` or `sprintf` function for Regression equation in ggpmisc and `dev="tikz"`


How can I control the numeric display in Regression equation by using round or sprintf function? I also could not figure out how to use dev="tikz" when using eq.with.lhs = "hat(Y)~=~".

library(ggplot2)
library(ggpmisc)

# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x, 
                      y, 
                      group = c("A", "B"), 
                      y2 = y * c(0.5,2),
                      block = c("a", "a", "b", "b"))

str(my.data)

# plot
ggplot(data = my.data, mapping=aes(x = x, y = y2, colour = group)) +
        geom_point() +
        geom_smooth(method = "lm", se =  FALSE, formula = y ~ poly(x=x, degree = 2, raw = TRUE)) +
        stat_poly_eq(
                       mapping     = aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~"))
                     , data        = NULL
                     , geom        = "text"
                     , formula     = y ~ poly(x, 2, raw = TRUE)
                     , eq.with.lhs = "hat(Y)~`=`~"
                     , eq.x.rhs    = "X"
                     , label.x     = 0
                     , label.y     = 2e6
                     , vjust       = c(1.2, 0)
                     , position    = "identity"
                     , na.rm       = FALSE
                     , show.legend = FALSE
                     , inherit.aes = TRUE
                     , parse       = TRUE
                     ) +
        theme_bw()

enter image description here


Solution

  • 1) The code below answers the dev="tikz" part of the question if used with the 'ggpmisc' (version >= 0.2.9)

    \documentclass{article}
    
    \begin{document}
    
    <<setup, include=FALSE, cache=FALSE>>=
    library(knitr)
    opts_chunk$set(fig.path = 'figure/pos-', fig.align = 'center', fig.show = 'hold',
                   fig.width = 7, fig.height = 6, size = "footnotesize", dev="tikz")
    @
    
    
    <<>>=
    library(ggplot2)
    library(ggpmisc)
    @
    
    <<>>=
    # generate artificial data
    set.seed(4321)
    x <- 1:100
    y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
    my.data <- data.frame(x,
                          y,
                          group = c("A", "B"),
                          y2 = y * c(0.5,2),
                          block = c("a", "a", "b", "b"))
    
    str(my.data)
    @
    
    <<>>=
    # plot
    ggplot(data = my.data, mapping=aes(x = x, y = y2, colour = group)) +
      geom_point() +
      geom_smooth(method = "lm", se =  FALSE, 
                  formula = y ~ poly(x=x, degree = 2, raw = TRUE)) +
      stat_poly_eq(
        mapping     = aes(label = paste("$", ..eq.label.., "$\\ \\ \\ $",
                           ..rr.label.., "$", sep = ""))
        , geom        = "text"
        , formula     = y ~ poly(x, 2, raw = TRUE)
        , eq.with.lhs = "\\hat{Y} = "
        , output.type = "LaTeX"
       ) +
      theme_bw()
    @
    
    \end{document}
    

    enter image description here

    Thanks for suggesting this enhancement, I will surely also find a use for it myself!

    2) Answer to the roundand sprintf part of the question. You cannot use round or sprintf to change the number of digits, stat_poly_eq currently uses signif with three significant digits as argument applied to the whole vector of coefficients. If you want full control then you could use another statistics, stat_fit_glance, that is also in ggpmisc (>= 0.2.8), which uses broom:glance internally. It is much more flexible, but you will have to take care of all the formating by yourself within the call to aes. At the moment there is one catch, broom::glance does not seem to work correctly with poly, you will need to explicitly write the polynomial equation to pass as argument to formula.