Base Plot

library(datasets)
data(cars)
# add a linear regression line to highlight the trends
with(cars, { plot(speed, dist) 
     lines(loess.smooth(speed, dist))
})
# add annotations
title("Speed vs stopping distance")

# copy the plot to a PNG file
dev.copy(png, file="carsStat01.png")
## quartz_off_screen 
##                 3
# close the PNG device
dev.off()
## quartz_off_screen 
##                 2
hist(airquality$Ozone)

# we are plotting ozone levels in New York by month, and the right hand side of the ~ indicate the month variable. However, we first have to transform the month variable in to a factor before we can pass it to boxplot(), or else boxplot() will treat the month variable as continuous.

airquality <- transform(airquality, Month = factor(Month))
boxplot(Ozone ~ Month, airquality, xlab = "Month", ylab = "Ozone (ppb)")

#scatterplot
with(airquality, plot(Wind, Ozone))

# base plot with annotations
with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City"))
with(subset(airquality, Month == 5), points(Wind, Ozone, col = "blue"))

with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City", type = "n"))
with(subset(airquality, Month == 5), points(Wind, Ozone, col = "blue"))
with(subset(airquality, Month != 5), points(Wind, Ozone, col = "red"))
legend("topright", pch = 1, col = c("blue", "red"), legend = c("May", "Other Months"))

# scatterplot with linear regression line
with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City", pch = 20))
## Fit a simple linear regression model
model <- lm(Ozone ~ Wind, airquality)
## Draw regression line on plot
abline(model, lwd = 2)

## Multiple base plots : panel plot with two plots
par(mfrow = c(1, 2))
with(airquality, {
  plot(Wind, Ozone, main = "Ozone and Wind")
  plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
})

# panel plot with three plots
par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0))
with(airquality, {
  plot(Wind, Ozone, main = "Ozone and Wind")
  plot(Solar.R, Ozone, main = "Ozone and Solar Radiation")
  plot(Temp, Ozone, main = "Ozone and Temperature")
  mtext("Ozone and Weather in New York City", outer = TRUE)
})

# smooth scatter for large datasets
set.seed(1)
x <- rnorm(10000)
y <- rnorm(10000)
smoothScatter(x, y)

ggplot2

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.3
data(mpg)
qplot(displ, hwy, data=mpg)

ggplot(airquality, aes(Temp, Ozone)) +
geom_point() +
geom_smooth(method = "loess", se = FALSE)
## Warning: Removed 37 rows containing non-finite values (stat_smooth).
## Warning: Removed 37 rows containing missing values (geom_point).

qplot(displ, hwy, data = mpg, color = drv)

qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))

# histograms
qplot(hwy, data = mpg, fill = drv, binwidth = 2)

qplot(hwy, data = mpg, facets = drv ~ ., binwidth = 2)

# boxplots
qplot(drv, hwy, data = mpg, geom = "boxplot")

#scatter plot
qplot(displ, hwy, data = mpg, facets = . ~ drv)

qplot(displ, hwy, data = mpg, facets = . ~ drv) + geom_smooth()

qplot(displ, hwy, data = mpg, facets = . ~ drv, geom = c("point", "smooth"))