Plotting

It's always a good idea to plot and re-arrange the data in various ways before jumping into any computation. A clever plot can reveal much about the data.

In [1]:
%reload_ext rpy2.ipython
In [4]:
%%R
pay <- c(2050, 2210, 1850, 2330, 1970, 2240, 2360, 2190, 1500, 1500)
sales <- c(20, 17, 24, 19, 24, 24, 21, 29, 13,  9)

barplot(sales, main="# Sales", names.arg=round(pay/100), xlab="Pay")

The plot shows some interesting facts about our sales force:

  • The top performing employee (number 8) does not get the highest pay

  • Number 7 gets the highest pay, although there are 4 people who sold more

  • The sales figure of number 9 is rather low but not much less than number 2 who gets paid a lot more

Instead of a bar plot we can plot the sales by pay i.e. using pay as x data and sales as the corresponding y data.

In [6]:
%%R
plot(x=pay, y=sales, ylim=c(0, max(sales)))

The limits for the y-axis are explicitely specified here, to start at zero. One would expect that higher pay is associated with higher sales; however, this plot shows that

  • There is not much structure in the data

  • The only indication of a trend are the two areas in the lower left and upper right

  • The upper right area shows practically no trend by itself

Plotting to an Image File

The plots above are shown interactively; for plotting to an image file add the statement

png('img.png')

before the plotting commands and the statement

dev.off()

afterwards. This results in an image file img.png being produced in your working directory. The available image formats depend on your installation; PNG should work everywhere.

Plotting to a file rather than interactively to the screen allows us to include the file in other documents, e.g. in this web page.

An option to view a plot even if you work remotely (i.e. the computer running R is not your workstation) is to put everything into the www/ directory, so you can view files with your web browser, including images; assuming of course that there is a web server running on the remote host. Enter the URL to your home page into the location bar, and add the filename.

In [ ]: