Pages

Saturday, June 8, 2019

Data Visualisations in R


Graphs are one of the easiest way to do some quick analysis on the data and help us get the basic information like variance, spread etc..

In this article we will understand how to do data visualisation using R and will download  SBI one year data (from NSE website) to play with it -  You can do the same by using Quantmod library as well.

> library(ggplot2)

#! read sbi stock data from the downloaded file
> SBI <- read.csv(file = "03-06-2018-TO-31-05-2019SBINALLN.csv", header=TRUE,sep = ",")

#! take only EQ series from the data
> SBIEQ <- SBI[SBI$Series == 'EQ',]

#! checking the types of data in various columns
> sapply(SBIEQ, typeof)
                 Symbol                  Series                    Date              Prev.Close
              "integer"               "integer"               "integer"                "double"
             Open.Price              High.Price               Low.Price              Last.Price
               "double"                "double"                "double"                "double"
            Close.Price           Average.Price   Total.Traded.Quantity                Turnover
               "double"                "double"               "integer"                "double"
          No..of.Trades         Deliverable.Qty X..Dly.Qt.to.Traded.Qty
              "integer"               "integer"                "double"

#!  converting Date into correct format as it was not in date format
> SBIEQ$Date <- as.Date(SBIEQ$Date,format = "%d-%b-%Y")

#! Plotting the graph
#! first argument is the data frame, second argument defines the variables to be used while 3rd argument is to define the kind of graph
#! please refer ggplot document as there are multiple options of graphs are available
> ggplot(SBIEQ,aes(x=Date, y= Close.Price)) + geom_point() + scale_x_date()

#! Adding one additional argument to see how the delivered Qty is vis-a-vis price
> ggplot(SBIEQ,aes(x=Date, y= Close.Price, color = Deliverable.Qty )) + geom_point() + scale_x_date()