Plotting
Plotting
Susan Holmes
setwd("~/RWork")
library("dplyr")
##
## Attaching package: 'dplyr'
y<- c(8, 33, 42.6, 138.1, 28.1, 199.5, 222.3, 66.8, 229.7)
plot(x,y)
Plot y (y-axis) versus x (x-axis) in a new window
## [1] 1.9 4.0 4.4 7.2 3.8 8.3 8.7 5.4 8.8
order(x)
## [1] 1 5 2 3 8 4 6 7 9
plot(x[order(x)],y[order(x)],type='l')
What happens if we do not reorder the points?
plot(x,y,type='l')
Extra arguments
We have extra arguments we can add to plots using:
plot(x[order(x)],y[order(x)],type='l',col="red",lty=3)
plot(x,y,type='p',col="blue",pch=5,main="Points")
pch: point type – 1=circle, 2=triangle, etc.
apropos("plot")
birthn data
load("birthn.RData")
attach(birthn)
summary(births)
Histogram
hist(birthn$births)
hist(birthn$births[day_of_week>5],col ="green")
summary(births)
hist(birthn$births[day_of_week>5],breaks=seq(5000,16000,by=1000),
col=rgb(1,0,0,0.5),xlim=c(5700,16000))
Boxplot
boxplot(birthn$births)
boxplot(birthn$births~day_of_week)
Notice the ~ sign this is useful for plotting with a formula.
Barplot
## [,1]
## [1,] 0.7
## [2,] 1.9
## [3,] 3.1
## [4,] 4.3
## [5,] 5.5
## [6,] 6.7
## [7,] 7.9
axis(at=bp,labels=c("M","T","W","Th","F","Sa","Su"),side=1)
Summary of this Session:
The plot function understands many different types of
objects and plots accordingly.