10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Appendix D 364D.2 Creating new graphics in RIn this section we illustrate some of the a<strong>for</strong>ementioned techniques by creating graphics<strong>for</strong> a squareplot and a scatterplot with additional in<strong>for</strong>mation in the margins.■ Example D.3: A squareplot alternative to barplots and pie chartsThe New York Times does an excellent job with its statistical graphics. Its staff is bothcareful to use graphics in an appropriate manner and creative in making new graphics toshowcase details. A case in point is a graphic the Times uses in place of a barplot, dotplotor pie chart that we will call a squareplot.Figure D.2 Squareplot of c (21, 7, 6)The squareplot shows counts of categorical data. Unlike the barplot, the squareplot makesit easy to see the exact counts. Unlike the dotplot, it is can be read without consulting thescales. Unlike the pie chart, the squareplot’s relative areas are easy to discern.The basic idea is to use squares of different colors to represent each count. Thesquares and colors are laid out in a way that facilitates counting values and comparingdifferent plots. Figure D.2 shows an example, from which we can count that thecategories have counts 21, 7, and 6.The <strong>Using</strong>R package contains the function squareplot (), which is reproduced below.Creating the graphic is pretty simple. A helper function to draw a square using polygon ()is defined. Then the larger square is defined and laid out. An empty plot is made. Then anew vector, cols, is created to match the colors with the counts. This is done with rep () torepeat the colors. Finally, the squares are made and colored one-by-one using a <strong>for</strong> loop.The functions floor <strong>for</strong> the integer portion of a number and %% <strong>for</strong> the remainder afterdivision are employed.squareplot

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!