简体   繁体   English

在每个图中绘制回归线方程(2的阶数)

[英]Plotting regression line equation (order of 2) in each plot

I have been working on some code to iterate creating scatter plots based on data from a data frame and exporting each scatter plot with an 2nd order regression line to a single PDF file with each page as its own scatter plot. 我一直在研究一些代码,以根据数据框中的数据迭代创建散点图,并将每个散点图(带有二阶回归线)导出到单个PDF文件,并将每个页面作为自己的散点图。 What I would like to do is produce the regression line equation and place it in the top left margin of the scatter plot for each iteration. 我想做的是生成回归线方程,并将其放置在每次迭代的散点图的左上角。

library(gridExtra)
library(purrr)
library(tidyverse)

plot_5 <-
    Infil_Data2 %>% 
    split(.$Site_ID) %>% 
    map2(names(.),
         ~ggplot(.x, aes(Sqrt_Time.x, Cal_Vol_cm)) + 
         geom_point() +
         labs(title = paste(.y)) +
         theme(plot.title = element_text(hjust = 0.5)) + 
         stat_smooth(mapping = aes(x = Sqrt_Time.x, y = Cal_Vol_cm),
                     method = "lm", se = FALSE, 
                     formula = y ~ poly(x, 2, raw = TRUE),
                     color = "red") +
         theme(plot.margin = unit(c(1, 5, 1, 1), "cm")))


    pdf("allplots5.pdf", onefile = TRUE)
    walk(plot_5, print)
    dev.off()

Here is a sample of the Infil_Data2 dataframe that I am using: 这是我正在使用的Infil_Data2数据帧的示例:

Infil_Data2 <-
    structure(list(Time = c(0L, 30L, 60L, 90L, 120L, 150L, 180L, 
    210L, 240L, 270L, 300L, 0L, 30L, 60L, 90L, 120L, 150L, 180L, 
    210L, 240L, 270L, 300L, 0L, 30L, 60L, 90L, 120L, 150L, 180L, 
    210L, 240L, 270L, 300L), Site_ID = c("H1", "H1", "H1", "H1", 
    "H1", "H1", "H1", "H1", "H1", "H1", "H1", "H2", "H2", "H2", "H2", 
    "H2", "H2", "H2", "H2", "H2", "H2", "H2", "H3", "H3", "H3", "H3", 
    "H3", "H3", "H3", "H3", "H3", "H3", "H3"), Vol_mL = c(63, 62, 
    60, 59, 58, 56, 54, 52.5, 50, 48.5, 46.5, 82, 77, 73, 68, 65, 
    51, 56, 52, 47.5, 42.5, 37.5, 69, 67, 65, 63, 61, 60, 58, 56, 
    54, 51.5, 49), Sqrt_Time.x = c(0, 5.477225575, 7.745966692, 9.486832981, 
    10.95445115, 12.24744871, 13.41640786, 14.49137675, 15.49193338, 
    16.43167673, 17.32050808, 0, 5.477225575, 7.745966692, 9.486832981, 
    10.95445115, 12.24744871, 13.41640786, 14.49137675, 15.49193338, 
    16.43167673, 17.32050808, 0, 5.477225575, 7.745966692, 9.486832981, 
    10.95445115, 12.24744871, 13.41640786, 14.49137675, 15.49193338, 
    16.43167673, 17.32050808), Cal_Vol_cm = c(0, 0.124339799, 0.373019398, 
    0.497359197, 0.621698996, 0.870378595, 1.119058194, 1.305567893, 
    1.616417391, 1.80292709, 2.051606688, 0, 0.621698996, 1.119058194, 
    1.74075719, 2.113776588, 3.854533778, 3.232834782, 3.730193979, 
    4.289723076, 4.911422072, 5.533121068, 0, 0.248679599, 0.497359197, 
    0.746038796, 0.994718394, 1.119058194, 1.367737792, 1.616417391, 
    1.865096989, 2.175946488, 2.486795986)), row.names = c(NA, 33L
    ), class = "data.frame")

Based off of what you did and the Adding Regression Line Equation and R2 on graph post, the code below produces a pdf with one plot per page, and with the equation in the plots. 根据您所做的工作以及在图形发布上添加回归线方程式和R2 ,下面的代码将生成一个pdf,每页包含一个图,并且图中包含方程式。 The equations appear in the same relative place on the plot even though the scales are changing. 即使比例在变化,方程也出现在图中相同的相对位置。 The original code in the question was very close and the code below only adds the call to stat_smooth_func . 问题中的原始代码非常接近,下面的代码仅将调用添加到stat_smooth_func

# Input data.
Infil_Data2 <-
structure(list(Time = c(0L, 30L, 60L, 90L, 120L, 150L, 180L, 
210L, 240L, 270L, 300L, 0L, 30L, 60L, 90L, 120L, 150L, 180L, 
210L, 240L, 270L, 300L, 0L, 30L, 60L, 90L, 120L, 150L, 180L, 
210L, 240L, 270L, 300L), Site_ID = c("H1", "H1", "H1", "H1", 
"H1", "H1", "H1", "H1", "H1", "H1", "H1", "H2", "H2", "H2", "H2", 
"H2", "H2", "H2", "H2", "H2", "H2", "H2", "H3", "H3", "H3", "H3", 
"H3", "H3", "H3", "H3", "H3", "H3", "H3"), Vol_mL = c(63, 62, 
60, 59, 58, 56, 54, 52.5, 50, 48.5, 46.5, 82, 77, 73, 68, 65, 
51, 56, 52, 47.5, 42.5, 37.5, 69, 67, 65, 63, 61, 60, 58, 56, 
54, 51.5, 49), Sqrt_Time.x = c(0, 5.477225575, 7.745966692, 9.486832981, 
10.95445115, 12.24744871, 13.41640786, 14.49137675, 15.49193338, 
16.43167673, 17.32050808, 0, 5.477225575, 7.745966692, 9.486832981, 
10.95445115, 12.24744871, 13.41640786, 14.49137675, 15.49193338, 
16.43167673, 17.32050808, 0, 5.477225575, 7.745966692, 9.486832981, 
10.95445115, 12.24744871, 13.41640786, 14.49137675, 15.49193338, 
16.43167673, 17.32050808), Cal_Vol_cm = c(0, 0.124339799, 0.373019398, 
0.497359197, 0.621698996, 0.870378595, 1.119058194, 1.305567893, 
1.616417391, 1.80292709, 2.051606688, 0, 0.621698996, 1.119058194, 
1.74075719, 2.113776588, 3.854533778, 3.232834782, 3.730193979, 
4.289723076, 4.911422072, 5.533121068, 0, 0.248679599, 0.497359197, 
0.746038796, 0.994718394, 1.119058194, 1.367737792, 1.616417391, 
1.865096989, 2.175946488, 2.486795986)), row.names = c(NA, 33L
), class = "data.frame")

Plotting code 绘图代码

# For the "stat_smooth_func", use the Laurae package.
# devtools::install_github("Laurae2/Laurae")

library(gridExtra)
library(purrr)
library(tidyverse)
library(Laurae)

plot_5 <-
    Infil_Data2 %>% 
    split(.$Site_ID) %>% 
    map2(names(.),
         ~ggplot(.x, aes(Sqrt_Time.x, Cal_Vol_cm)) + 
         geom_point() +
         labs(title = paste(.y)) +
         theme(plot.title = element_text(hjust = 0.5)) + 
         stat_smooth(mapping = aes(x = Sqrt_Time.x, y = Cal_Vol_cm),
                     method = "lm", se = FALSE, 
                     formula = y ~ poly(x, 2, raw = TRUE),
                     color = "red") +
         theme(plot.margin = unit(c(1, 5, 1, 1), "cm")) +
         stat_smooth_func(geom="text", method = "lm", hjust=0, parse=TRUE))


pdf("allplots5.pdf", onefile = TRUE)
walk(plot_5, print)
dev.off()

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

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