1

This is a follow up question to Combine ggflags with linear regression in ggplot2

I have a plot like below with a log-linear model for x and y for certain countries that I have made in R with ggplot2 and ggflags:

enter image description here

The problem is when I want to print out the regression equation, the R2 and the p-value with the help of stat_regline_equation and stat_cor, I get values for a linear model and not the log-linear model I want to use.

How can I solve this?

library(ggplot2)
library(ggflags)
library(ggpubr)
library(SciViews)
    
set.seed(123)

Data <- data.frame(
  country = c("at", "be", "dk", "fr", "it"),
  x = runif(5),
  y = runif(5)
)

ggplot(Data, aes(x = x, y = y, country = country, size = 11)) +
  geom_flag() +
  scale_country() +
  scale_size(range = c(10, 10)) +
  geom_smooth(aes(group = 1), method = "lm", , formula = y ~ log(x), se = FALSE, size = 1) +
  stat_regline_equation(label.y = 0.695, 
                          aes(group = 1, label = ..eq.label..), size = 5.5) +
  stat_cor(aes(group = 1,
                 label =paste(..rr.label.., ..p.label.., sep = "~`,`~")),
             label.y = 0.685, size = 5.5, digits= 1) 

edit: I have also tried to use ln(x) instead of log(x) but I do not get any results when printing out the coefficient from that either.

KGB91
  • 630
  • 2
  • 6
  • 24
  • 1
    Why not compute the model outside of `ggplot`, extract what you need, and plot that? This is a more flexible approach than trying to compute and visualize a model entirely inside a plotting function. – jdobres Apr 03 '22 at 21:53
  • Well, I have done that too - but I thought it was possible to do it within `ggplot`? I have done it for linear models and it worked fine. Otherwise, I have to work with the regression model to print it in a nice way on the plot. – KGB91 Apr 03 '22 at 21:59

1 Answers1

1

There are four things you need to do:

  1. Provide your regression formula to the formula argument of stat_regline_equation
  2. Use sub to change "x" to "log(x)" in eq.label
  3. Change the x aesthetic of stat_cor to log(x)
  4. Fix the x limits inside coord_cartesian to compensate

ggplot(Data, aes(x = x, y = y, country = country, size = 11)) +
  geom_flag() +
  scale_country() +
  scale_size(range = c(10, 10)) +
  geom_smooth(aes(group = 1), method = "lm", , formula = y ~ log(x), 
              se = FALSE, size = 1) +
  stat_regline_equation(label.y = 0.695, label.x = 0.25,
                          aes(group = 1, label = sub("x", "log(x)", ..eq.label..)), 
                          size = 5.5,
                          formula = y ~ log(x),
                          check_overlap = TRUE, output.type = "latex") +
  stat_cor(aes(group = 1, x = log(x),
                 label =paste(..rr.label.., ..p.label.., sep = "~`,`~")),
             label.x = 0.25,
             label.y = 0.65, size = 5.5, digits= 1, check_overlap = TRUE) +
  coord_cartesian(xlim = c(0.2, 1))

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks, that solves the problem with the equation but R2 and P still come from the linear model as far as I can tell? Edit: this is true even if I use this code: ` stat_cor(label.y = 0.685, aes(group = 1, label =paste(..rr.label.., ..p.label.., sep = "~`,`~")), size = 5.5, formula = y ~ log(x), digits= 1, check_overlap = TRUE) ` – KGB91 Apr 03 '22 at 22:05
  • @KGB91 see my update. By the time things get this complex inside ggplot, it may be worth moving some of the work outside. Anyway, it _is_ possible - the values on the plot are correct, as you can see if you run `summary(lm(y~log(x), Data))` – Allan Cameron Apr 03 '22 at 22:21
  • Thanks, could you update the code as well? It would be easier to follow. – KGB91 Apr 03 '22 at 22:40
  • Sorry - thought I had. Done now. – Allan Cameron Apr 03 '22 at 22:49