0% found this document useful (0 votes)
111 views

Create Elegant Data Visualisations Using The Grammar of Graphics - Ggplot2

ggplot2 is an R package for creating graphics based on the grammar of graphics. It allows users to declaratively specify data mappings, graphical primitives, scales, faceting, and coordinate systems. Users start by calling ggplot() and supplying data and an aesthetic mapping with aes(), then adding layers like geom_point() or geom_histogram(). ggplot2 has been stable for over 10 years and is used by hundreds of thousands to create millions of plots.

Uploaded by

RAMZI Azeddine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Create Elegant Data Visualisations Using The Grammar of Graphics - Ggplot2

ggplot2 is an R package for creating graphics based on the grammar of graphics. It allows users to declaratively specify data mappings, graphical primitives, scales, faceting, and coordinate systems. Users start by calling ggplot() and supplying data and an aesthetic mapping with aes(), then adding layers like geom_point() or geom_histogram(). ggplot2 has been stable for over 10 years and is used by hundreds of thousands to create millions of plots.

Uploaded by

RAMZI Azeddine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Overview

ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics
(http://amzn.to/2ef1eWp). You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical
primitives to use, and it takes care of the details.

Installation
# The easiest way to get ggplot2 is to install the whole tidyverse:
install.packages (https://www.rdocumentation.org/packages/utils/topics/install.packages)

# Alternatively, install just ggplot2:


install.packages (https://www.rdocumentation.org/packages/utils/topics/install.packages)

# Or the the development version from GitHub:


# install.packages("devtools")
devtools::install_github (https://www.rdocumentation.org/packages/devtools/topics/reexpo

Cheatsheet

(https://github.com/rstudio/cheatsheets/blob/master/data-visualization-2.1.pdf)

Usage
It’s hard to succinctly describe how ggplot2 works because it embodies a deep philosophy of visualisation.
However, in most cases you start with ggplot() (reference/ggplot.html) , supply a dataset and aesthetic
mapping (with aes() (reference/aes.html) ). You then add on layers (like geom_point()
(reference/geom_point.html) or geom_histogram() (reference/geom_histogram.html) ), scales

(like scale_colour_brewer() (reference/scale_brewer.html) ), faceting specifications (like


facet_wrap() (reference/facet_wrap.html) ) and coordinate systems (like coord_flip()

(reference/coord_flip.html) ).

library (https://www.rdocumentation.org/packages/base/topics/library)(ggplot2)

ggplot (reference/ggplot.html)(mpg, aes (reference/aes.html)(displ, hwy, colour = class)


geom_point (reference/geom_point.html)()

Lifecycle
lifecycle stable (https://www.tidyverse.org/lifecycle/#stable)

ggplot2 is now over 10 years old and is used by hundreds of thousands of people to make millions of plots. That
means, by-and-large, ggplot2 itself changes relatively little. When we do make changes, they will be generally to
add new functions or arguments rather than changing the behaviour of existing functions, and if we do make
changes to existing behaviour we will do them for compelling reasons.

If you are looking for innovation, look to ggplot2’s rich ecosystem of extensions. See a community maintained list
at http://www.ggplot2-exts.org/gallery/ (http://www.ggplot2-exts.org/gallery/).

Learning ggplot2
If you are new to ggplot2 you are better off starting with a systematic introduction, rather than trying to learn
from reading individual documentation pages. Currently, there are three good places to start:

1. The data visualisation (http://r4ds.had.co.nz/data-visualisation.html) and graphics for communication


(http://r4ds.had.co.nz/graphics-for-communication.html) chapters in R for data science
(http://r4ds.had.co.nz). R for data science is designed to give you a comprehensive introduction to the
tidyverse (http://tidyverse.org), and these two chapters will get you up to speed with the essentials of
ggplot2 as quickly as possible.

2. If you’d like to take an online course, try Data Visualization in R With ggplot2
(http://shop.oreilly.com/product/0636920052807.do) by Kara Woo.

3. If you want to dive into making common graphics as quickly as possible, I recommend The R Graphics
Cookbook (https://amzn.to/2TU78ip) by Winston Chang. It provides a set of recipes to solve common
graphics problems.

If you’ve mastered the basics and want to learn more, read ggplot2: Elegant Graphics for Data Analysis
(http://amzn.to/2fncG50). It describes the theoretical underpinnings of ggplot2 and shows you how all the pieces
fit together. This book helps you understand the theory that underpins ggplot2, and will help you create new
types of graphics specifically tailored to your needs. The book is not available for free, but you can find the
complete source for the book at https://github.com/hadley/ggplot2-book (https://github.com/hadley/ggplot2-
book).

Getting help
There are two main places to get help with ggplot2:

1. The RStudio community (https://community.rstudio.com/) is a friendly place to ask any questions about
ggplot2.

2. Stack Overflow (http://stackoverflow.com/questions/tagged/ggplot2?sort=frequent&pageSize=50) is a


great source of answers to common ggplot2 questions. It is also a great place to get help, once you have
created a reproducible example that illustrates your problem.
ggplot2 is a part of the tidyverse, an ecosystem of Developed by Hadley Wickham (http://hadley.nz),
packages designed with common APIs and a shared Winston Chang, Lionel Henry, Thomas Lin Pedersen,
philosophy. Learn more at tidyverse.org Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki
(https://tidyverse.org). Yutani. Site built by pkgdown (https://pkgdown.r-
lib.org).

You might also like