##Load the tidyverse package

library(tidyverse)
## -- Attaching packages ----------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts -------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

##Create data vectors for x and y

altorigin = c(90,230,240,260,330,400,410, 550,590,610,700,790)
x <- altorigin
#altitude of origin is our x variable
resprate = c(0.11,0.20,0.13,0.15,0.18,0.16,0.23,0.18,0.23,0.26,0.32,0.37)
y <- resprate
#rate of respiration is our y variable

##Create a scatterplot for x and y

##place data in a table
wind <- data.frame(x,y)
str(wind)
## 'data.frame':    12 obs. of  2 variables:
##  $ x: num  90 230 240 260 330 400 410 550 590 610 ...
##  $ y: num  0.11 0.2 0.13 0.15 0.18 0.16 0.23 0.18 0.23 0.26 ...
ggplot(data = wind, aes(x,y)) + geom_point(color = "blue")

##Add a regression line to the scatterplot

##use lm function to get intercepts for the regression line
reg1 <- lm(y~x,data=wind)
summary(reg1)
## 
## Call:
## lm(formula = y ~ x, data = wind)
## 
## Residuals:
##       Min        1Q    Median        3Q       Max 
## -0.067164 -0.021287 -0.000934  0.025648  0.054772 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.196e-02  2.520e-02   2.856 0.017075 *  
## x           3.186e-04  5.254e-05   6.063 0.000121 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0374 on 10 degrees of freedom
## Multiple R-squared:  0.7862, Adjusted R-squared:  0.7648 
## F-statistic: 36.76 on 1 and 10 DF,  p-value: 0.0001215
##first call the scatterplot again
with(wind,plot(x,y))
##now use abline to add the regression line
abline(reg1)