Data Visualization with R and ggplot2

*Updated code explanation

Prerequisite, Ubuntu 14.04.

Get the latest R version

Install R in your computer.

sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys E084DAB9
sudo add-apt-repository ‘deb http://star-www.st-andrews.ac.uk/cran/bin/linux/ubuntu trusty/’
sudo apt-get update
sudo apt-get install r-base

Install RStudio

RStudio is like an IDE for R language.

wget http://download1.rstudio.org/rstudio-0.98.1062-amd64.deb
sudo dpkg -i *.deb
rm *.deb

After installation has done, run RStudio.

Install ggplot2

I am going to be using ggplot2 package in this guide to make visualization and graphical analytics easy.

  1. Run RStudio
  2. Go to: Tools > Install Packages
  3. Install "ggplot2" package. "Install dependencies" check box should be checked.

Creating Visual Analytics

Two basic visualization I am going to demonstrate here is the Bar chart and Scatter plot.

1. The Bar Chart

Write these codes in the source code area:

library(ggplot2) # import ggplot2 package

ggplot(data=diamonds, aes(x=diamonds$cut)) + geom_bar()

The visualization will come out like this.

bar-chart

2. The Scatter Plot

library(ggplot2)

ggplot(data=diamonds, aes(x=diamonds$carat, y=diamonds$price)) + geom_point()

scatter-plot

Code Explanation

  1. library(ggplot2): load ggplot2 package
  2. data=diamonds: loads the diamonds data pre-packaged with ggplot2
  3. aes(x, y, ...): Generate aesthetic mappings that describe how variables in the data are mapped to visual properties (aesthetics) of geoms.
  4. diamonds$carat: from data diamonds, column carat
  5. geom_point: type of visualization we would want to generate. See here for more viz. Different geom requires different aes parameters.

Source:

  1. http://www.sharpsightlabs.com/
  2. http://www.thertrader.com/2014/09/22/installing-rrstudio-on-ubuntu-14-04/
  3. https://www.udacity.com/course/viewer#!/c-ud651/l-685569242/e-746818712/m-746818713
  4. http://docs.ggplot2.org/
Show Comments

Get the latest posts delivered right to your inbox.