Chapter 7 Default Scatter Plots in R
R base scatter plot: plot():
Example-1:
par(mfrow=c(2,1), mar=c(3,3,1,0)+.5, mgp=c(1.6,.6,0))
x <- mtcars$hp
y <- mtcars$mpg
# Plot with main and axis titles
# Change point shape (pch = 19) and remove frame.
plot(x, y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE,col="#FFC000")
# Add regression line
plot(x, y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE,col="#7573D9")
abline(lm(y ~ x, data = mtcars), col = "blue")
Example-2:
x <- mtcars$hp
y <- mtcars$mpg
# Add loess fit
plot(x, y, main = "Main title",
xlab = "X axis title", ylab = "Y axis title",
pch = 19, frame = FALSE,col="red")
lines(lowess(x, y), col = "blue")
Addtional->Enhanced scatter plots:
The components of the plot contains:
- the points
- the regression line (in green)
- the smoothed conditional spread (in red dashed line)
- the non-parametric regression smooth (solid line, red)
#install.packages("car")
library("car")
## Loading required package: carData
scatterplot(wt ~ mpg, data = mtcars)