Search code examples
rggplot2ggpmisc

Editing *row* names of tabular display when using stat_fit_tb() in ggpmisc & ggplot


While stat_poly_eq() allows variable names to be changed using eq.with.lhs and eq.x.rhs, a similar functionality does not seem to be available within stat_fit_tb(), according to my reading of the ggpmisc documentation.

Is there a way of modifying the plt object in the following example to force the table display to show parameter names that are easier on the eye and more consistent with the equation and axis labels?

## dummy data
set.seed(1)
df <- data.frame(month = c(1:60))
df$observed <- 2.5 + 0.05*df$month + rnorm(60, sd = 1)
## min plot example
my.formula <- y ~ poly(x,2,raw=TRUE) ## formula with generic variable names

plt <- ggplot(df, aes(x=month, y=observed)) +
  geom_point() +
  ## show fit and CI
  geom_smooth(method = "lm", se=TRUE, level=0.95, formula = my.formula) +
  ## display equation with useful variable names (i.e. not x and y)
  stat_poly_eq(eq.with.lhs = "italic(Obs)~`=`~",
               eq.x.rhs = ".month",
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
               parse = TRUE,
               formula = my.formula, label.y = 0.9) +
  ## show table of each coefficient's p-value
  stat_fit_tb(method.args = list(formula = my.formula),
              tb.vars = c(parameter = "term", ## can change column headings
                          coeff = "estimate", 
                          "p-val" = "p.value"),
              label.y = 0.8, label.x = "left")

plt

resulting plot


Solution

  • The updated 'ggpmisc' (>= 0.3.7) makes this answer possible, and in my view should be the preferred one.

    ## ggpmisc (>= 0.3.7)
    library(ggpmisc)
    
    ## dummy data
    set.seed(1)
    df <- data.frame(month = c(1:60))
    df$observed <- 2.5 + 0.05*df$month + rnorm(60, sd = 1)
    
    ## min plot example
    my.formula <- y ~ poly(x,2,raw=TRUE) ## formula with generic variable names
    
    plt <- ggplot(df, aes(x=month, y=observed)) +
      geom_point() +
      ## show fit and CI
      geom_smooth(method = "lm", se=TRUE, level=0.95, formula = my.formula) +
      ## display equation with useful variable names (i.e. not x and y)
      stat_poly_eq(eq.with.lhs = "italic(Obs)~`=`~",
                   eq.x.rhs = '" month"',
                   aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                   parse = TRUE,
                   formula = my.formula, label.y = 0.9) +
      ## show table of each coefficient's p-value
      stat_fit_tb(method.args = list(formula = my.formula),
                  tb.vars = c(parameter = "term", ## can change column headings
                              coeff = "estimate",
                              "p-val" = "p.value"),
                  tb.params = c(1, month = 2, "month^2" = 3), ##
                  label.y = 0.8, label.x = "left",
                  parse = TRUE)
    
    plt
    
    

    Giving the following plot. (I did also change the argument to eq.x.rhs although not directly part of the question. The nicer formatting of P-values is as implemented in the new version of the 'ggpmisc' package.)

    generated plot