24 Slices
Medium 9780596801700

14. Graphics

Adler, Joseph O'Reilly Media ePub

R includes tools for drawing most common types of charts, including bar charts, pie charts, line charts, and scatter plots. Additionally, R can also draw some less familiar charts like quantile-quantile (Q-Q) plots, mosaic plots, and contour plots. The following table shows many of the charts included in the graphics package.

You can show R graphics on the screen or save them in many different formats. Graphics Devices explains how to choose output methods. R gives you an enormous amount of control over graphics. You can control almost every aspect of a chart. Customizing Charts explains how to tweak the output of R to look the way you want. This section shows how to use many common types of R charts.

To show how to use scatter plots, we will look at cases of cancer in 2008 and toxic waste releases by state in 2006. Data on new cancer cases (and deaths from cancer) are tabulated by the American Cancer Society; information on toxic chemicals released into the environment is tabulated by the U.S. Environmental Protection Agency (EPA).[35]

See All Chapters
Medium 9780596801700

7. R Objects

Adler, Joseph O'Reilly Media ePub

Table7-1 shows all of the built-in object types. I introduced these objects in Chapter3, so they should seem familiar. I classified the object types into a few categories, to make them easier to understand.

These are vectors containing a single type of value: integers, floating-point numbers, complex numbers, text, logical values, or raw data.

These objects are containers for the basic vectors: lists, pairlists, S4 objects, and environments. Each of these objects has unique properties (described below), but each of them contains a number of named objects.

These objects serve a special purpose in R programming: any, NULL, and .... Each of these means something important in a specific context, but you would never create an object of these types.

These are objects that represent R code; they can be evaluated to return other objects.

Functions are the workhorses of R; they take arguments as inputs and return objects as outputs. Sometimes, they may modify objects in the environment or cause side effects outside the R environment like plotting graphics, saving files, or sending data over the network.

See All Chapters
Medium 9780596801700

5. An Overview of the R Language

Adler, Joseph O'Reilly Media ePub

Learning a computer language is a lot like learning a spoken language (only much simpler). If youre just visiting a foreign country, you might learn enough phrases to get by without really understanding how the language is structured. Similarly, if youre just trying to do a couple simple things with R (like drawing some charts), you can probably learn enough from examples to get by.

However, if you want to learn a new spoken language really well, you have to learn about syntax and grammar: verb conjugation, proper articles, sentence structure, and so on. The same is true with R: if you want to learn how to program effectively in R, youll have to learn more about the syntax and grammar.

This chapter gives an overview of the R language, designed to help you understand R code and write your own. Ill assume that youve spent a little time looking at R syntax (maybe from reading Chapter3). Heres a quick overview of how R works.

See All Chapters
Medium 9780596801700

3. A Short R Tutorial

Adler, Joseph O'Reilly Media ePub

Lets get started using R. When you enter an expression into the R console and press the Enter key, R will evaluate that expression and display the results (if there are any). If the statement results in a value, R will print that value. For example, you can use R to do simple math:

The interactive R interpreter will automatically print an object returned by an expression entered into the R console. Notice the funny [1] that accompanies each returned value. In R, any number that you enter in the console is interpreted as a vector. A vector is an ordered collection of numbers. The [1] means that the index of the first item displayed in the row is 1. In each of these cases, there is also only one element in the vector.

You can construct longer vectors using the c(...) function. (c stands for combine.) For example:

is a vector that contains the first seven elements of the Fibonacci sequence. As an example of a vector that spans multiple lines, lets use the sequence operator to produce a vector with every integer between 1 and 50:

See All Chapters
Medium 9780596801700

20. Regression Models

Adler, Joseph O'Reilly Media ePub

A regression model shows how a continuous value (called the response variable, or the dependent variable) is related to a set of other values (called the predictors, stimulus variables, or independent variables). Often, a regression model is used to predict values where they are unknown. For example, warfarin is a drug commonly used as a blood thinner or anticoagulant. A doctor might use a regression model to predict the correct dose of warfarin to give a patient based on several known variables about the patient (such as the patients weight). Another example of a regression model might be for marketing financial products. An analyst might estimate the average balance of a credit card customer (which, in turn, affects the expected revenue from that customer).

Sometimes, a regression model is simply used to explain a phenomenon, but not to actually predict values. For example, a scientist might suspect that weight is correlated to consumption of certain types of foods, but wants to adjust for a variety of factors, including age, exercise, genetics (and, hopefully, other factors). The scientist could use a regression model to help show the relationship between weight and food consumed by including other variables in the regression. Models can be used for many other purposes, including visualizing trends, analysis of variance tests, and testing variable significance.

See All Chapters

See All Slices