Sie sind auf Seite 1von 7

Stats 4590: Lab #2

Graphics and Printing R Output


Jan. 25, 2010

The focus of today’s lab will be to: (1) become acquainted with creating graphical displays
in R (2) creating files that contain the stuff that appears on the screen and R, and printing
this output.

Creating Graphs in R
An in last week’s lab, open a terminal window and to the ST4590 directory we created last
week. Since today is a new lab, we’ll create a new directory to store our results. We’ll call
the directory lab2:

$ cd ST4590
$ mkdir lab2
$ cd lab2
$ pwd

/.../ST4590/lab2

Before we start R, we’re going to set things up so we can get a file of our results. There
are different ways to do this, and some of you may use one of these other methods already.
If so, you do not have to use the method I suggest here.
We’ll save our results with the Linux command script. This command causes everything
typed after that point to be saved in a file. This saving will only stop when we type exit at
the $ prompt.

NOTE 1: If you don’t want to use the script command, you can just cut and paste the
output into a file that you have opened up with a text editor.

NOTE 2: If you are running R on your laptop or PC, there is an easy way to save the
on-screen output. Just select File–Save to File .. and all the on-screen output will be
saved to a text file.

$ script lab2
Script started, file is lab2
$

1
This tells us that everything that appears on the screen will go into the file lab2.

$ R

> x <- c(5,6,7,8,9,-1.2,-3.4)


> y <- scan()
1: 11 14 5 8.4 1.004
6: 2.34 5.67
8: end # Oops!
9: # hit <Enter>
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
scan() expected ’a real’, got ’end’

R was not happy when I typed end in. The error message is saying it expected a real
number, not a word.
Let’s try again:

> y <- scan()


1: 11 14 5 8.4 1.004
6: 2.34 5.67
8:
> length(x) # how many numbers are in x?
[1] 7
> length(y)
[1] 7

Now let’s issue some graphics commands.

> plot(x,y) # This plots x vs. y

> plot(x,y,type=’l’) # This plots x vs. y, and connects the points

2
# A histogram of x.
> hist(x)

# A stem-and-leaf plot of y. Note this is NOT drawn in the graphics


# window.

> stem(y)

The decimal point is 1 digit(s) to the right of the |

0 | 12
0 | 568
1 | 14

Now let’s do some work with the dataset on automobile data that we discussed in class.
First, let’s take a look at part of the dataset, which is built-in to R.

> mtcars[1:3,1:7]
mpg cyl disp hp drat wt qsec
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02
Datsun 710 22.8 4 108 93 3.85 2.320 18.61

We have learned that it can be easy to extract information from a data frame in R. We
can issue a logical command in R to see if mtcars is a data frame or not:

> is.data.frame(mtcars)
[1] TRUE

So it is a data frame. If it wasn’t (so FALSE would have been returned), we would use
the following command to change an R object (called object below) into a data frame:

> newobject <- as.data.frame(object)

Let’s construct some plots, starting with the miles per gallon, as we did in class:

3
> hist(mtcars$mpg)
Life could be easier if we could display several plots on one page. We use the par
command, with the mfrow option. The c(3,2) is telling R to break up the plotting region
into 3 rows and 2 columns, so it can hold a total of 6 plots.

> par(mfrow=c(3,2))

> hist(mtcars$mpg)

> hist(mtcars$mpg, breaks = c(10,20,30,40)) # to have our classes start


# at 10, 20, 30 and 40

> boxplot(mtcars$mpg)

Let’s construct a QQ (quantile-quantile) plot. If this gives a straight line (approxi-


mately), it is reasonable to assume the data come from a normal distribution.

> qqnorm(mtcars$mpg)

> plot(mtcars$disp,mtcars$hp)

Now let’s plot 2 boxplots beside each other, so they are on the same scale and easy to
compare. This is helpful for this data if we want to compare the mileage that cars with
manual vs. automatic transmissions obtain.
The == notation is telling R to extract the mileages that correspond to manual trans-
missions (am = 0) and then the mileages that correspond to automatics (am = 1).

boxplot(mtcars$mpg[mtcars$am==0], mtcars$mpg[mtcars$am==1])

Printing Graphs
We now have a full page of plots, so let’s print the page.

NOTE: Saving and print graphs is much easier in the version of R that would be on
your PC or laptop.

First we type a command called pdf():

4
> pdf()

This is telling R to save everything that we start graphing into a pdf file. Now we have
to type all our plotting commands again. In next week’s lab we’ll discuss a way to avoid this
part:

> par(mfrow=c(3,2))

> hist(mtcars$mpg)

> boxplot(mtcars$mpg)

> qqnorm(mtcars$mpg)

> plot(mtcars$disp,mtcars$hp)

> boxplot(mtcars$mpg[mtcars$am==0], mtcars$mpg[mtcars$am==1])

Note that R did not redraw any of these plots in the graphics window.
To tell R we’re finished with saving our graphs, type

> dev.off()

To see where R has saved these plots, open a new terminal window. Once you’re in that
window, go to the directory for today’s lab and look at the files:

$ cd ST4590/lab2
$ ls

Rplots.pdf

The file Rplots.pdf has the graph you just constructed. You can look at it under Linux
by typing

$ acroread Rplots.pdf

You can print it (if you have money on your printing account), or email to yourself to
print at home.
It’s often handy to put labels on our plots for names of variables, titles for the plot, and
so on. We have a few ways to accomplish this. We’ll begin by resizing our graphics window:

5
>par(mfrow=c(2,2))

Next, we’ll plot horsepower vs. displacement, and put in the variable names using xlab
and ylab:

> plot(mtcars$disp,mtcars$hp, xlab = ’Displacement’, ylab=’HP’)

If we want to put a title on the plot, we use the main option within plot:

> plot(mtcars$disp,mtcars$hp, xlab = ’Displacement’, ylab=’HP’,


main = ’HP vs. Disp’)

Here’s another way to do the same thing, using mtext:

> plot(mtcars$disp,mtcars$hp, xlab=’’,ylab=’’)


# The ’’ tell R not to put any labels on the x and y-axes for now.

> mtext(’Disp’, side = 1) # side = 1 tells R to put label on x-axis


> mtext(’HP’, side = 2) # side = 2 is y-axis
> mtext(’HP vs. Disp’) # if side not specified, title goes on top
Note that mtext didn’t put the x-axis and y-axis labels in very good places. See if this
improves things:

> plot(mtcars$disp,mtcars$hp, xlab=’’,ylab=’’)

> mtext(’Disp’, side = 1, line = 2)

> mtext(’HP’, side = 2, line = 2)

Finally, if we want a title for an entire page of plots, we would also use the mtext
command, where outer = T tells R this title will go on the outer margin of the page:
> mtext("Plots on Autos", outer = T, line = -1)
If you read the help file on the par command, you’ll find out about many more options
for graphical displays.
We’re done with R:

> q()

6
Now that we’re done with R, we can stop having all our output go into the file lab2. We do
this by typing exit at the $ prompt:

$ exit
Script done, file is lab2

There may be some funny stuff in your file; things like ^M or ^L symbols. To get rid of
these, you can type

$ /users/math/faculty/sneddon/bin/descript lab2 > lab2.sc

where lab2.sc will be the file without the funny stuff. You don’t have to call it lab2.sc;
give it any file name you want, but make sure the > is there.
Now you can edit the file lab2.sc, take out any unneeded information, and so on. This
is what you’ll want to do when preparing output to submit with an assignment or project.
You can also print it using the lp command.
Once you’re all done, simply log out.
More help on graphics in R can be accessed through the R information on the course
webpage. We will look at some more advanced graphics features in future labs.

Das könnte Ihnen auch gefallen