Укажите уравнение регрессии линии тренда в ggplot - PullRequest
0 голосов
/ 31 января 2020

Я пытаюсь построить уравнение линий тренда с квадратом R для трех переменных (SA, SA1, SA2), используя ggplot geom_smooth(). При построении трех переменных я получаю перекрывающееся уравнение. Я попытался отрегулировать положение лаборатории с помощью stat_regline_equation(label.y = c(1.78e15,3.9e17,2.5e15)), но с треском провалился. DATA LINK (Требование: уравнение 3 линий тренда в изменяющемся положении и убрать странные символы в легенде) Задача: Переместить уравнение в любом месте графика

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 я также пытался

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

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...