cereal_read.table("cereal.txt",header=T) cereal attach(cereal) # so we can use variable names sodium sort(sodium) # ordered array stem(sodium) # stem and leaf plot stem(sodium,scale=0.5) # half as many rows stem(sodium,scale=2) # twice as many rows -- this is hard to read x11() # open graphics window hist(sodium) # histogram hist(sodium,breaks=4) # histogram with fewer bins hist(sodium,breaks=7) # it ignores you here hist(sodium,breaks=c(0,60,120,180,240,300,360)) # force the number of bins par(mfrow=c(2,2)) # put four plots on one page hist(sodium,breaks=4) hist(sodium,breaks=c(0,70,140,210,280,350)) hist(sodium,breaks=c(0,60,120,180,240,300,360)) hist(sodium) hist(sodium,breaks=c(0,70,140,210,280,350)) hist(sodium) hist(sodium,breaks=12) hist(sodium,breaks=23) par(mfrow=c(1,1)) # go back to one plot per page plot(sodium) # plot in order they appear in the dataset plot(sort(sodium)) plot(sugars,calories) # scatterplot hist(sugars) hist(calories) stem(calories) stem(calories,scale=2) plot(sodium, calories) table(type) # summary table table(mfr) table(mfr)/length(mfr) # percentages barplot(table(mfr)) # bar chart pie(mfr) # pie chart table(shelf) table(shelf,mfr) # contingency (cross-tabulation) table barplot(table(shelf,mfr),beside=T) # side-by-side bar chart table(shelf,mfr)/length(mfr) # table of total percentages round(100*table(shelf,mfr)/length(mfr),2) # nicer display # getting a table of row percentages is not easy -- one way: round(t(100*apply(table(shelf,mfr),1,function(x){x/sum(x)})),2) # another way: row.sums_apply(table(shelf,mfr),1,sum) round(100*sweep(table(shelf,mfr),1,row.sums,"/"),2) # and here is a table of column percentages: round(apply(table(shelf,mfr),2,function(x){x/sum(x)}),2)