Our Support: During the COVID-19 outbreak, we request learners to CALL US for Special Discounts!

- Data Science Blogs -

How Effective is the Graphics in R?

Graphics in R - Introduction  

Even to display a small line or dot on the CRT (Cathode Ray Tube) screen is a very difficult process. It seems very easy that cathode is emitted and bombards over the fluorescent screen and it glows. But it takes a lot of effort to direct the ray and control its frequency for the display of different colour over the screen. This controlling of ray before falling over the fluorescent screen is performed by various programming languages. Various algorithm and techniques are involved in the generation of graphics over the screen. Computer graphics is one of the most important achievements in its history, which will be required throughout the generation. R-programming is one such programming language. R has three graphical in r systems, namely: 

  1. Standard Graphics Package
  2. Lattice Package
  3. Grammar-of-graphics Package or ggplot2

In this blog, our first point of interest will be the third package of graphic with R i.e. Grammar-of-graphics or ggplot2. After that, there will be an introduction to the construction of two plot and their type of changing technique. Then we will proceed to the multi-panel display through explaining Coordinate transformation. But before we start one very important point to be noted is that plot() is the main function in the graphics through R. It automatically produces simple plots for vectors, functions or data frames and has many customization options too.

Grammar-of-graphics Package or ggplot2

We can understand Grammar-of Graphics as a framework that goes through a set predefined path in which you have to describe the visualization or graphics in a particular structured manner. This description involves multiple aspects, component, multidimensional data, etc. This package of Grammar-of-Graphics was first proposed by Leland Wilkinson. The name of the book is “THE GRAMMAR OF GRAPHICS” and it is recommended to read this book if are interested in this topic

ggplot2 is one of the most powerful and tangible packages of R graphics. It was first proposed by Hadley Wickham for creating fantastic and elegant graphics. Syntax of ggplot2:

ggplot (diamonds, aes(x=carat), color="blue")

 This ggplot2 divides the plot into three different fundamental parts:

 Plot = Aesthetics + Geometry + Data.

They are described as follows:

  • Data – The function always starts with data, which identifies how many dimensions you want to visualize
  • Aesthetic – This attribute checks the required axes based on the data dimensions, the position of different data points in the plot. It can be also used to control colour, size, shape or height of bars, etc.
  • Geometry – It defines the type graphics either histogram, box plot, density or dot plot, etc...

Apart from these three, there are some other attributes required in ggplot2, they are:

  • Scale – It is used when a specific scale of the graph is required to be mention.
  • Statistic – Used when one needs to show some statistical measure in the visualization like measures of central tendency, spread, confidence intervals, etc... 
  • Facet – Used when one needs to create subplot on certain data dimensions. 
  • Coordinate System – Used to tell that what kind of coordinate system is required in the visualization – either cartesian or polar.

Data Science Training - Using R and Python

Read: Job Description & All Key Responsibilities of a Data Scientist
  • Detailed Coverage
  • Best-in-class Content
  • Prepared by Industry leaders
  • Latest Technology Covered

Quick Plot

Quick Plot is a helper function denoted by qplot(). It can hide much of the complexities when creating standard graphs. It can be used to create the most common graph types. It does not expose the whole power of the ggplot2. But its can be used to create simple as well as most commonly used charts.

Below is the syntax of qplot():

qplot (X_coordinates, Y_coordinates, data=<123abc>, color=<red, blue, green, etc.), shape=<any geometry>, size=, alpha=, geom=, method=, formula=, facets=, xlim=, ylim=, xlab=, ylab=, main=, sub=)

Following are the description of those terms that were not discussed earlier:

  • Alpha – It refers to the transparency of the object and expressed as fraction 0 to 1 as completely transparent to complete opaque respectively.
  • xlab, ylab – It refers to the label that is to be specified to x and y-axis. 
  • xlim, ylim – It refers to the minimum and maximum limit of horizontal and vertical axes. It is a two-element
  • x, y – It specifies the variables allocated to vertical and horizontal axes.
  • Formula – This is used to employ more efficient smoothing algorithms.
  • Main, Sub – To specify titles and subtitles.

Incremental Plot and its Construction

The incremental plot is nothing but a dynamic or time series plotting in which the visualization is customized automatically after every updating of the data. This type of plot build using various time series packages and different plotting system such as ts, zoo, xts, etc. and base R, lattice, etc.…

Following is an example of Incremental plotting:

library(ggplot2)
library(xts)
library(dygraphs)
 i_url <- <dataset>

l_url <- <dataset>
y.read <- function(url){
   dat <- read.table(url,header=TRUE,sep=",")
   df <- dat[,c(1,5)]
   df$Date <- as.Date(as.character(df$Date))
   return(df)}

i  <- y.read(i_url)

Read: How to work with Deep Learning on TensorFlow?

l <- y.read(l_url)
ggplot(i,aes(Date,Close)) + 
  geom_line(aes(color="i")) +
  geom_line(data=l,aes(color="l")) +
  labs(color="Legend") +
  scale_colour_manual("", breaks = c("i", "l"),
                          values = c("blue", "brown")) +
  ggtitle("Closing Stock Prices: I & Linkedin") + 
  theme(plot.title = element_text(lineheight=.7, face="bold"))

Coordinate Transformation

We can transform this information to another informational collection with another CRS utilizing the spTransform function from the rgdal package

we utilize the Robinson projection. First, we have to locate the correct documentation.

newcrs <- CRS ("+proj=robin +datum=WGS84")

Now use it

rob <- spTransform(p, newcrs)

rob
## class       : SpatialPolygonsDataFrame
## features    : 12
## extent      : 471320.7, 536010.5, 5269709, 5345677  (xmin, xmax, ymin, ymax)
## crs         : +proj=robin +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
## variables   : 5
## names       : ID_1, NAME_1, ID_2,   NAME_2, AREA
## min values  : 1, Diekirch,    1, Capellen, 76
## max values  : 3, Luxembourg,   12, Wiltz, 312

After this transformation the units of geometry are no longer in degrees, instead, they are in the form of meters.

It is easy to transform vector data from lat/lon coordinate to planer or vice versa without loss of any precision. But it is not same with raster graph plot. A raster consists of rectangular cells of a similar size (regarding the units of the CRS; their genuine size may fluctuate). It can't transform cell by cell. Or maybe evaluates for the estimations of new cells must be made dependent on the qualities in the old cells. On the off chance that the qualities are class information, the 'closest neighbour' is commonly utilized. In any case a type of interpolation (for example 'bilinear'). 

Multipanel Display

The multipanel display is to put multiple graphs in a single plot by passing graphical parameters. There are four main approaches to multipanel layouts in R:

Read: How to import Data into R using Excel, CSV, Text and XML

max. temp    # vector used for plotting

Sun Mon Tue Wed Thu Fri Sat
22  27 26  24 23 26  28
par (mfrow=c (1,2)) # set the plotting area into a 1*2 array
barplot (max. temp, main="Barplot")
pie (max. temp, main="Pie chart", radius=1)

Output:

 Graphics in R with output

     Figure 1 Output of Multipanel Display Program
 

  • Layout () - Notwithstanding what you can do with par (mfrow), design () lets you consolidate panels.

Conclusion

At last, it is drawn out from the blog that R programming is one of the best modes of visualization of data as it consists of quick plot feature which helps create a common plot with much less complexity, on the other hand, it has incremental of dynamic or Time series based graph which can be used to visualize time series data. We have functions for the transformation of the coordinate and functions for multipanel displays. Hence Graphics in R could be the best option for plotting graphs in any type of data analysis. 

Please leave query and comments in comment sections. 

Read: Statistics Interview Questions and Answers



    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


Comments

Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

-0 day 05 Aug 2020

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

5 days 10 Aug 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

12 days 17 Aug 2020

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

2 days 07 Aug 2020

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

5 days 10 Aug 2020

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

2 days 07 Aug 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

-0 day 05 Aug 2020

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

9 days 14 Aug 2020

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

2 days 07 Aug 2020

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

2 days 07 Aug 2020

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

11 days 16 Aug 2020

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

2 days 07 Aug 2020

Search Posts

Reset

Receive Latest Materials and Offers on Data Science Course

Interviews