# Basic plotting in R

*ADaish*

*January 2015*

### Basic plot

Plotting with continuous explanatory varibales

```
x<-1:10
y<-c(11,12,9,7,5,8,4,4,5,3)
plot(x,y,ylab='Response variable',xlab='Explanatory variable')
```

### Add a regression line

```
plot(x,y,pch=5,ylab='Response variable',xlab='Explanatory variable')
abline(lm(y~x)) # adds a regression line
```

### Add a specific line

```
plot(x,y,pch=5,ylab='Response variable',xlab='Explanatory variable')
lines(c(0,10,c(12,0),lty=2))# draw a line y=12 x=0 and when y=0 when x=10
```

#### Load data

`## [1] "xs" "ys"`

`## [1] "xs2" "ys2"`

`## [1] "upper" "lower" "rain" "month" "yr"`

`## [1] "tmax" "tmin" "day"`

### Plot data

#### Plot version 1

```
plot(xs,ys,col='red')#plot xv ys with red default circles
abline(lm(ys~xs))#arguement the linear model object lm((ys~xs))
points(xs2,ys2,col='blue')
abline(lm(ys2~xs2))# add a regression line to extra points
```

### Plot version 1

```
plot(c(xs,xs2),c(ys,ys2),xlab='x',ylab='y',type='n')#plot xs and xs2 together and ys and ys2 together type n means the axes scaled to encompass all the points from the data set
points(xs,ys,col='red')#plot xv and ys in red
points(xs2,ys2,col='blue')#plot xs and ys2 in col blue
abline(lm(ys~xs))# add the line for the regression between ys and xs
abline(lm(ys2~xs2))# add the regression for ys2 and xs2
```

### Add bounds to the graph

Find the range of the data to set the limits of the graph

`range(c(xs,xs2))# can find the value of the range of data `

`## [1] 0.0285 99.9326`

`range(c(ys,ys2))# find the range value of ys and ys2`

`## [1] 13.42 62.59`

```
plot(c(xs,xs2),c(ys,ys2),xlim=c(0,100),ylim=c(0,80),xlab='x',ylab='y',type='n') # add limits for x and y axis
points(xs,ys,col='red')#plot xs and ys in red
points(xs2,ys2,col='blue')#plot xs2 and ys2 in col blue
abline(lm(ys~xs))# add the line for the regression between ys and xs
abline(lm(ys2~xs2))# add the regression for ys2 and xs2
```

### Box plots

#### Plotting with catergorical explanatory variables

```
sex<-c("male","female")
month<-factor(month)#need to declare that month is a factor
is.factor(month)# check on the status of the variable by asking 'is this a factor?'
```

`## [1] TRUE`

`plot(month,upper)`

### Draw curved lines

```
x<-seq(0,10,0.1)# 10 or more values of x
y1<-2+3*x-0.25*x^2 # the equations want different colours for different curves
y2<-3+3.3*x-0.3*x^2
par(bg='ghostwhite')# paper colur bg means background white colour
plot(c(x,x),c(y1,y2),type='n',ylab='')# a blank axes
lines(x,y2,col='red')
lines(x,y1,col='blue')
```

### Draw sets of lines between points

`max(tmax)# maximum value`

`## [1] 10.8`

`min(tmin)# minimum value`

`## [1] -11.5`

```
plot(day,tmax,ylim=c(-12,12),type='n',ylab='Temperature')
points(day,tmin,col='blue',pch=16)
points(day,tmax,col='red',pch=16)
```

```
plot(day,tmax,ylim=c(-12,12),type='n',ylab='Temperature')
points(day,tmin,col='blue',pch=16)
points(day,tmax,col='red',pch=16)
join<-function(i){lines(c(i,i),c(tmin[i],tmax[i]),col='green')}# same as loop but quicker and reduce memory join together max and min temp for the same day with a green line
#sapply(1:31,join) # apply the function called join to clauses 1 to 31
for(i in 1:31){lines(c(i,i),c(tmin[i],tmax[i]),col='green')}# old fashion for loop put the line funciton in the loop
```