This R Markdown document is intended to give some examples of the basic graphics in R.

The first task is to retrieve the investor dataset revisited in class. This dataset is contained in a secured web site, so we need to call the downloader package and then use the download command.

The command will copy the dataset to the work directory to get it ready for analyses.

require(downloader)
## Loading required package: downloader
download("https://dl.dropboxusercontent.com/u/95175494/datasets/investor.RData", destfile="investor.RData")
load ("investor.RData") # Load the binary data
attach (investor)
investor
##   InvType Amount
## 1  Stocks  46500
## 2   Bonds  32000
## 3      CD  15500
## 4 Savings  16000

PIE CHART

To build a pie chart, we need to know the perecentages of each type of investment, this can be computed by two procedures at least:

Procedure 1 (The hard way):

# Percent <- numeric() # Generates an empty vector
# for (i in 1:length(Amount)){
#    Percent <- append(Percent, Amount[i]/sum(Amount)*100) #Append the percentage for each type of investment
# }

Procedure 2 (Much more easy!!!):

Percent <- Amount/sum(Amount)*100 
# Attach the percentage column to the data frame:
investor <- cbind (investor,Percent)
investor
##   InvType Amount  Percent
## 1  Stocks  46500 42.27273
## 2   Bonds  32000 29.09091
## 3      CD  15500 14.09091
## 4 Savings  16000 14.54545

Generate the pie chart. Procedure 1 (The hard way):

# slices <- c(Percent[1],Percent[2],Percent[3],Percent[4]) 
# lbls <- c("Stocks","Bonds","CD","Savings")
# pct <- round(slices/sum(slices)*100)
# lbls <- paste(lbls, pct) # add percents to labels 
# lbls <- paste(lbls,"%",sep="") # ad % to labels 
# pie(slices,labels = lbls, col=rainbow(length(lbls)),main="Pie Chart of an Investor")

Generate the pie chart. procedure 2 (Much more easy):

As with the first procedure, we need to execute the pie command to produce such a graphic.

lbls <- paste(InvType, ": ",round(Percent,2), " %", sep="")
pie(Percent,labels = lbls, col=rainbow(length(Percent)),main="Pie Chart of an Investor")

A 3D PIE:

First of all we need to call the plotrix package which must be already installed, once loaded, we use the pie3D command to build a 3D pie:

library(plotrix) # Load the package
pielabels <- paste(InvType, ": ",round(Percent,2), " %", sep="")
investPie <- pie3D(Percent, explode=0.1,col=rainbow(length(Percent)),main="Pie Chart of an Investor")
pie3D.labels(investPie, labels=pielabels, labelcex=0.8, labelcol="black")

A BAR CHART

To build a barchart, we need only to execute the barplot command.

lbls <- paste(InvType, ": ",round(Percent,2), " %", sep="")
bp <- barplot (Percent, horiz=TRUE, main="Barplot Chart of an Investor", ylab="Type of investment", xlab="Amount in thousands",col=rainbow(length(round(Percent))))
text(0, bp, lbls, cex=.8, pos=4)

A PARETO CHART

To produce a Pareto Chart, we need to call the qcc package which must be already installed. Once loaded to the work session, we can execute the pareto.chart command for this kind of graphic.

library (qcc)
## Package 'qcc', version 2.6
## Type 'citation("qcc")' for citing this R package in publications.
optionsPareto <- Percent
names(optionsPareto) <- c("Stocks","Bonds","CD","Savings")
pareto.chart (optionsPareto, ylab="Percentage of investment", xlab="Investment Type", main="Pareto Chart for the investor of example", cex.names=0.6, las=1)

##          
## Pareto chart analysis for optionsPareto
##           Frequency Cum.Freq. Percentage Cum.Percent.
##   Stocks   42.27273  42.27273   42.27273     42.27273
##   Bonds    29.09091  71.36364   29.09091     71.36364
##   Savings  14.54545  85.90909   14.54545     85.90909
##   CD       14.09091 100.00000   14.09091    100.00000

A LINE CHART

For this chart we use a dataset contained in the same repository, so we first need to load such dataset.

With the plot command we can produce a wide kinds of graphics, plot command is used in such way that we only indicate the x variable followed by the y variable. Then, we put the type of graphic needed, in this case type “b” is indicated.

download("https://dl.dropboxusercontent.com/u/95175494/datasets/UKinflationRate.RData", destfile="UKinflationRate.RData")
load ("UKinflationRate.RData") # Load the binary data
attach (UKinflationRate)
plot (Year,InfRate, col="red", type="b",cex=0.6, lwd=2, axes=FALSE, xlab="",ylab="")
axis(1, at=Year, labels=Year,cex.axis=0.7, tck=1)
axis(2, at=c(0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5), cex.axis=0.7, tck=1, las=2)
title(main="Uk Inflation Rate in the last 20 years",col.main="darkblue",xlab="YEAR",ylab="Inflation Rate",col.lab="blue",cex.lab=0.70, sub="Graphic by Carlos Rodriguez, PhD", col.sub="red",cex.sub=0.6)