Data Visualization using ggplot2 Package in R

Ph.D. Course Work - Computer Application

Part 2

———————————————————————–

What is ggplot2?
* A package termed as a grammar of Graphics  for visualization of data.
Layers of ggplot
  • Data & aesthetics: The first layer is comprised of data frame and a function aes() that defines the aesthetics(Variable mapping with axes,graph features like size,color etc.)
  • Geometrics: How the data being displayed using point, line, histogram, bar, boxplot
  • Facets: It displays the subset of the data using Columns and rows
  • Statistics: Binning, smoothing, descriptive, intermediate
  • Coordinates: the space between data and display using Cartesian, fixed, polar, limits
  • Themes: Set of themes that can be used in the graph.
Scatter Diagram
#Remove the environment variable
rm(list=ls())
#Load Packages
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
#Load Compensation Data File
library(readr)
compensation = read_csv("D:\\R Course\\DataAnalysis\\Data\\datasets-master\\compensation.csv")
## Rows: 40 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Grazing
## dbl (2): Root, Fruit
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ggplot(compensation,aes(x=Root,y=Fruit))+geom_point()

#Use black and white background
ggplot(compensation,aes(x=Root,y=Fruit))+geom_point()+theme_bw()

#Increase the size of the point
ggplot(compensation,aes(x=Root,y=Fruit))+geom_point(size=3)+theme_bw()

#Add axis labels
ggplot(compensation,aes(x=Root,y=Fruit))+geom_point(size=3)+xlab("Root Biomass")+ylab("Fruit Production")+theme_bw()

#Add colors for different groups based on a factor
ggplot(compensation,aes(x=Root,y=Fruit,color=Grazing))+geom_point(size=3)+xlab("Root Biomass")+ylab("Fruit Production")+theme_bw()

Box-and-whisker plots
ggplot(compensation,aes(x=Grazing,y=Fruit))+geom_boxplot()+xlab("Grazing Treatment")+ylab("Fruit Production")+theme_bw()

#Add raw data changing size, color and transparancy
ggplot(compensation,aes(x=Grazing,y=Fruit))+geom_boxplot()+geom_point(size=3,color='red',alpha=.5)+xlab("Grazing Treatment")+ylab("Fruit Production")+theme_bw()

Histogram
ggplot(compensation,aes(x=Fruit))+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

#Adding custom binwidth
ggplot(compensation,aes(x=Fruit))+geom_histogram(binwidth = 15)

#Choosing no of bins
ggplot(compensation,aes(x=Fruit))+geom_histogram(bins=10)

#Faceting based on some factor 
ggplot(compensation,aes(x=Fruit))+geom_histogram(bins=10)+facet_wrap(~Grazing)

##### Saving the figure

ggsave("MyFirstFigure.png")
## Saving 7 x 5 in image