Plotting

Create a text file sales.dat that contains data on our sales employees:

emp years salary sales
1  5 20495.00 20
2  5 22061.00 17
3  5 18464.00 24
4  6 23335.00 19
5  7 19658.00 24
6  8 22423.00 24
7  7 23552.00 21
8  8 21914.00 29
9  0 15000.00 13
10 0 15000.00 9

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

To get an overview, we want to read the file in R, attach the columns, and plot to a file:

  t <- read.table("sales.dat", header=TRUE)
  attach(t)
  salary
 [1] 20495 22061 18464 23335 19658 22423 23552 21914 15000 15000
  sales
 [1] 20 17 24 19 24 24 21 29 13  9
  png('sales.png')
  barplot(sales, main="# Sales", names.arg=round(salary/1000), xlab="Salary (K)")
  dev.off()

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.

The plot shows some interesting facts:

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

  png('sales-by-salary.png')
  plot(x=salary, y=sales, ylim=c(0, max(sales)))
  dev.off()

The limits for the y-axis are explicitely specified here, to start at zero. This plot shows that