Advanced R Programming GGPLOT2 Notes
Advanced R Programming GGPLOT2 Notes
ggplot2:
ggplot2 is a plotting package that provides helpful commands
to create complex plots from data in a data frame. It provides a
more programmatic interface for specifying what variables to
plot, how they are displayed, and general visual properties.
Therefore, we only need minimal changes if the underlying data
change or if we decide to change from a bar plot to a
scatterplot. This helps in creating publication quality plots with
minimal amounts of adjustments and tweaking.
ggplot2 plots work best with data in the ‘long’ format, i.e., a
column for every variable, and a row for every observation.
Well-structured data will save lots of time when making figures
with ggplot2
Note: Before start the programming first we need to install ggplot2 package.
Also load the required package
#Install ggplot2 package
install.packages("ggplot2")
#Load ggplot2 package
library(ggplot2)
#Load ggplot2 package
library(dplyr)
Data set used
ggplot(data=mtcars)+labs(title="MTcars Data plot")
print(mtcars)
1) Aesthetic: The data is to map onto the Aesthetics attributes such as x-
axis, y-axis, color, fill, size, labels, alpha, shape, line width, line type
Example
ggplot(data=mtcars,aes(x=hp,y=mpg,col=disp))+labs(title="MTcars Data plot")
2) Geometric layer: How our data being displayed using point, line,
histogram, bar, boxplot
Example
Add the size
ggplot(data=mtcars,aes(x=hp,y=mpg,col="red"))+geom_point()+
labs(title="Miles per gallon vs horse power",
x="Horse power",
y="Miles per Gallon")
Example
Adding shape and color
ggplot(data=mtcars,aes(x=hp,y=mpg,col=factor(cyl),shape=factor(am)))+
geom_point()+labs(title="Miles per gallon vs horse power",
x="Horse power",
y="Miles per Gallon")
Example
ggplot(data=class,aes(x=Height,y=Weight,col=factor(Age),shape=factor(Sex)))+
geom_point()+labs(title="Miles per gallon vs horse power",
x="Height",
y="Weight")
Example
setwd("E:\\R programme\\R.Directory")
class<-read.csv("E:\\R programme\\R.Directory\\CLASS (2).CSV")
print(class)
View(class)
ggplot(data=class,aes(x=Age))+geom_histogram(binwidth=0.5)+
labs(title="Class Information",
x="Age",
y="Count")
3)Facets layer: ggplot2 in R facet layer is used to split the data up into
subsets of the entire dataset and it allows the subsets to be visualized on
the same plot.
Example
Separate row
dm<-ggplot(data=class,aes(x=Weight,y=Height,shape=factor(sex))
+geom_point()
dm+facet_grid(Age~.)+labs(title="Class Information",
x="Weight",
y="Height")
separate columns
p<-ggplot(data=class,aes(x=Weight,y=Height,shape=factor(sex)))
+geom_point()
p+facet_grid(.~Age)+
labs(title="class Information",
x="Weight",
y="Height")
print(mtcars)
Separate rows according to transmission type
p <- ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl))) +
geom_point()
p + facet_grid(am ~ .) +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")
p + facet_grid(. ~ cyl) +
labs(title = "Miles per Gallon vs Horsepower",
x = "Horsepower",
y = "Miles per Gallon")
Example
Step-1:selecting specific columns
selected_cols<-c("mpg","disp","hp","drat")
selected_data<-mtcars[,selected_cols]
selected_data
Step-2 creating histogram for each column
hist_plot_mgp<-ggplot(selected_data,aes(x=mpg))+
geom_histogram(binwidth=2,fill="blue",color="white")+
labs(title="Histogram:Miles per gallon",
x="Miles",y="frequency")
hist_plot_disp<-ggplot(selected_data,aes(x=disp))+
geom_histogram(binwidth=50,fill="red",color="white")+
labs(title="Histogram:displacement",
x="displacement",y="frequency")
hist_plot_hp<-ggplot(selected_data,aes(x=hp))+
geom_histogram(binwidth=20,fill="green",color="white")+
labs(title="Histogram:horse power",
x="horse power",y="frequency")
hist_plot_drat<-ggplot(selected_data,aes(x=drat))+
geom_histogram(binwidth=0.5,fill="orange",color="white")+
labs(title="Histogram:drat",
x="drat",y="frequency")