R Graphing

From ECLR
Revision as of 12:15, 23 July 2015 by Rb (talk | contribs) (Created page with "Here we will introduce one of the most powerful tools R has to offer, graphical representation of data. Adrian Pagan, an excellent Australian econometrician once said "A...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Here we will introduce one of the most powerful tools R has to offer, graphical representation of data. Adrian Pagan, an excellent Australian econometrician once said "A simple plot tells a lot!". So let's see how to create simple plots.

Wit R being on open source software you will find quite a few different ways to produce graphics. Some are build, by default, into the R software, like the plot function. But as a regular user of R you will want to develop a wider range of graphics and then you will want to use a graphics package. The package we will introduce here is the lattice graphics package. But rather than directly installing the lattice package we propose to download and install a different package, the mosaic package, which includes the lattice package but has other useful features.

install.packages("mosaic")

As you know, the above package installation you will only have to do once on your computer.

library(mosaic)

When you load a package with the library() command you will often get some warning messages. On most occasions you can just ignore them.

Let's get a dataset to look at. We shall use the Baseball wages dataset

mydata <- read.csv("T:/ECLR/R/GraphIntro/mlb1.csv")

Let's check out what variables we have in this data-file

names(mydata)
##  [1] "salary"   "teamsal"  "nl"       "years"    "games"    "atbats"  
##  [7] "runs"     "hits"     "doubles"  "triples"  "hruns"    "rbis"    
## [13] "bavg"     "bb"       "so"       "sbases"   "fldperc"  "frstbase"
## [19] "scndbase" "shrtstop" "thrdbase" "outfield" "catcher"  "yrsallst"
## [25] "hispan"   "black"    "whitepop" "blackpop" "hisppop"  "pcinc"   
## [31] "gamesyr"  "hrunsyr"  "atbatsyr" "allstar"  "slugavg"  "rbisyr"  
## [37] "sbasesyr" "runsyr"   "percwhte" "percblck" "perchisp" "blckpb"  
## [43] "hispph"   "whtepw"   "blckph"   "hisppb"   "lsalary"

You can find short variable descriptions here