0

i am trying to plot trend lines equation with R square for three variable (SA,SA1,SA2) using ggplot geom_smooth(). While plotting three variables i get overlapping equation. I tried to adjust the y lab position using stat_regline_equation(label.y = c(1.78e15,3.9e17,2.5e15)) but miserably failed in doing so. DATA LINK (Requirement: 3 trend lines equation in varying position and remove the weird looking symbols in the legend) Task: Re-position the equation anywhere in the plot

library(ggplot2)
library(reshape2)
test <- read.xlsx2("filepath/test.xlsx", 1, header=TRUE)
> test
   year           SA          SA1         SA2
1  2008 1.409155e+15 3.632740e+17 4.06998e+15
2  2009 1.533598e+15 3.767342e+17 4.05015e+15
..
..
10 2017 1.761596e+15 3.581407e+17 3.03403e+15
11 2018 1.677707e+15 3.428239e+17 3.15862e+15
dput(test)
structure(list(year = structure(1:11, .Label = c("2008", "2009", 
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", 
"2018"), class = "factor"), SA = c(1409155313839800, 1533598052716370, 
1524727969175020, 1583941250825040, 1597021832828680, 1549362217661020, 
1607700438214130, 1592107298305410, 1735331260744350, 1761596167580970, 
1677707298223350), SA1 = c(363273957183114432, 376734225895083200, 
355896023882281984, 368398075167704192, 367791249493954048, 360257619620708800, 
360061958768956736, 367763926166363648, 355088403981918272, 358140732212706304, 
342823915606135936), SA2 = c(4.06998e+15, 4.05015e+15, 3.94057e+15, 
3.9507e+15, 3.58963e+15, 3.53037e+15, 3.43302e+15, 3.20139e+15, 
3.94638e+15, 3.03403e+15, 3.15862e+15)), row.names = c(NA, -11L
), class = "data.frame")
test$SA=as.numeric(levels(test$SA))[test$SA]
test$SA1=as.numeric(levels(test$SA1))[test$SA1]
test$SA2=as.numeric(levels(test$SA2))[test$SA2]
DF <- reshape2::melt(test, id.var = "year")
DF
   year variable        value
1  2008  SA 1.409155e+15
2  2009  SA 1.533598e+15
3  2010  SA1 1.524728e+17
4  2011  SA1 4.583941e+17
5  2012  SA2 3.597022e+15
6  2013  SA2 3.549362e+15
...
...

library(ggpmisc)
library(ggpubr)
library(dplyr)
library(tidyr)
library(ggplot2)

test.formula <- y ~ x
g<-ggplot(DF,aes(x = year, y = value, group = variable, colour = variable)) +facet_grid(variable ~ .)+
  geom_line() +
  geom_smooth(method = "lm", formula = test.formula) +theme_test()+
  scale_color_manual(values = c("black", "red", "blue")) +
  scale_y_continuous(name = " Primary axis")
p <-g+stat_regline_equation(label.y = c(1.78e15,3.9e17,2.5e15))

enter image description here i also tried

p<-g+stat_poly_eq(formula = my.formula, 
               aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")), 
               parse = TRUE)

enter image description here

Lalantra
  • 67
  • 1
  • 11
  • How would your desired outcome look like? – otwtm Jan 31 '20 at 09:06
  • i just want the equations not to overlap on line..like SA1 and SA2 is overlapping and equation followed by comma ',' instead of 'x' – Lalantra Jan 31 '20 at 09:08
  • Maybe [increase the upper limit](https://stackoverflow.com/a/48678947/786542) of the y-axis then put the equation there? – Tung Jan 31 '20 at 21:09
  • 1
    @Lalantra: for your 2nd question, see this https://stackoverflow.com/a/52704557/786542 – Tung Jan 31 '20 at 21:13
  • Thanks..@Tung Ratio scale is what i needed for the second part of the question. – Lalantra Feb 01 '20 at 07:28

1 Answers1

0

I ran into the same problem but had chosen the wrong input parameters (col and labels - which gave me serious troubles. Found some productive ideas at : https://cran.r-project.org/web/packages/ggpmisc/ggpmisc.pdf looking at the notes for stat_poly_eq().

The facet wrap is more intuitive for my purpose.

Then use your second suggestion for stat_poly_eq adding label.y, label.x and font size

ggplot(Standard_730_7_long,aes(x = date, 
                                  y = Value, 
                               # col = Hormones, These two columns causes trouble
                               # label = Value
                                  group = Hormones, 
                                  #colour = Hormones
                               )) +
    facet_wrap(Hormones ~ ., scales = "free")+
    #geom_line() + commented out as I do not want more than the regression line
    geom_smooth(method = "lm") +
    #theme_test()+ for my purpose this is not interesting
    #scale_y_continuous(name = " Primary axis")+
    stat_poly_eq( aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
                  #The above code is from your own suggestion
                      label.y = 1.0, #top and 0.0 is bottom
                 label.x = 0.0, # left
                 size = 3) # able to ajust font size
# A tibble: 6 × 7
  date       month   day time      hour Hormones     Value
  <chr>      <int> <int> <chr>    <int> <chr>        <dbl>
1 1901-01-01     1     1 07:00:00     7 TRH_ng.l   4948.  
2 1901-01-01     1     1 07:00:00     7 TSH_mU.l      2.69
3 1901-01-01     1     1 07:00:00     7 TT4_nmol.l  128.  
4 1901-01-01     1     1 07:00:00     7 FT4_pmol.l   18.6 
5 1901-01-01     1     1 07:00:00     7 TT3_nmol.l    3.38
6 1901-01-01     1     1 07:00:00     7 FT3_pmol.l    5.62

My Result

As per your suggestion

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
glensbo
  • 31
  • 1
  • 6