Basic Line Graph with Regression

Year <- c(1971,1972,1973,1974,1975,1976)
PopSize <- c(500,562,544,532,580,590)
df <- data.frame (Year, PopSize )
df
##   Year PopSize
## 1 1971     500
## 2 1972     562
## 3 1973     544
## 4 1974     532
## 5 1975     580
## 6 1976     590
##   Year PopSize
## 1 1971     500
## 2 1972     562
## 3 1973     544
## 4 1974     532
## 5 1975     580
## 6 1976     590
plot(df$Year, df$PopSize)

plot(df$Year, df$PopSize, type = "l")

plot(df$Year, df$PopSize, type = "l", lty = "dashed")

plot(df$Year, df$PopSize, type = "l", lty = "dotted")

plot(df$Year, df$PopSize, type = "l")

plot(df$Year, df$PopSize, type = "l", col = "red")

plot(df$Year, df$PopSize, type = "l", col = 3)

plot(df$Year, df$PopSize, type = "l", col = "red", lwd = 3)

plot(df$Year, df$PopSize, 
     type = "l", 
     col = "red", 
     lwd = 3,
     xlab = "Year",
     ylab = "Population Size",
     main = "Moomin Population Size on Ruissalo 1971 - 2001")

   Bsic Linear Regression
   
plot(df$Year, df$PopSize, 
     type = "l", 
     col = "red", 
     lwd = 3,
     xlab = "Year",
     ylab = "Population Size",
     main = "Moomin Population Size on Ruissalo 1971 - 2001")

fit1 <- lm (PopSize ~ Year, data = df) 
summary(fit1)  
## 
## Call:
## lm(formula = PopSize ~ Year, data = df)
## 
## Residuals:
##        1        2        3        4        5        6 
## -16.1905  31.7524  -0.3048 -26.3619   7.5810   3.5238 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -27190.438  10641.429  -2.555   0.0630 .
## Year            14.057      5.392   2.607   0.0596 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.56 on 4 degrees of freedom
## Multiple R-squared:  0.6295, Adjusted R-squared:  0.5369 
## F-statistic: 6.796 on 1 and 4 DF,  p-value: 0.05961
## 
## Call:
## lm(formula = PopSize ~ Year, data = df)
## 
## Residuals:
##        1        2        3        4        5        6 
## -16.1905  31.7524  -0.3048 -26.3619   7.5810   3.5238 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept) -27190.438  10641.429  -2.555   0.0630 .
## Year            14.057      5.392   2.607   0.0596 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.56 on 4 degrees of freedom
## Multiple R-squared:  0.6295, Adjusted R-squared:  0.5369 
## F-statistic: 6.796 on 1 and 4 DF,  p-value: 0.05961
plot(df$Year, df$PopSize,                              # x variable, y variable
     type = "l",                                                 # draw a line graphs
     col = "red",                                                # red line colour
     lwd = 3,                                                    # line width of 3
     xlab = "Year",                                              # x axis label
     ylab = "Population Size",                                   # y axis label
     main = "Moomin Population Size on Ruissalo 1971 - 2001")    # plot title

fit1 <- lm (PopSize ~ Year, data = df)             # carry out a linear regression
abline(fit1, lty = "dashed")                            # add the regression line to the plot
text(x = 1978, y = 750, labels = "R2 = 0.896\nP = 2.615e-15")   # add a label to the plot at (x,y)