繁体   English   中英

ggplot中的位置趋势线回归方程

[英]Position Trend line regression equation in ggplot

我正在尝试使用 ggplot geom_smooth()为三个变量(SA、SA1、SA2)绘制带有 R 方的趋势线方程。 在绘制三个变量时,我得到了重叠的方程。 我尝试使用stat_regline_equation(label.y = c(1.78e15,3.9e17,2.5e15))调整 y 实验室位置,但很不幸地失败了。 数据链接(要求: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))

在此处输入图片说明 我也试过

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

在此处输入图片说明

我遇到了同样的问题,但选择了错误的输入参数(col 和标签 - 这给我带来了严重的麻烦。在以下位置找到了一些富有成效的想法: https : //cran.r-project.org/web/packages/ggpmisc/ggpmisc。 pdf查看 stat_poly_eq 的注释。

对于我的目的,facet wrap 更直观。

然后使用你的第二个建议 stat_poly_eq 添加 label.y、label.x 和字体大小

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM