1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
|
# Dot Plot #############################################################
par(mfrow=c(1,2))
numMpg = mtcars$mpg
dotchart(numMpg, labels=row.names(mtcars), cex=0.7,
main="Dot Plot for Car MPG",
xlab="Fuel Efficiency (MPG)")
tableMpg = structure(numMpg, .Dim = length(numMpg),
.Dimnames = structure(list(row.names(mtcars)), .Names = ""),
class = "table")
tableMpg
objBarPlot = barplot(tableMpg, names.arg=row.names(mtcars),
horiz=TRUE, space=1.5, col="lightblue", cex.names=0.7,
main="Car Distribution",
xlab="Fuel Efficiency (MPG)", axis.lty = 1)
text(x = tableMpg+1, y = objBarPlot+0.2, labels=as.character(tableMpg), cex=0.7)
# Dot Plot by Groups
par(mfrow=c(1,1))
mtcars$mpg
order(mtcars$mpg) # Not sort. Print the order
dfMtCarsSortByMpg = mtcars[order(mtcars$mpg),] # sort by mpg
head(dfMtCarsSortByMpg)
tail(dfMtCarsSortByMpg)
dfMtCarsSortByMpg$cyl = factor(dfMtCarsSortByMpg$cyl) # Transformed to factors
dfMtCarsSortByMpg$cyl
dfMtCarsSortByMpg$col[dfMtCarsSortByMpg$cyl==4] = "red"
dfMtCarsSortByMpg$col[dfMtCarsSortByMpg$cyl==6] = "blue"
dfMtCarsSortByMpg$col[dfMtCarsSortByMpg$cyl==8] = "darkgreen"
head(dfMtCarsSortByMpg)
tail(dfMtCarsSortByMpg)
dotchart(dfMtCarsSortByMpg$mpg, labels=row.names(dfMtCarsSortByMpg), cex=.7,
groups= dfMtCarsSortByMpg$cyl, gcolor="black", color=dfMtCarsSortByMpg$col,
main="Car MPG \n (Grouped by Cylinder)",
xlab="Fuel Efficiency (MPG)")
|