Section 24 Two Numerics
24.1 Function plot
The generic function plot
is used for plotting of R objects using X-Y coordinates.
For simple scatter plots, plot.default
will be used. However, there are plot
methods for many R objects, including function
, data.frame
, density
objects, etc.
For more details about the graphical parameter arguments that can be plugged into the plot
function, type: ?plot
.
24.2 A quick plot
function
Deterministic relationship between x and y
x <- c(1:20)
y <- 5*x^2
plot(x=x, y=y,
main='Scatter Plot',
xlab='Time (day)',
ylab='Growth (cfu)',
type='p', col='blue')
plot(x=x, y=y,
main='Scatter Plot',
xlab='Time (day)',
ylab='Growth (cfu)',
type='l', col='blue')
plot(x=x, y=y,
main='Scatter Plot',
xlab='Time (day)',
ylab='Growth (cfu)',
type='b', col='blue')
Try other type
options. See ?plot
for details.
- Annotate the plot
- Change the color to ‘red’ (
col
) - Use a different plot character (
pch
) - Use a different character expansion (
cex
) - Use a different line width (
lwd
) - Use a different line type (
lty
)
- Change the color to ‘red’ (
24.3 Standard Normal Distribution
Hint: Use dnorm
to generate normal density.
24.4 Explore the plot
function
?plot
set.seed(1234)
x <- rnorm(n = 20, mean=10, sd=2)
y <- 2*x + rnorm(n = 20, mean=0, sd=1)
DF <- data.frame(X=x, Y=y, C=LETTERS[1:20])
summary(DF)
X Y C
Min. : 5.309 Min. :11.08 A : 1
1st Qu.: 8.299 1st Qu.:15.71 B : 1
Median : 8.942 Median :18.00 C : 1
Mean : 9.499 Mean :18.42 D : 1
3rd Qu.:10.631 3rd Qu.:20.59 E : 1
Max. :14.832 Max. :29.20 F : 1
(Other):14
# plot function with x & y arguments
# plot(x = DF$X, y = DF$Y,
# type = 'n', xlab = '', ylab = '',
# xlim = c(6,14), ylim = c(12,28),
# col.axis = 'blue')
# plot function with formula
plot(Y ~ X, data = DF,
type = 'n', xlab = '', ylab = '',
xlim = c(6,14), ylim = c(12,28),
col.axis = 'blue')
title(main = 'Scatter Plot',
col.main='red',
cex.main=2,
xlab='Variable X',
ylab='Variable Y',
col.lab='purple',
cex.lab=1.25)
points(x = DF$X, y = DF$Y,
pch=16, col='orange', cex=2)
# points(x = DF$X, y = DF$Y,
# pch='*', col='blue', cex=2)
text(x = DF$X, y = DF$Y,
labels = DF$C,
cex=0.8, pos=4,
col='brown')
abline(h=mean(DF$Y), v=mean(DF$X), col='grey', lty=2, lwd=1)
# summary(lm(Y ~ X, data=DF))
abline(lm(Y ~ X, data=DF), col='red', lty='solid', lwd=2)
text(x = 8, y = 25,
labels = 'Y = 0.45 + 1.89X',
col = 'red', cex = 2)