# Listing 6.8 - Comparing kernel density plots
par(lwd=2)
install.packages("sm",repos = "http://cran.nexr.com")
##
## The downloaded binary packages are in
## /var/folders/mh/3t4pmyhs1m99g9fkhvxc62180000gn/T//Rtmpbx5j62/downloaded_packages
library(sm)
## Package 'sm', version 2.2-5.4: type help(sm) for summary information
attach(mtcars)
# create value labels
cyl.f <- factor(cyl, levels= c(4, 6, 8),
labels = c("4 cylinder", "6 cylinder", "8 cylinder"))
# plot densities
sm.density.compare(mpg, cyl, xlab="Miles Per Gallon")
title(main="MPG Distribution by Car Cylinders")
# add legend via mouse click
colfill<-c(2:(2+length(levels(cyl.f))))
cat("Use mouse to place legend...","\n\n")
## Use mouse to place legend...
legend("topleft", levels(cyl.f), fill=colfill)

detach(mtcars)
par(lwd=1)
# Listing 6.10 - Violin plots
install.packages("vioplot",repos = "http://cran.nexr.com")
##
## The downloaded binary packages are in
## /var/folders/mh/3t4pmyhs1m99g9fkhvxc62180000gn/T//Rtmpbx5j62/downloaded_packages
library(vioplot)
x1 <- mtcars$mpg[mtcars$cyl==4]
x2 <- mtcars$mpg[mtcars$cyl==6]
x3 <- mtcars$mpg[mtcars$cyl==8]
vioplot(x1, x2, x3,
names=c("4 cyl", "6 cyl", "8 cyl"),
col="gold")
title("Violin Plots of Miles Per Gallon")

# dot chart
dotchart(mtcars$mpg,labels=row.names(mtcars),cex=.7,
main="Gas Mileage for Car Models",
xlab="Miles Per Gallon")

# Listing 6.11 - Dot plot grouped, sorted, and colored
x <- mtcars[order(mtcars$mpg),]
x$cyl <- factor(x$cyl)
x$color[x$cyl==4] <- "red"
x$color[x$cyl==6] <- "blue"
x$color[x$cyl==8] <- "darkgreen"
dotchart(x$mpg,
labels = row.names(x),
cex=.7,
pch=19,
groups = x$cyl,
gcolor = "black",
color = x$color,
main = "Gas Mileage for Car Models\ngrouped by cylinder",
xlab = "Miles Per Gallon")
