## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
## gear
## cyl 3 4 5
## 4 1 8 2
## 6 2 4 1
## 8 12 0 2
## gear
## cyl 3 4 5
## 4 3.125 25.000 6.250
## 6 6.250 12.500 3.125
## 8 37.500 0.000 6.250
## gear
## cyl 3 4 5 Sum
## 4 3.125 25.000 6.250 34.375
## 6 6.250 12.500 3.125 21.875
## 8 37.500 0.000 6.250 43.750
## Sum 46.875 37.500 15.625 100.000
## gear
## cyl 3 4 5
## 4 1 8 2
## 6 2 4 1
## 8 12 0 2
## gear
## cyl 3 4 5
## 4 9.090909 72.727273 18.181818
## 6 28.571429 57.142857 14.285714
## 8 85.714286 0.000000 14.285714
## gear
## cyl 3 4 5 Sum
## 4 9.090909 72.727273 18.181818 100.000000
## 6 28.571429 57.142857 14.285714 100.000000
## 8 85.714286 0.000000 14.285714 100.000000
## gear
## cyl 3 4 5
## 4 6.666667 66.666667 40.000000
## 6 13.333333 33.333333 20.000000
## 8 80.000000 0.000000 40.000000
## gear
## cyl 3 4 5
## 4 6.666667 66.666667 40.000000
## 6 13.333333 33.333333 20.000000
## 8 80.000000 0.000000 40.000000
## Sum 100.000000 100.000000 100.000000
## gear
## cyl 3 4 5
## 4 3.125 25.000 6.250
## 6 6.250 12.500 3.125
## 8 37.500 0.000 6.250
## gear
## cyl 3 4 5 Sum
## 4 3.12 25.00 6.25 34.38
## 6 6.25 12.50 3.12 21.88
## 8 37.50 0.00 6.25 43.75
## Sum 46.88 37.50 15.62 100.00
# 1) Create the Contingency Table for which you want a Pie Chart
tab1 <- table(mtcars$am)
# 2) Calculate percentages %
tab2 = prop.table(tab1)
percent <- round(tab2*100,1)
# 3a) Create labels for each pie in the chart
pielabels <- paste(percent, "%", sep="")
# 3b) Generate the Pie Chart
pie(tab2,
col = c("gray","black"),
labels = pielabels,
main = '% of cars by Transmission (am)',
cex = 1.1)
# 3c) Legend for the pie chart
legend("topright",
c("0","1"),
cex=0.8,
fill=c("gray","black"))tab1 <- table(mtcars$cyl)
tab2 <- prop.table(tab1)
tab3 <- round(tab2*100, 2)
# 2) Generate the Bar Plot
bp <- barplot(tab3,
xlab = "cyl", ylab = "Percent (%)",
main = "% Cars by number of cylinders (cyl=4,6,8)",
col = c("grey"),
ylim = c(0, 60))
# 3) (Optional) Display the percentages on the Bar Plot
text(bp, 0, tab3, pos=3)# 1)
tab1 <- table(mtcars$am, mtcars$cyl)
tab2 <- prop.table(tab1)
tab3 <- round(tab2*100, 2)
# 2) Grouped bar-plot
bp <- barplot(tab3,
xlab = "cyl", ylab = "Percent (%)",
main = "% Cars by number of cylinders (cyl=4,6,8) and transmission (am=0,1)",
col = c("lightblue","orange"),
beside = TRUE,
ylim = c(0, 50),
legend = rownames(tab3))
# 3) (Optional) Display percentage on the bars
text(bp, 0, tab3, pos = 3)# 1) Create table of counts
counts <- table(mtcars$am, mtcars$cyl)
# 2) Generate a Stacked Bar plot
barplot(counts,
col = c("white","black"),
xlab = "cyl", ylab = "Counts",
main = "Count Cars by cylinders (cyl=4,6,8) and transmission (am=0,1)",
beside = FALSE,
legend = rownames(counts))