R - ggplot2 examples

Recently I had conducted training on ggplot2. So, I thought to share some of the sample code examples with graph output here. I am sharing only graph examples here. To understand ggplot2 go through this post.

Here I have tried to include all type of sample graphs / charts which are useful in data analytics

Example 1- Bar charts

---------------------------------------------------------------------------------------------------------------------------------------------------------------

# library library(ggplot2) search()

# Dataset specie=c(rep("KYC-PASS" , 3) , rep("KYC-LIC" , 3) , rep("KYC-PAN" , 3) , rep("KYC-AADHAR" , 3) ) condition=rep(c("GROUP1" , "GROUP2" , "GROUP3") , 4) value=abs(rnorm(12 , 0 , 15)) data=data.frame(specie,condition,value)

data

# Grouped ggplot(data, aes(fill=condition, y=value, x=specie)) + geom_bar(position="dodge", stat="identity")

# Stacked ggplot(data, aes(fill=condition, y=value, x=specie)) + geom_bar( stat="identity")

# Stacked Percent ggplot(data, aes(fill=condition, y=value, x=specie)) + geom_bar( stat="identity", position="fill")

# Change color of Bars ggplot(data, aes(fill=condition, y=value, x=specie)) + geom_bar( stat="identity", position="fill") + scale_fill_brewer(palette = "Set1")

# Faceting ggplot(data, aes(y=value, x=specie, color=specie, fill=specie)) + geom_bar( stat="identity") + facet_wrap(~condition)

Example 2 - Scatter Plots

---------------------------------------------------------------------------------------------------------------------------------------------------------------

library(ggplot2)

head(iris)

# Normal Scatter Plot ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_point(size=6, alpha=0.6)

# Color depend on factor (categorical variable) ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + geom_point(size=6, alpha=0.6)

# Color and shape depend on factor (categorical variable) ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species, shape=Species)) + geom_point(size=6, alpha=0.6)

Example 3 - Treemap / Mozaic plot

---------------------------------------------------------------------------------------------------------------------------------------------------------------

# library # install.packages("treemap") library(treemap)

# Create data group=c("group-1","group-2","group-3") value=c(13,5,22) data=data.frame(group,value)

# treemap treemap(data, index="group", vSize="value", type="index" )

Example 4 - Animated .gif with gganimate, ggplot2

---------------------------------------------------------------------------------------------------------------------------------------------------------------

# Get data: # install.packages("gapminder") library(gapminder) library(ggplot2) # install.packages('installr') # library(installr) # install.ImageMagick() # install.packages("htmltools") # devtools::install_github("dgrtwo/gganimate") # library("ImageMagick") library(gganimate) summary(gapminder) head(gapminder) # Make a ggplot, but add frame=year: one image per year p= ggplot(gapminder, aes( gdpPercap, lifeExp, size = pop, color = continent, frame = year)) + geom_point() + scale_size_continuous(range = c(3, 20))+ scale_x_log10() + # theme_bw() theme(axis.text=element_text(size=18), axis.title=element_text(size=18,face="bold")) # Make the animation!ç # magickPath <- shortPathName("C:\\Program Files\\ImageMagick-7.0.6-Q16\\magick.exe") # library(animation) # ani.options(convert=magickPath)

gganimate(p,ani.width=1200, ani.height=800)

Example 5 - Financial charts /Stock Market Charts

--------------------------------------------------------------------------------------------------------------------------------------------------------------- # install.packages("quantmod") library('quantmod') getSymbols("AAPL") chartSeries(AAPL, subset='last 3 months') addBBands()

Download code :

GitHub

References :

http://www.r-graph-gallery.com/


About Author

Dattatray Shinde have over 6+ years of experience in Software Design, Development & Maintenance of Web Based Applications; worked on Healthcare, Insurance, E-commerce and Learning Management System domains. Over 2.5 + years as Data Scientist worked mainly in predictive analytics, survey analytics, risk analytics platforms.

Featured Posts
Recent Posts
Search By Tags
No tags yet.
Connect

Contact Me

Mobile: +91 - 956 130 9595 

dattatrayshindeharipur@gmail.com

  • Google+ Social Icon
  • Facebook Social Icon
  • LinkedIn Social Icon
  • Twitter Social Icon

© 2023 by Dattatray Shinde