Plotting functions we will use today:
plot()
barplot()
boxplot()
hist()
pairs()
These commands should be mostly self-explanatory
## Remember:
mean(passengers$age)
[1] 30.31759
## Only one variable?
## X axis is row number.
## Can look like a trend.
plot(passengers$age)
## Use sample() to avoid.
plot(sample(passengers$age))
## X/Y Axis order matters!
plot(passengers$passenger_class,
passengers$age
)
plot()
## Now with boxplots!
plot(as.factor(passengers$passenger_class),
passengers$age
)
You can't create a barplot with raw data. It needs a table.
# Our passenger data has more males than females
table(passengers$sex)
female male
107 143
# Creates a table called tbl_sex
tbl_sex <- table(passengers$sex)
tbl_sex
female male
107 143
## Input is a table.
barplot(tbl_sex)
## Input is the output of table()
barplot(table(passengers$sex))
## Learn how to use the prop.table command.
## Use this command to build a table showing the
## proportions of males and females in passengers.
?prop.table
prop.table(tbl_sex)*100
female male
42.8 57.2
## Now use prop.table to build a proportional barplot.
barplot( prop.table(tbl_sex)*100 )
Hint: There's a nice plot on the next slide
Titanic in Cobh Harbour, County Cork Ireland