Wednesday, 11 January 2017

Data Visualisation : An Introduction to GGPlot2 ( 05/01/2017 )



DATA VISUALIZATION USING GGPLOT2

BASIC

Visualizations bring data to life. A good visualization will give you new insights and will often lead to new ideas for additional analyses or visualizations. As humans we are much better at processing visual information than numeric information - both in terms of comprehension and speed. 

The ggplot2 library is one of the gems of R. The syntax for producing plots may appear at bit strange at first, but once we “get it”, it will be easy to  produce beautiful and insightful visualizations in no time. With ggplot2 one can  create visualizations by adding layers to a plot.
  • Any plot in ggplot2 consists of
    • Data
    • Aesthetics: which variables go on the x-axis, y-axis, colors, styles etc.
    • Style of plot: Bar, scatter, line etc. These are called plot layers in ggplot and are specified using the syntax geom_layer, e.g., geom_point, geom_line, geom_histogram etc.
Let’s get some data to plot. We will be working with Iris dataset which is a pre installed dataframe in R.


  • GLIMPSE  OF THE DATASET 





  • USING HEAD( )  FUNCTION TO VIEW THE FIRST FEW OBSERVATIONS






  • USING FUNCTION QPLOT( )
Firstly , we will plot sepal. length against petal.length and obtain the plot. Sepal.Length goes into the x axis , Petal.Length goes into the y axis and data refers to the iris dataframe. Here we have used a single color to denote all the points of the different species. 


We can also represent the different species by different colors as shown below using color=Species argument.

Similarly, we can let the size of each point denote sepal width, by adding a size = Sepal.Width argument. The scatter plot shown below is the modified version of the above plot in which an additional size agrument is added while plotting the two variables , this argument increases the size of the petal.width ,which increases the size of the dots in the scatter plots denoting the petal.width. Here we see that the Iris Setosa flowers have the smallest petals.


Here we see that we can add the value of alpha to avoid over plotting . So we add an argument  called alpha with a  value of  0.7 , to reduce the affects of over plotting 
Alpha transparency for overlapping elements expressed as a fraction between 0 (complete transparency) and 1 (complete opacity).


We add the axes labels and titles to the plot.


Specifies the geometric objects that define the graph type. The geom option is expressed as a character vector with one or more entries. geom values include "point", "smooth", "boxplot", "line", "histogram", "density", "bar", and "jitter".


In the scatterplot examples above, we implicitly used a point geom, the default when you supply two arguments to qplot().



In this plot we use the geometric object as line to define the graph object in the graph below the lines of different  colors shows the different species of the flowers plotted  below .


Orange is another built in data-frame that describes the growth of orange trees. Variation of orange tree circumference with age.


We can also plot both points and lines.




ADVANCED


















































11/01/2017- Data Visualization with R ( DETAILED)

DATA VISUALIZATION  IN R ( DETAILED ) 

With ever increasing volume of data in today's world , the need for data visualization has also increased . It is very important to effectively visualize the data so that we can tell stories and draw insights from the data.R programming offers a set of inbuilt functions and packages to visualize and present the data.
Data visualization in R can be broadly classified into the following two categories-
  • BASIC VISUALIZATION
  • ADVANCED VISUALIZATION
Basic visualization can be in the form of  histogram, line/bar chart , scatter plot , boxplot , etc. Advanced visualization can be in the form of heat map, mosaic map, correlogram, 3D graphs, map visualizations , etc.

  • HISTOGRAM
Histogram is basically a  plot that breaks the data into bins and shows frequency distribution of these bins. par ( mfrow=c(2,5)) enables us to fit multiple plots on the same page. We installed and called the library RColorBrewer and using the dataset VADeaths , drew a set of histograms. 



 Here we see that if the number of breaks is less than the number of colours specified , the colors just go to extreme values as in the " Set 3 8 colors " graph .However if the number of breaks is more than the number of colors , the colors start repeating as in the first row.


  • BAR/LINE CHART
LINE CHART-

The line chart below shows the increase in air passengers over given time period.Line charts are basically used to analyse trend over a period of time.



BAR CHART-

Bar plots are recommended when we wan to plot a categorical variable or a combination of categorical and continuous variable. 


  • BOX PLOT
Box plot is an important tool for visualizing the spread of the data and deriving inferences accordingly.A boxplot , also known as box and whisker plot , splits the dataset into quartiles .The body of the boxplot consists of a box which goes from the first quartile to the third quartile.Within the box, a vertical line is drawn as Q2 which represents the median of the dataset. 
Below is the code and output for drawing boxplots. 




In the code below , we have drawn four boxplots on the same page. The spread of sepal length across various categories of species can be easily visualized. In the last two graphs is the example of color palettes.Color palettes is a group of colors used to make the plot more appealing and visually creative.

  • SCATTER PLOT
Scatter plot is used to see the relationship between two continuous variables. We can draw both simple and multivariate scatter plots using the code below. We can also draw a simple pie chart using the code pie(table(iris$Species)). However , pie charts are not always a good choice because the human eye cannot visualize circular distances as good as it can visualize linear distances . Therefore, instead of using a pie chart , we can use a line graph.




  • HEXBIN PLOT
Hexagon binning is a form of bivariate histogram useful for visualizing the structure in datasets with large n. For better visual effect , we can use color palette and then draw the hexbin plot.




  • MOSAIC PLOT
Mosaic plot is a graphical method for visualizing data from two or more qualitative variables. It gives an overview of the data and makes it possible to visualize relationships between different variables. The area of the tiles , also known as the bin size is proportional to the number of observations within that category. 



  • HEAT MAP
A heat map is a two dimensional representation of data in which values are represented by colors. A single heat map provides an immediate summary of information. More complex heatmaps enable the reader to understand complex datasets. Below code is used to draw a heat map on the mtcars dataset.
An important point to note here is that we need to convert the dataset into matrix format. 




  • MAP VISUALIZATION
We can also visualize the data in R using javascript libraries. Leaflet is one of the most popular open-source JavaScript libraries for interactive maps. We can easily install it from github and plot the map using simple code.




  • 3D GRAPHS
We use the package RCommander which acts as graphical user interface (GUI).For this we simply install the RCmdr package and use the 3D plot option from within graphs. 
We can also make 3D graphs using Lattice package . Lattice can also be used for xyplots using the following simple code - 







  •  CORRELOGRAM
The corrgram function produces a graphical description of the correlation matrix , known as correlogram. The cells of the matrix can be colored or shaded to show the correlation value. Darker the color , higher is the correlation between the variables. Color intensity is proportional to the correlation value.





Data Visualization in R - 11/01/2017

DATA VISUALIZATION  IN R

With ever increasing volume of data in today's world , the need for data visualization has also increased . It is very important to effectively visualize the data so that we can tell stories and draw insights from the data.R programming offers a set of inbuilt functions and packages to visualize and present the data.

Data visualization in R can be broadly classified into the following two categories-
  • BASIC VISUALIZATION
  • ADVANCED VISUALIZATION
Basic visualization can be in the form of  histogram, line/bar chart , scatter plot , boxplot , etc. 
Advanced visualization can be in the form of heat map, mosaic map, correlogram, 3D graphs, map visualizations , etc.

  • SCATTER PLOT

In our above mart dataset, if we want to visualize the items as per their cost data, then we can use scatter plot chart using two continuous variables, namely Item_Visibility & Item_MRP as shown below.





Now, we can view a third variable also in same chart, say a categorical variable (Item_Type) which will give the characteristic (item_type) of each data set. Different categories are depicted by way of different color for item_type in below chart.

We can even make it more visually clear by creating separate scatter plots for each separate Item_Type as shown below.




  • HISTOGRAM

Histogram is basically a  plot that breaks the data into bins and shows frequency distribution of these bins. par ( mfrow=c(2,5)) enables us to fit multiple plots on the same page. We installed and called the library RColorBrewer and using the dataset VADeaths , drew a set of histograms. 



Here we see that if the number of breaks is less than the number of colours specified , the colors just go to extreme values as in the " Set 3 8 colors " graph .However if the number of breaks is more than the number of colors , the colors start repeating as in the first row.

  • BAR AND STACK BAR CHART

Here we see that if the number of breaks is less than the number of colours specified , the colors just go to extreme values as in the " Set 3 8 colors " graph .However if the number of breaks is more than the number of colors , the colors start repeating as in the first row.







  • BOXPLOT

Box plot is an important tool for visualizing the spread of the data and deriving inferences accordingly.A boxplot , also known as box and whisker plot , splits the dataset into quartiles .The body of the boxplot consists of a box which goes from the first quartile to the third quartile.Within the box, a vertical line is drawn as Q2 which represents the median of the dataset. 

Below is the code and output for drawing boxplots. 



  • AREA CHART

Area chart is used to show continuity across a variable or data set. It is very much same as line chart and is commonly used for time series plots. Alternatively, it is also used to plot continuous variables and analyze the underlying trends.



  • HEAT MAP
A heat map is a two dimensional representation of data in which values are represented by colors. A single heat map provides an immediate summary of information. More complex heatmaps enable the reader to understand complex datasets. Below code is used to draw a heat map on the mtcars dataset. An important point to note here is that we need to convert the dataset into matrix format. 



  • CORRELOGRAM
The corrgram function produces a graphical description of the correlation matrix , known as correlogram. The cells of the matrix can be colored or shaded to show the correlation value. Darker the color , higher is the correlation between the variables. Color intensity is proportional to the correlation value.From our dataset, let’s check co-relation between Item cost, weight, visibility along  with Outlet establishment year and Outlet sales from below plot.

  • MAP VISUALIZATION
We can also visualize the data in R using javascript libraries. Leaflet is one of the most popular open source JavaScript libraries for interactive maps. We can easily install it from github and plot the map using simple code.