Chapter5

# Chapter5 - Chapter 5 R Chapter 5 5.1 R Graphics Bar Chart A...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 5 R - 1 - Chapter 5 R Graphics 5.1 Bar Chart A bar chart draws a bar with a height proportional to the count in the table. The height could be given by the frequency, or the proportion. The graph will look the same, but the scales may be different. Suppose a group of 25 people are surveyed as to their beer-drinking preference. The categories were (1) Domestic can, (2) Domestic bottle, (3) Microbrew and (4) Import. The raw data is 3 4 1 1 3 4 3 3 1 3 2 1 2 1 2 3 2 3 1 1 1 1 4 3 1 Let's make a bar chart of both frequencies and proportions. First, we use the scan function to read in the data then we plot a bar chart > beer = scan() 1: 3 4 1 1 3 4 3 3 1 3 2 1 2 1 2 3 2 3 1 1 1 1 4 3 1 26: Read 25 items > barplot(beer) # this isn't correct > barplot(table(beer)) # Yes, call with summarized data > barplot(table(beer)/length(beer)) # divide by n for proportion a. We did 3 barplots. The first to show that we don't use barplot with the raw data. b. The second shows the use of the table command to create summarized data, and the result of this is sent to barplot creating the barplot of frequencies shown. c. Finally, the command > table(beer)/length(beer) 1 2 3 4 0.40 0.16 0.32 0.12

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 5 R - 2 - produces the proportions first. (We divided by the number of data points which is 25 or length(beer).) The result is then handed off to barplot to make a graph. Notice it has the same shape as the previous one, but the height axis is now between 0 and 1 as it measures the proportion and not the frequency. d. This is ugly, you can change the color by specifying col > barplot(table(beer)/length(beer),col=c("lightblue", "mistyrose", "lightcyan","cornsilk"))
R - 3 - 5.2 Pie Charts The same data can be studied with pie charts using the pie function. Here are some simple examples illustrating the usage (similar to barplot(), but with some added features). > beer.counts = table(beer) # store the table result > pie(beer.counts) # first pie -- kind of dull > names(beer.counts) = c("Domestic\n can","Domestic\n bottle", "Microbrew","Import") # give names > pie(beer.counts) # prints out names # now with colors > pie(beer.counts,col=c("purple","green2","cyan","white")) The names() allows us to specify names to the categories. The resulting piechart shows how the names are used. Finally, we added color to the piechart. This is done by setting the piechart attribute col. We set this equal to a vector of color names that was the same length as our beer.counts . The help command ( ?pie ) gives some examples for automatically getting different colors, notably using rainbow and gray. Notice we used additional arguments to the function pie. The syntax for these is name=value . The ability to pass in named values to a function makes it easy to have fewer functions as each one can have more functionality. Try it for a bar chart!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 19

Chapter5 - Chapter 5 R Chapter 5 5.1 R Graphics Bar Chart A...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online