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 –recv-keys E084DAB9
sudo add-apt-repository ‘deb trusty/’
sudo apt-get update
sudo apt-get install r-base

Install RStudio

RStudio is like an IDE for R language.

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.


2. The Scatter Plot


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


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.


Show Comments

Get the latest posts delivered right to your inbox.