---
title: "Siraj's R Notebook"
output: html_notebook
---

```{r}
plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
## the x- and y-axis, and an integer grid
abline(h = 0, v = 0, col = "gray60")
text(1,0, "abline( h = 0 )", col = "gray60", adj = c(0, -.1))
abline(h = -1:5, v = -2:3, col = "lightgray", lty = 3)
abline(a = 1, b = 2, col = 2)
text(1,3, "abline( 1, 2 )", col = 2, adj = c(-.1, -.1))
```

```{r}
## Simple Regression Lines:
require(stats)
sale5 <- c(6, 4, 9, 7, 6, 12, 8, 10, 9, 13)
plot(sale5)
abline(lsfit(1:10, sale5))
abline(lsfit(1:10, sale5, intercept = FALSE), col = 4) # less fitting

```


```{r}
z <- lm(dist ~ speed, data = cars)
plot(cars)
abline(z) # equivalent to abline(reg = z) or
abline(coef = coef(z))
summary(z)
## trivial intercept model
abline(mC <- lm(dist ~ 1, data = cars)) ## the same as
abline(a = coef(mC), b = 0, col = "blue")

```

```{r}
plot(circumference~age, pch = as.numeric(as.character(Tree)),data = Orange)
abline(lm(circumference~age, data = Orange, subset = Tree == "1"),lty = 1)
abline(lm(circumference~age, data = Orange, subset = Tree == "2"),lty = 2)
abline(lm(circumference~age, data = Orange, subset = Tree == "3"),lty = 3, lwd = 2)
abline(lm(circumference~age, data = Orange, subset = Tree == "4"),lty = 4)
abline(lm(circumference~age, data = Orange, subset = Tree == "5"),lty = 5)
legend("topleft", legend = paste("Tree", 1:5), lty = 1:5, pch = 1:5,lwd = c(1, 1, 2, 1, 1))
title("Line Plot")
mtext("The Effects of Different Bearings", side = 2, line = 2)
```

```{r}
data<-read.table("./globalmeantemp_annual.txt", header = TRUE)
data
```

```{r}
plot(data$YEARS, data$TEMP, xaxt = "n", type = "l")
axis(1, at=1:114, labels=data$YEARS[1:114])
plot(data$YEARS, data$TEMP, type = "l")
```

Use linear regression model to create trendline
```{r}
trend<-lm(data$TEMP~data$YEARS)
summary(trend)

#Replot data with trendline
slope<- trend$coefficients[2]
intercept <- trend$coefficients[1]
plot(data$TEMP~data$YEARS, type="l")
abline(trend, col="red")
```


```{r}
plot(data$TEMP~data$YEARS, type="l", ylim = c(-0.8,0.8), col="orange", xlab = "Year", ylab = "Temperature Anomaly", main = "Temperature Anomaly 1901 - 2014")
```

```{r}
plot(data$TEMP~data$YEARS, type="h", ylim = c(-0.8,0.8), axes = TRUE, xlab="Year", ylab = "Temperature Anomaly", col =ifelse(trend$residuals>0.0,"red","blue"))
```


```{r}
plot(data$TEMP~data$YEARS, type="h", ylim = c(-0.8,0.8), axes = FALSE, xlab="", ylab = "", col = ifelse(trend$residuals>0.0,"red","blue"))
par(new=TRUE)
plot(data$TEMP~data$YEARS, type="l", ylim = c(-0.8,0.8), col="black", xlab = "Year", ylab = "Temperature Anomaly", main = "Temperature Anomaly 1901 - 2014")
```
