What is ggplot2 ?
ggplot2 is a powerful R package, implemented by Hadley Wickham, for producing nice graphs. The gg in ggplot2 means Grammar of Graphics, a graphic concept which describes plots by using a “grammar”. Plot can be divided in different fundamental parts : Plot <- data + Aesthetics + Geometry.
data are a data.frame Aesthetics is used to specify x and y variables. It can also be used to control the color, the size or the shape of a point, the height of a bar, etc….. Geometry corresponds to the type of graphs (histogram, box plot, line plot, density plot, dot plot, ….)
Read more —-> ggplot2 online documentation
Introduction to easyGgplot2 package
“ggplot2 is too complicated: it is hard for me to improve, and hard for new developers to understand.” — Hadley Wickham 2011 (http://vita.had.co.nz/papers/future-ig.pdf)
The idea behind ggplot2 is seductively simple but the detail is, yes, difficult. To customize a plot, the syntax is sometimes a tiny bit opaque and this raises the level of difficulty.
For these reasons, I created the easyGgplot2 package (which depends on ggplot2) to make and customize quickly plots including box plot, dot plot, strip chart, violin plot, histogram, density plot, scatter plot, bar plot, line plot, etc, ….
In this chapter, you will find many tutorials on ggplot2 plotting systems and on how to personalize quickly graphical parameters including title, axis labels, legend, background and colors.
The syntax used to call easyGgplot2 functions is simple and it’s a kind of an easy interface between users and ggplot2 package. An example to illustrate the difference between the syntax of ggplot2 and easyGgplot2 is shown hereafter.
Install and load easyGgplot2 package
To install the package, use the following R code from an R console (R >=3.1.0) :
install.packages("devtools") library(devtools) install_github("easyGgplot2", "kassambara")
Load the package using the R code:
Syntax of easyGgplot2 and ggplot2 packages
In the following R code, a box plot is drawn using either ggplot2 or easyGgplot2.
ggplot2 syntax :
ggplot(data=ToothGrowth, aes(x=dose, y=len, fill=dose))+ geom_boxplot()+ # Customize main title, x and y axis labels ggtitle("Plot of length \n by dose") + xlab("Dose (mg)")+ theme( # customize main title plot.title = element_text( size=14, face="bold.italic", colour="red", lineheight=1.0), # customize x and y axis labels axis.title.x=element_text(size=14, face="bold", colour="blue", hjust=0.4), axis.title.y=element_text(size=14, face="bold", colour="blue", vjust=0.4, angle=90))+ #change group colors scale_fill_manual(values=c('#999999','#E69F00','#56B4E9'))
easyGgplot2 syntax :
# Change main title and axis titles # Customize title styles. Possible values for the font style : # 'plain', 'italic', 'bold', 'bold.italic'. ggplot2.boxplot(data=ToothGrowth, xName='dose',yName='len', groupName="dose", groupColors=c('#999999','#E69F00','#56B4E9'), xtitle="Dose (mg)", ytitle="Length", mainTitle="Plot of length \n by dose", mainTitleFont=c(14,"bold.italic", "red"), xtitleFont=c(14,"bold", "blue"), ytitleFont=c(14,"bold", "blue"))
Note that the outputs of the two packages are the same, and easyGgplot2 is just a wrapper for ggplot2 package
Click on the following articles to visualize your data using easyGgplot2 R package.
Easy ggplot2 ebook
Note that an eBook is available on easyGgplot2 package here.
By Alboukadel Kassambara
Copyright 2014 Alboukadel Kassambara. All rights reserved.
Published by STHDA (http://www.sthda.com/english).
September 2014 : First edition.
Licence : This document is under creative commons licence (http://creativecommons.org/licenses/by-nc-sa/3.0/).
Contact : Alboukadel Kassambara email@example.com
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
- Course: Machine Learning: Master the Fundamentals by Standford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Books - Data Science
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Want to Learn More on R Programming and Data Science?
Follow us by Email On Social Networks:
Click to follow us on Facebook and Google+ :
Comment this article by clicking on "Discussion" button (top-right position of this page)
Articles contained by this category :
ggplot2 boxplot : Easy box and whisker plots maker function
ggplot2 density : Easy density plot using ggplot2 and R statistical software
ggplot2 dot plot : Easy function for making a dot plot
ggplot2 histogram : Easy histogram graph with ggplot2 R package
ggplot2 violin plot : Easy function for data visualization using ggplot2 and R software
ggplot2.barplot : Easy bar graphs in R software using ggplot2
ggplot2.customize : How to personalize easily ggplot2 graphs in R statistical software
ggplot2.lineplot : Easy line plots in R software using ggplot2
ggplot2.multiplot : Put multiple graphs on the same page using ggplot2
ggplot2.scatterplot : Easy scatter plot using ggplot2 and R statistical software
ggplot2.stripchart : Easy one dimensional scatter plot using ggplot2 and R software