Simple plot using R

September 21, 2011

(This article was first published on We think therefore we R, and kindly contributed to R-bloggers)

As a task for my Financial eco assignment I had to plot a simple time series of the overnight MIBOR(Mumbai interbank offer rates) for the past one year . The job could very well have been done easily in MS-Excel but I choose to plot it in R instead and the quality of the graph, pixel-wise and neatness wise, was way better than what I could have obtained with MS-Excel. All this at the cost of a minimal 3 lines of code:

# The overnight MIBOR rates were stored in a file name “Call_Rates_2011.csv”, this is just a normal Excel file saved in a CSV(comma separated delimited) format that R can read.
# The way in which R conceptualizes the data is similar to that in Excel, to draw a simple analogy you can assume that the variable “a” now stores the entire Excel spreed sheet in it.
# You will have to make sure that the working directory is the one that contains the file “Call_Rates_2011.csv”

a <- read.csv(“Call_Rates_2011.csv”)

# The 2 column headers in my CSV file were “date” and “mibor”, so the below code plots “date” on the x-axis and “mibor” on the y-axis. The as.Date() tells R that the column “date” contains dates in the format “day-month-year”(‘%d-%b-%y’).
# a$(column header) is the standard way of referring to a column in the “spreadsheet” contained in “a”
# xlab : x-axis label
# ylab : y- axis label
# type : line(l)
# col : color of the line

plot(as.Date(a$date,’%d-%b-%y’), a$mibor, xlab= “Months”, ylab= “MIBOR overnight rates(percentage)”, type=’l’, col=’red’)

# This is to get the titles in place
# main : main title
# col.main : color of the main title
#font.main : font size of the title

title(main=”Overnight MIBOR rates for last one year”, col.main=”black”, font.main=4)

And the plot hence obtained thus looks like:

Incase you can’t make out the difference in the quality of the plot obtained just drop in your comments and email address and I will mail you the pdf and the jpg image of the plot. You can pull/stretch it to see that the pixels don’t get distorted and it looks way neater if you present it in your slide in a presentation.

To leave a comment for the author, please follow the link and comment on their blog: We think therefore we R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)