1

I'm having some trouble having stat_poly_eq print the correct formula within my plot. My current plot is plotted as follows.

  ggplot(DataSB,aes(x=Year,y=Kilos))+
  geom_point(shape=21, size=3, color="black", fill="#E29578") +
  geom_smooth(method = "glm", method.args=list(family=Gamma(link = "log")),
              se=T, size=2, color="#E29578") +
  labs(x= "", y = "Catch per average trip (Kg)", title = "(a) Small benthics")+   
  stat_poly_eq(formula = y ~ x,
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
               parse = TRUE, color = "black")

As you can see I use a gamma(log) distribution to model my data. I also use the same analysis in the glm function which provides me with two coefficients, output of glm:

glm(formula = Kilos ~ Year, family = Gamma(link = "log"), data = DataSB)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-4.4636  -0.9370  -0.3679   0.3633   2.1094  

Coefficients:
             Estimate Std. Error t value Pr(>|t|)   
(Intercept) 31.081487   9.571677   3.247  0.00149 **
Year        -0.012418   0.004771  -2.603  0.01035 * 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for Gamma family taken to be 0.903844)

    Null deviance: 199.67  on 128  degrees of freedom
Residual deviance: 193.40  on 127  degrees of freedom
AIC: 1853.4

Number of Fisher Scoring iterations: 6

Now in order to use these coefficients I realise I need to put them in the following formula:

exp(b+a*x) #So:
exp(31.081487-0.012418*x)

The problem I'm struggling with is how to make stat_poly_eq print this formula. I've tried pretty much every combination for the formula, but nothing is quite right, is it even possible for stat_poly_eq to print this? If not is there a function that can, including the R-squared? Data:

Num   Reference Order         Group Individuals   Kilos Size TotalCatch Year
1         A001    01 Small Benthic      500.00  125.00   15   670.0000 2021
11        A002    01 Small Benthic      500.00  300.00   25  1381.0000 2021
21        A003    01 Small Benthic      500.00   60.00   16  5742.0000 2021
31        A004    01 Small Benthic      800.00  230.00   18  1535.0000 2021
41        A005    01 Small Benthic      225.00  111.00   18  3192.0000 2021
61        A008    01 Small Benthic      500.00 1200.00   18  4983.5000 2008
71        A009    01 Small Benthic       20.00   60.00   21   121.0000 2021
81        A010    01 Small Benthic     3600.00  600.00   17  4008.0000 2021
91        A011    01 Small Benthic      450.00  135.00   23   272.0000 2021
101       A012    01 Small Benthic     2100.00  350.00   26  2080.0000 2021
111       A013    01 Small Benthic        5.00    3.00   27   140.0000 2020
121       A014    01 Small Benthic     5000.00 2000.00   18  2135.0500 2017
131       A015    01 Small Benthic     1500.00  700.00   32  1483.0000 2021
141       A016    01 Small Benthic     1600.00  600.00   27  1993.0000 2021
151       A017    01 Small Benthic      120.00   80.00   26   626.0000 2021
161       A018    01 Small Benthic        0.00    0.01 <NA>    31.0000 2021
181       A020    01 Small Benthic      100.00  120.00   20  5210.0000 2021
191       A021    01 Small Benthic     2400.00 1800.00   35  7441.0000 2021
201       A022    01 Small Benthic     4000.00  800.00   35  7085.0000 2021
211       A023    01 Small Benthic        0.00    0.01 <NA>  2268.0000 2021
221       A024    01 Small Benthic      300.00  121.00   25  4152.0000 2021
231       A025    01 Small Benthic     1000.00  600.00   33  3697.0000 2021
241       A026    01 Small Benthic      300.00  131.00   28  2522.0000 2021
251       A027    01 Small Benthic      100.00   60.00   25   315.0000 2021
271       A029    01 Small Benthic     1000.00  520.00   27  2511.0000 2021
281       F001    01 Small Benthic      155.00   35.00   35  2855.5000 2006
291       F002    01 Small Benthic       50.00   10.00 17.5   270.0000 2021
301       F003    01 Small Benthic        0.46   14.00   40   145.5000 2021
311       F004    01 Small Benthic       75.00   15.00 28.5   829.2917 2021
331       L001    01 Small Benthic     1450.00 1000.00   41  4371.0000 2021
341       L002    01 Small Benthic     1500.00  800.00   63  2915.0000 2021
351       L003    01 Small Benthic      200.00   70.00   29   945.0000 2021
361       L004    01 Small Benthic     1040.00 1000.00   37  4329.5000 2021
371       L005    01 Small Benthic     1000.00  500.00   22  2024.0000 2021
381       L006    01 Small Benthic     2700.00  450.00   40  1470.0000 2021
391       L007    01 Small Benthic     1500.00  300.00   27  1048.0000 2012
401       L008    01 Small Benthic      900.00  400.00   30  2664.0000 2021
411       L009    01 Small Benthic     1000.00  200.00   24  5935.0000 2021
421       L010    01 Small Benthic     1500.00 1000.00   28  4375.0000 2021
431       L011    01 Small Benthic     1300.00  260.00   25  3343.0000 2021
441       L012    01 Small Benthic      500.00  100.00   28  2369.0000 2021
451       L013    01 Small Benthic      400.00   67.00   26   716.0000 2021
461       L014    01 Small Benthic     3000.00  500.00   28  1500.0000 2021
471       L015    01 Small Benthic     1000.00  200.00   20  1657.0000 2021
481       L016    01 Small Benthic     3540.00  590.00   20  3930.0000 2021
491       L017    01 Small Benthic     4000.00  800.00   22  2619.0000 2021
501       L018    01 Small Benthic     2530.00  422.00   30  7897.0000 2021
511       L019    01 Small Benthic      455.00   76.00   23  2018.0000 2021
521       L020    01 Small Benthic     3000.00  500.00   23  4037.0000 2010
531       L021    01 Small Benthic     6300.00 1260.00   27  5709.0000 2021
541       L022    01 Small Benthic      450.00   90.00   26  1188.0000 2016
551       L023    01 Small Benthic      478.00  120.00   24   526.0000 2021
561       L024    01 Small Benthic     2100.00  350.00   20  2171.0000 2021
571       L025    01 Small Benthic      812.00  163.00   25   891.0000 2021
581       L026    01 Small Benthic     6000.00 1050.00   20  8748.0000 2021
591       L027    01 Small Benthic     4500.00  900.00   23  8197.0000 2021
601       L028    01 Small Benthic     2380.00  476.00   21  3621.0000 2012
611       L029    01 Small Benthic     1172.00  235.00   23  8493.0000 2021
621       L030    01 Small Benthic      780.00  130.00   15  4268.0000 2021
631       L031    01 Small Benthic     3500.00  700.00   20  1842.0000 2021
641       L032    01 Small Benthic      400.00  134.00   22  2304.0000 2021
651       L033    01 Small Benthic     1275.00  260.00   25  1244.0000 2021
661       L034    01 Small Benthic     1200.00  240.00   20  2211.0000 2002
671       L035    01 Small Benthic     1700.00  283.00   24  3023.0000 2005
681       L036    01 Small Benthic     2750.00  458.00   20  2798.0000 2021
701       L037    01 Small Benthic     1100.00  183.00   20  2645.0000 2021
711       L038    01 Small Benthic     1090.00  182.00   25   540.0000 2021
721       L039    01 Small Benthic     1300.00  217.00   19  1212.0000 2021
731       L040    01 Small Benthic     1250.00  210.00   19  1853.0000 2021
741       A001    01 Small Benthic     3050.00  762.50   22  1312.5000 1993
751       A002    01 Small Benthic      800.00  500.00   31  4528.0000 2011
761       A003    01 Small Benthic     1000.00  400.00   23  6165.0000 2001
771       A004    01 Small Benthic     1500.00  400.00   23  9650.0000 1993
791       A007    01 Small Benthic      500.00 2500.00   25  3886.0000 1972
811       A009    01 Small Benthic      200.00  400.00   21   594.0000 2014
831       A011    01 Small Benthic      750.00  225.00   28  1100.0000 1994
841       A012    01 Small Benthic     2000.00  500.00   26  2595.0000 1984
851       A013    01 Small Benthic        5.00    3.00   27   140.0000 2002
861       A014    01 Small Benthic     5000.00 2000.00   18  2135.0500 1987
871       A015    01 Small Benthic     2500.00  875.00   41  3033.0000 1996
881       A016    01 Small Benthic     1600.00  600.00   27  1993.0000 2014
891       A017    01 Small Benthic      120.00   80.00   26   626.0000 2006
901       A018    01 Small Benthic        0.00    0.01 <NA>    32.0000 1991
981       A026    01 Small Benthic      900.00  320.00   28  8025.0000 1991
991       A027    01 Small Benthic      400.00  200.00   25   957.0000 1993
1011      A029    01 Small Benthic     1800.00  800.00   27  2935.0000 2011
1021      F001    01 Small Benthic      155.00   35.00   35  2949.5000 2000
1031      F002    01 Small Benthic       20.00    2.00 17.5   130.0000 2006
1041      F003    01 Small Benthic        1.50   45.00   60   408.0000 1973
1051      F004    01 Small Benthic       75.00   15.00 28.5   328.4931 1982
1071      L001    01 Small Benthic     1450.00 1000.00   41  4880.0000 1996
1081      L002    01 Small Benthic     1500.00  800.00   63  3035.0000 2010
1091      L003    01 Small Benthic      200.00   70.00   29   982.0000 2015
1101      L004    01 Small Benthic     1040.00 1000.00   37  4802.5000 2011
1111      L005    01 Small Benthic     1000.00  500.00   22  2024.0000 1986
1121      L006    01 Small Benthic     2700.00  450.00   40  1930.0000 2002
1131      L007    01 Small Benthic     2000.00  350.00   27  1188.0000 1985
1141      L008    01 Small Benthic     2000.00  900.00   30  4630.0000 2000
1151      L009    01 Small Benthic     2000.00  400.00   33  5372.0000 1996
1161      L010    01 Small Benthic     4400.00 2000.00   28  6170.0000 1993
1171      L011    01 Small Benthic     1795.00  445.00   27  6136.0000 1999
1181      L012    01 Small Benthic     3600.00  720.00   28  2791.0000 1971
1191      L013    01 Small Benthic     2432.00  405.00   34  2939.0000 2009
1201      L014    01 Small Benthic     2260.00  377.00   28  2263.0000 1991
1211      L015    01 Small Benthic     1650.00  330.00   24  4977.0000 1983
1221      L016    01 Small Benthic      640.00  107.00   20  4758.0000 1988
1231      L017    01 Small Benthic     3750.00  750.00   28  3356.0000 1981
1251      L019    01 Small Benthic     2750.00  458.00   23  2952.0000 1996
1261      L020    01 Small Benthic     3500.00  583.00   40  4756.0000 1993
1271      L021    01 Small Benthic     6600.00 1320.00   27  9607.0000 1997
1281      L022    01 Small Benthic     1350.00  270.00   26  2035.0000 1970
1291      L023    01 Small Benthic      608.00  152.00   24  1362.0000 2003
1301      L024    01 Small Benthic     6443.00 1074.00   22  3063.0000 1999
1311      L025    01 Small Benthic     1209.00  242.00   31  1298.0000 1983
1321      L026    01 Small Benthic    12500.00 2083.00   20  4793.0000 1978
1331      L027    01 Small Benthic     4320.00  864.00   21  5456.0000 1986
1341      L028    01 Small Benthic     3780.00  756.00   32  8515.0000 1960
1351      L029    01 Small Benthic     4810.00  962.00   25  4044.0000 1979
1361      L030    01 Small Benthic     1740.00  290.00   25  2953.0000 2001
1371      L031    01 Small Benthic     6650.00 1330.00   24  4273.0000 1980
1381      L032    01 Small Benthic      700.00  233.00   22  8991.0000 1986
1391      L033    01 Small Benthic     2750.00  550.00   25  2706.0000 1984
1401      L034    01 Small Benthic     1900.00  380.00   28  3438.0000 1962
1411      L035    01 Small Benthic     3090.00  515.00   24  5297.0000 1963
1421      L036    01 Small Benthic     3230.00  538.00   23  4420.0000 1965```
Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
Thije Zuidewind
  • 87
  • 1
  • 1
  • 7
  • It's hard to experiment with your problem because we don't have your data. Could you edit your question to make it reproducible? You might include a small sample dataset, or use one of the built-in ones, but we should be able to get the same output as you get. – user2554330 Jul 16 '21 at 09:47

2 Answers2

1

As you not share the data with us I will give you only description how to solve it.

  • (1) ..eq.label.. is a regular character so you could apply strsplit on it and other string operations. You could write your own function which consume ..eq.label.. and return proper equation.
  • (2) Another idea which will not be very efficient will be to assess glm model twice and paste proper string equation instead of using ..eq.label...
  • Last (3) solution, using ggplot2::ggplot_build:
# generate artificial data

library(ggplot2)
library(ggpmisc)
#> Loading required package: ggpp
#> 
#> Attaching package: 'ggpp'
#> The following object is masked from 'package:ggplot2':
#> 
#>     annotate
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 = x, y = y,
                      group = c("A", "B"),
                      y2 = y * c(0.5,2),
                      w = sqrt(x))

# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)

# no weights
gg <- ggplot(my.data, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = formula) +   
  stat_poly_eq(formula = y ~ x,
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
               parse = TRUE, color = "black")

gg_raw <- ggplot_build(gg)

process_label <- function(x) gsub("~~~",")~~~", gsub(")~`=`~",")~`=`~exp(",x))

gg_raw$data[[3]]$label <- process_label(gg_raw$data[[3]]$label)

gtable <- ggplot_gtable(gg_raw)

cowplot::plot_grid(gtable)

Created on 2021-07-16 by the reprex package (v2.0.0)

polkas
  • 3,797
  • 1
  • 12
  • 25
  • Please excuse me, I thought that as it is a technical question that didn't have that much to do with my data I thought it wasn't necessary to add any. If I'm honest with myself I do not think my R skills are good enough to write my own function. The third option does partially work, however the numbers in the equation are still way of from the actual coefficients that come out of the model. Also as stat_poly_eq prints an R-squared value I was hoping stat_poly_eq could be modified in some way that it allows for such formulas to be printed. It looks however like it is a lot more complicated. – Thije Zuidewind Jul 19 '21 at 08:09
0

stat_poly_eq() is designed for equations of polynomials. For other types of formulas you can use stat_fit_tidy(), also from package 'ggpmisc'. See for a similar case, but using nls(), my answer to this other question

You can find also other examples in the package documentatiion

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23