Statistical Graphics with ggplot2
Statistical Graphics with ggplot2
Programming
statistical
graphics
Professor
Amany E. Aly Lecture 3: Programming statistical graphics
Programming
statistical
graphics
Simple high level
plots
Professor Amany E. Aly
Bar charts and
dot charts
Pie charts
Histograms
Department of Mathematics, Faculty of Science,
Boxplots Helwan University, Ain Helwan, Cairo, Egypt.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
14 / 10 / 2024
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 1 / 146
Presentation Outline
Lecture 3:
Programming
1 Programming statistical graphics
statistical
graphics Simple high level plots
Bar charts and dot charts
Professor
Amany E. Aly Pie charts
Histograms
Programming Boxplots
statistical
graphics Scatterplots
Simple high level
plots Plotting data from data frames
Bar charts and
dot charts
QQ plots
Pie charts
Histograms
Boxplots
Low level graphics functions
Scatterplots Adding to plots
Plotting data
from data
frames
QQ plots
Graphics as a language: ggplot2
Low level Details of the ggplot2 grammar
graphics
functions Layers in ggplot2
Adding to plots
Graphics as a
Setting colors
language:
ggplot2 Customizing the look of a graph
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 2 / 146
Programming statistical graphics
Lecture 3:
Programming
statistical
graphics
Professor The grid package provides the basis for a newer graphics sys-
Amany E. Aly
tem.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 3 / 146
Programming statistical graphics
Lecture 3:
Programming
statistical
graphics
Professor The grid package provides the basis for a newer graphics sys-
Amany E. Aly
tem.
Programming
statistical
graphics It also has facilities to produce good quality graphics, but the
Simple high level
plots
Bar charts and
programmer has access to the individual pieces of a graph, and
dot charts
Pie charts
Histograms
can modify them.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 3 / 146
Programming statistical graphics
Lecture 3:
Programming
statistical
graphics
Professor The grid package provides the basis for a newer graphics sys-
Amany E. Aly
tem.
Programming
statistical
graphics It also has facilities to produce good quality graphics, but the
Simple high level
plots
Bar charts and
programmer has access to the individual pieces of a graph, and
dot charts
Pie charts
Histograms
can modify them.
Boxplots
Scatterplots
Plotting data
A graph is more like a physical model being built and displayed,
from data
frames
QQ plots rather than just drawn.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 3 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The ggplot2 and lattice packages provide functions for high
Amany E. Aly
level plots based on grid graphics.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 4 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The ggplot2 and lattice packages provide functions for high
Amany E. Aly
level plots based on grid graphics.
Programming
statistical
graphics
In ggplot2 the code to draw a plot is an abstract description
Simple high level
plots
Bar charts and
of the intention of what to show in the plot, rather than how
dot charts
Pie charts
Histograms
to draw it.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 4 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The ggplot2 and lattice packages provide functions for high
Amany E. Aly
level plots based on grid graphics.
Programming
statistical
graphics
In ggplot2 the code to draw a plot is an abstract description
Simple high level
plots
Bar charts and
of the intention of what to show in the plot, rather than how
dot charts
Pie charts
Histograms
to draw it.
Boxplots
Scatterplots
Plotting data
The package translates that description into grid commands
from data
frames
QQ plots
when you ask to draw it.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 4 / 146
Simple high level plots
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
The functions to draw these in R are called ”high level” because
graphics
Simple high level
plots
you don’t need to worry about the details of where the ink goes;
Bar charts and
dot charts
Pie charts
you just describe the plot you want, and R does the drawing.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 5 / 146
Bar charts and dot charts
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The most basic type of graph is one that summarizes a single
statistical
graphics set of numbers.
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 6 / 146
Bar charts and dot charts
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The most basic type of graph is one that summarizes a single
statistical
graphics set of numbers.
Simple high level
plots
Bar charts and
dot charts
Bar charts and dot charts do this by displaying a bar or dot
Pie charts
Histograms
Boxplots
whose length or position corresponds to the number.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 6 / 146
Example 3.1
Lecture 3:
Programming
statistical
graphics
The W orldP hones matrix holds counts of the numbers of tele-
Professor
Amany E. Aly phones in the major regions of the world for a number of years.
Programming
statistical
The first row of the matrix corresponds to the year 1951. In order
graphics
Simple high level
plots
to display these data graphically, we first extract that row.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 7 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The default code to produce a bar chart of these data using the
Programming
statistical
graphics
barplot() function is
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 8 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 9 / 146
Lecture 3:
Programming Some minor changes are required in order for the plot to be sat-
statistical
graphics isfactory.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 10 / 146
Lecture 3:
Programming Some minor changes are required in order for the plot to be sat-
statistical
graphics isfactory.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The cex.names = 0.75 argument reduced the size of the
dot charts
Pie charts
Histograms
region names to 0.75 of their former size,
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 10 / 146
Lecture 3:
Programming Some minor changes are required in order for the plot to be sat-
statistical
graphics isfactory.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The cex.names = 0.75 argument reduced the size of the
dot charts
Pie charts
Histograms
region names to 0.75 of their former size,
Boxplots
Scatterplots The cex.axis = 0.75 argument reduced the labels on the
Plotting data
from data
frames
QQ plots
vertical axis by the same amount.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 10 / 146
Lecture 3:
Programming Some minor changes are required in order for the plot to be sat-
statistical
graphics isfactory.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The cex.names = 0.75 argument reduced the size of the
dot charts
Pie charts
Histograms
region names to 0.75 of their former size,
Boxplots
Scatterplots The cex.axis = 0.75 argument reduced the labels on the
Plotting data
from data
frames
QQ plots
vertical axis by the same amount.
Low level
graphics
functions
The main argument sets the main title for the plot, and the
Adding to plots
Graphics as a
language:
ylab and xlab arguments are used to include axis labels.
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 10 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly An alternative way to plot the same kind of data is in a dot chart
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
The values are shown by the horizontal positions of the dots.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 11 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 12 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Data sets having more complexity can also be displayed using
Programming
statistical these graphics functions.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 13 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Data sets having more complexity can also be displayed using
Programming
statistical these graphics functions.
graphics
Simple high level
plots The barplot() function has a number of options which allow
Bar charts and
dot charts
Pie charts for side-by-side or stacked styles of displays, legends can be
Histograms
Boxplots
Scatterplots
included using the legend argument, and so on.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 13 / 146
Example 3.2
Lecture 3:
Programming
statistical
graphics The V ADeaths data set in R contains death rates (number of
Professor
Amany E. Aly deaths per 1000 population per year) in various subpopulations
Programming within the state of Virginia in 1940.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 14 / 146
Lecture 3:
Programming This data set may be displayed as a sequence of bar charts, one
statistical
graphics for each subgroup
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 15 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 16 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The bars correspond to each number in the matrix.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 16 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The bars correspond to each number in the matrix.
statistical
graphics The beside = T RU E argument causes the values in each
Simple high level
plots
Bar charts and
column to be plotted side-by-side; legend = T RU E causes
dot charts
Pie charts
Histograms
the legend in the top right to be added.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 16 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The bars correspond to each number in the matrix.
statistical
graphics The beside = T RU E argument causes the values in each
Simple high level
plots
Bar charts and
column to be plotted side-by-side; legend = T RU E causes
dot charts
Pie charts
Histograms
the legend in the top right to be added.
Boxplots
Scatterplots
Plotting data
The ylim = c(0, 90) argument modifies the vertical scale of
from data
frames
QQ plots
the graph to make room for the legend.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 16 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The bars correspond to each number in the matrix.
statistical
graphics The beside = T RU E argument causes the values in each
Simple high level
plots
Bar charts and
column to be plotted side-by-side; legend = T RU E causes
dot charts
Pie charts
Histograms
the legend in the top right to be added.
Boxplots
Scatterplots
Plotting data
The ylim = c(0, 90) argument modifies the vertical scale of
from data
frames
QQ plots
the graph to make room for the legend.
Low level
graphics
functions
Finally, main = ”Death rates in V irginia” sets the main
Adding to plots
Graphics as a
language:
title for the plot.
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 16 / 146
Example 3.3
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The dot chart offers an alternative way of viewing the data.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 17 / 146
Lecture 3:
Programming
statistical
graphics We set the x-axis limits to run from 0 to 75 so that zero is
Professor
Amany E. Aly included, because it is natural to want to compare the total
Programming rates in the different groups.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 18 / 146
Lecture 3:
Programming
statistical
graphics We set the x-axis limits to run from 0 to 75 so that zero is
Professor
Amany E. Aly included, because it is natural to want to compare the total
Programming rates in the different groups.
statistical
graphics
Simple high level
plots
We have also set cex to 0.8. This shrinks the plotting char-
Bar charts and
dot charts
Pie charts
acter to 80% of its default size, but more importantly, shrinks
Histograms
Boxplots the axis tick labels to 80% of their default size.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 18 / 146
Lecture 3:
Programming
statistical
graphics We set the x-axis limits to run from 0 to 75 so that zero is
Professor
Amany E. Aly included, because it is natural to want to compare the total
Programming rates in the different groups.
statistical
graphics
Simple high level
plots
We have also set cex to 0.8. This shrinks the plotting char-
Bar charts and
dot charts
Pie charts
acter to 80% of its default size, but more importantly, shrinks
Histograms
Boxplots the axis tick labels to 80% of their default size.
Scatterplots
Plotting data
from data
frames
For this example, the default setting would cause some over-
QQ plots
Low level
graphics
lapping of the tick labels, making them more difficult to read.
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 18 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 19 / 146
Pie charts
Lecture 3:
Programming
statistical
graphics
Pie charts display a vector of numbers by breaking up a circular
Professor
Amany E. Aly
disk into pieces whose angle (and hence area) is proportional
Programming
statistical
to each number.
graphics
Simple high level
plots
For example, the letter grades assigned to a class might arise
Bar charts and
dot charts
Pie charts
in the proportions, A: 18%, B: 30%, C: 32%, D: 10%, and
Histograms
Boxplots
Scatterplots
F: 10%.
Plotting data
from data
frames These data are graphically displayed in Figure 3.5, which was
QQ plots
Low level
graphics
functions
drawn with the R code.
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 20 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 21 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms Pie charts are popular in non-technical publications, but they
Boxplots
Scatterplots
Plotting data
have fallen out of favor with statisticians.
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 22 / 146
Histograms
Lecture 3:
Programming
statistical A histogram is a special type of bar chart that is used to show
graphics
Professor
the frequency distribution of a collection of numbers.
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 23 / 146
Histograms
Lecture 3:
Programming
statistical A histogram is a special type of bar chart that is used to show
graphics
Professor
the frequency distribution of a collection of numbers.
Amany E. Aly
Each bar represents the count of x values that fall in the range
Programming
statistical indicated by the base of the bar.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 23 / 146
Histograms
Lecture 3:
Programming
statistical A histogram is a special type of bar chart that is used to show
graphics
Professor
the frequency distribution of a collection of numbers.
Amany E. Aly
Each bar represents the count of x values that fall in the range
Programming
statistical indicated by the base of the bar.
graphics
Simple high level
plots Usually all bars have the same width; this is the default in R.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 23 / 146
Histograms
Lecture 3:
Programming
statistical A histogram is a special type of bar chart that is used to show
graphics
Professor
the frequency distribution of a collection of numbers.
Amany E. Aly
Each bar represents the count of x values that fall in the range
Programming
statistical indicated by the base of the bar.
graphics
Simple high level
plots Usually all bars have the same width; this is the default in R.
Bar charts and
dot charts
Pie charts In this case, the height of each bar is proportional to the
Histograms
Boxplots
Scatterplots
number of observations in the corresponding interval.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 23 / 146
Histograms
Lecture 3:
Programming
statistical A histogram is a special type of bar chart that is used to show
graphics
Professor
the frequency distribution of a collection of numbers.
Amany E. Aly
Each bar represents the count of x values that fall in the range
Programming
statistical indicated by the base of the bar.
graphics
Simple high level
plots Usually all bars have the same width; this is the default in R.
Bar charts and
dot charts
Pie charts In this case, the height of each bar is proportional to the
Histograms
Boxplots
Scatterplots
number of observations in the corresponding interval.
Plotting data
from data
frames If bars have different widths, then the area of the bar should
QQ plots
Low level
graphics
functions
be proportional to the count; in this way the height represents
Adding to plots
Graphics as a
language:
the density (i.e. the frequency per unit of x).
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 23 / 146
Lecture 3:
Programming
statistical
In base graphics, hist(x, ...) is the main way to plot histogram-
graphics
s.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 24 / 146
Lecture 3:
Programming
statistical
In base graphics, hist(x, ...) is the main way to plot histogram-
graphics
s.
Professor
Amany E. Aly
Here x is a vector consisting of numeric observations, and
Programming
statistical optional parameters in ... are used to control the details of the
graphics
Simple high level
plots
display.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 24 / 146
Lecture 3:
Programming
statistical
In base graphics, hist(x, ...) is the main way to plot histogram-
graphics
s.
Professor
Amany E. Aly
Here x is a vector consisting of numeric observations, and
Programming
statistical optional parameters in ... are used to control the details of the
graphics
Simple high level
plots
display.
Bar charts and
dot charts
Pie charts
An example of a histogram (of the areas of the world’s largest
Histograms
Boxplots
Scatterplots
landmasses) drawn with the default parameter settings.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 24 / 146
Lecture 3:
Programming
statistical
In base graphics, hist(x, ...) is the main way to plot histogram-
graphics
s.
Professor
Amany E. Aly
Here x is a vector consisting of numeric observations, and
Programming
statistical optional parameters in ... are used to control the details of the
graphics
Simple high level
plots
display.
Bar charts and
dot charts
Pie charts
An example of a histogram (of the areas of the world’s largest
Histograms
Boxplots
Scatterplots
landmasses) drawn with the default parameter settings.
Plotting data
from data
frames The histogram bars decrease in height, roughly exponentially,
QQ plots
Low level
graphics
functions
in the direction of increasing land area (along the horizontal
Adding to plots
Graphics as a axis), together with better axis labeling and a title.
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 24 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 25 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 26 / 146
Boxplots
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
A boxplot (or ”box-and-whisker plot”) is an alternative to a
Programming
statistical histogram to give a quick visual display of the main features
graphics
Simple high level
plots of a set of data.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 27 / 146
Boxplots
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
A boxplot (or ”box-and-whisker plot”) is an alternative to a
Programming
statistical histogram to give a quick visual display of the main features
graphics
Simple high level
plots of a set of data.
Bar charts and
dot charts
Pie charts
Histograms
A rectangular box is drawn, together with lines which protrude
Boxplots
Scatterplots from two opposing sides.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 27 / 146
Lecture 3:
Programming
statistical
graphics
Professor The box gives an indication of the location and spread of the
Amany E. Aly
central portion of the data, while the extent of the lines (the
Programming
statistical
graphics
”whiskers”) provides an idea of the range of the bulk of the
Simple high level
plots data.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 28 / 146
Lecture 3:
Programming
statistical
graphics
Professor The box gives an indication of the location and spread of the
Amany E. Aly
central portion of the data, while the extent of the lines (the
Programming
statistical
graphics
”whiskers”) provides an idea of the range of the bulk of the
Simple high level
plots data.
Bar charts and
dot charts
Pie charts
Histograms
In some implementations, outliers (observations that are very
Boxplots
Scatterplots
Plotting data
different from the rest of the data) are plotted as separate
from data
frames
QQ plots
points.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 28 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The basic construction of the box part of the boxplot is as follows:
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 29 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The basic construction of the box part of the boxplot is as follows:
Amany E. Aly
Programming
statistical A horizontal line is drawn at the median.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 29 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The basic construction of the box part of the boxplot is as follows:
Amany E. Aly
Programming
statistical A horizontal line is drawn at the median.
graphics
Simple high level
plots
Bar charts and
Split the data into two halves, each containing the median.
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 29 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The basic construction of the box part of the boxplot is as follows:
Amany E. Aly
Programming
statistical A horizontal line is drawn at the median.
graphics
Simple high level
plots
Bar charts and
Split the data into two halves, each containing the median.
dot charts
Pie charts
Histograms
Calculate the upper and lower quartiles as the medians of each
Boxplots
Scatterplots
Plotting data
half, and draw horizontal lines at each of these values. Then
from data
frames
QQ plots
connect the lines to form a rectangular box.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 29 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly The box thus drawn defines the interquartile range (IQR). This
Programming is the difference between the upper quartile and the lower quar-
statistical
graphics
Simple high level
tile.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 30 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly The box thus drawn defines the interquartile range (IQR). This
Programming is the difference between the upper quartile and the lower quar-
statistical
graphics
Simple high level
tile.
plots
Bar charts and
dot charts We use the IQR to give a measure of the amount of variability
Pie charts
Histograms
Boxplots
in the central portion of the data set, since about 50% of the
Scatterplots
Plotting data
from data
data will lie within the box.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 30 / 146
Lecture 3:
Programming
statistical
The lower whisker is drawn from the lower end of the box to
graphics
Professor
the smallest value that is no smaller than 1.5 IQR below the
Amany E. Aly
lower quartile.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 31 / 146
Lecture 3:
Programming
statistical
The lower whisker is drawn from the lower end of the box to
graphics
Professor
the smallest value that is no smaller than 1.5 IQR below the
Amany E. Aly
lower quartile.
Programming
statistical
graphics
Similarly, the upper whisker is drawn from the middle of the
Simple high level
plots upper end of the box to the largest value that is no larger than
Bar charts and
dot charts
Pie charts 1.5 IQR above the upper quartile.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 31 / 146
Lecture 3:
Programming
statistical
The lower whisker is drawn from the lower end of the box to
graphics
Professor
the smallest value that is no smaller than 1.5 IQR below the
Amany E. Aly
lower quartile.
Programming
statistical
graphics
Similarly, the upper whisker is drawn from the middle of the
Simple high level
plots upper end of the box to the largest value that is no larger than
Bar charts and
dot charts
Pie charts 1.5 IQR above the upper quartile.
Histograms
Boxplots
Scatterplots The rationale for these definitions is that when data are drawn
Plotting data
from data
frames
QQ plots
from the normal distribution or other distributions with a sim-
Low level
graphics
functions
ilar shape, about 99% of the observations will fall between the
Adding to plots
Graphics as a
language:
whiskers.
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 31 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 32 / 146
Construction of a boxplot
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 33 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
An annotated example of a boxplot is displayed in previous
Programming
statistical Figure.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 34 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
An annotated example of a boxplot is displayed in previous
Programming
statistical Figure.
graphics
Simple high level
plots Boxplots are convenient for comparing distributions of data in
Bar charts and
dot charts
Pie charts two or more categories, with a number (say 10 or more) of
Histograms
Boxplots
Scatterplots
numerical observations per category.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 34 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
For example, the iris data set in R is a well-studied data set of
Simple high level
plots measurements of 50 flowers from each of three species of iris.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 35 / 146
Lecture 3:
Programming Compares the distributions of the sepal length measurements
statistical
graphics
between the different species.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 36 / 146
Lecture 3:
Programming Compares the distributions of the sepal length measurements
statistical
graphics
between the different species.
Professor
Amany E. Aly Here we have used R’s formula-based interface to the graph-
Programming
statistical
ics function: the syntax Sepal.Length ∼ Species is read as
graphics
Simple high level
”Sepal.Length depending on Species,” where both are columns
plots
Bar charts and
dot charts of the data frame specified by data = iris.
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 36 / 146
Lecture 3:
Programming Compares the distributions of the sepal length measurements
statistical
graphics
between the different species.
Professor
Amany E. Aly Here we have used R’s formula-based interface to the graph-
Programming
statistical
ics function: the syntax Sepal.Length ∼ Species is read as
graphics
Simple high level
”Sepal.Length depending on Species,” where both are columns
plots
Bar charts and
dot charts of the data frame specified by data = iris.
Pie charts
Histograms
Boxplots
The boxplot() function draws separate side-by-side boxplots
Scatterplots
Plotting data
from data
for each species.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 36 / 146
Lecture 3:
Programming Compares the distributions of the sepal length measurements
statistical
graphics
between the different species.
Professor
Amany E. Aly Here we have used R’s formula-based interface to the graph-
Programming
statistical
ics function: the syntax Sepal.Length ∼ Species is read as
graphics
Simple high level
”Sepal.Length depending on Species,” where both are columns
plots
Bar charts and
dot charts of the data frame specified by data = iris.
Pie charts
Histograms
Boxplots
The boxplot() function draws separate side-by-side boxplots
Scatterplots
Plotting data
from data
for each species.
frames
QQ plots
Low level
From these, we can see substantial differences between the
graphics
functions
Adding to plots
mean lengths for the species, and that there is one unusually
Graphics as a
language:
ggplot2 small specimen among the virginica samples.
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 36 / 146
side-by-side boxplots
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 37 / 146
Scatterplots
Lecture 3:
Programming
statistical
graphics
The most commonly used plots is the scatterplot, in which
Professor
Amany E. Aly
points (xi , yi ), i = 1, ..., n are drawn using dots or other sym-
Programming
bols.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 38 / 146
Scatterplots
Lecture 3:
Programming
statistical
graphics
The most commonly used plots is the scatterplot, in which
Professor
Amany E. Aly
points (xi , yi ), i = 1, ..., n are drawn using dots or other sym-
Programming
bols.
statistical
graphics These are drawn to show relationships between the xi and yi
Simple high level
plots
Bar charts and
dot charts
values.
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 38 / 146
Scatterplots
Lecture 3:
Programming
statistical
graphics
The most commonly used plots is the scatterplot, in which
Professor
Amany E. Aly
points (xi , yi ), i = 1, ..., n are drawn using dots or other sym-
Programming
bols.
statistical
graphics These are drawn to show relationships between the xi and yi
Simple high level
plots
Bar charts and
dot charts
values.
Pie charts
Histograms
Boxplots
In R, scatterplots (and many other kinds of plots) are drawn
Scatterplots
Plotting data
from data
using the plot() function.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 38 / 146
Scatterplots
Lecture 3:
Programming
statistical
graphics
The most commonly used plots is the scatterplot, in which
Professor
Amany E. Aly
points (xi , yi ), i = 1, ..., n are drawn using dots or other sym-
Programming
bols.
statistical
graphics These are drawn to show relationships between the xi and yi
Simple high level
plots
Bar charts and
dot charts
values.
Pie charts
Histograms
Boxplots
In R, scatterplots (and many other kinds of plots) are drawn
Scatterplots
Plotting data
from data
using the plot() function.
frames
QQ plots
Low level
Its basic usage is plot(x, y, ...) where x and y are numeric
graphics
functions
Adding to plots vectors of the same length holding the data to be plotted.
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 38 / 146
Lecture 3:
Programming
statistical
graphics
Professor
One important optional argument is type.
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 39 / 146
Lecture 3:
Programming
statistical
graphics
Professor
One important optional argument is type.
Amany E. Aly
The default is type = ”p”, which draws a scatterplot.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 39 / 146
Lecture 3:
Programming
statistical
graphics
Professor
One important optional argument is type.
Amany E. Aly
The default is type = ”p”, which draws a scatterplot.
Programming
statistical
graphics
Line plots (in which line segments join the (xi , yi ) points in
Simple high level
plots
Bar charts and
order from first to last) are drawn using type = ”l”.
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 39 / 146
Lecture 3:
Programming
statistical
graphics
Professor
One important optional argument is type.
Amany E. Aly
The default is type = ”p”, which draws a scatterplot.
Programming
statistical
graphics
Line plots (in which line segments join the (xi , yi ) points in
Simple high level
plots
Bar charts and
order from first to last) are drawn using type = ”l”.
dot charts
Pie charts
Histograms
Many other types are available, including type = ”n”, to draw
Boxplots
Scatterplots
Plotting data
nothing: this just sets up the frame around the plot, allowing
from data
frames
QQ plots
other functions to be used to draw in it.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 39 / 146
An example of a scatterplot.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 40 / 146
An example of a scatterplot.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
The main argument sets the main title for the plot.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 40 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 41 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Other possibilities you should try:
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 42 / 146
Plotting data from data frames
Example 3.4
Lecture 3:
Programming
statistical
graphics
Professor
The Orange data frame is in the datasets package installed with
Amany E. Aly
R. It consists of 35 observations on the age (in days since Decem-
Programming
statistical ber 31, 1968) and the corresponding circumference of five different
graphics
Simple high level
plots
orange trees, with identifiers
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 43 / 146
Lecture 3:
Programming
statistical Since Orange$T ree is a factor, we use as.character() to get the
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 45 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly This figure hides important information: the observations are
Programming not all from the same tree, and they are not all from different
statistical
graphics
Simple high level
trees; they are from five different trees, but we cannot tell
plots
Bar charts and
dot charts
which observations are from which tree.
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 46 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly This figure hides important information: the observations are
Programming not all from the same tree, and they are not all from different
statistical
graphics
Simple high level
trees; they are from five different trees, but we cannot tell
plots
Bar charts and
dot charts
which observations are from which tree.
Pie charts
Histograms
Boxplots
One way to remedy this problem is to use a different plotting
Scatterplots
Plotting data
from data
symbol for each tree.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 46 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The pch parameter controls the plotting character.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 47 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The pch parameter controls the plotting character.
Programming The default setting pch = 1 yields the open circular dot.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 47 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The pch parameter controls the plotting character.
Programming The default setting pch = 1 yields the open circular dot.
statistical
graphics
Simple high level
Other numerical values of this parameter will give different
plots
Bar charts and
dot charts plotting characters.
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 47 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The pch parameter controls the plotting character.
Programming The default setting pch = 1 yields the open circular dot.
statistical
graphics
Simple high level
Other numerical values of this parameter will give different
plots
Bar charts and
dot charts plotting characters.
Pie charts
Histograms
Boxplots We can also ask for different characters to be plotted; for
Scatterplots
Plotting data
from data
frames
example, pch = ”A” causes R to plot the character A.
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 47 / 146
A scatterplot of circumference versus age for five different orange
trees.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 48 / 146
Example 3.5
Lecture 3:
Programming
statistical
The following code can be used to identify the individual trees
graphics
(Figure 3.12).
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 49 / 146
Example 3.5
Lecture 3:
Programming
statistical
The following code can be used to identify the individual trees
graphics
(Figure 3.12).
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The cex parameter controls the size of the plotting character.
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 49 / 146
Example 3.5
Lecture 3:
Programming
statistical
The following code can be used to identify the individual trees
graphics
(Figure 3.12).
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The cex parameter controls the size of the plotting character.
dot charts
Pie charts
Histograms
The pch parameter has been assigned the levels of the T ree
Boxplots
Scatterplots
Plotting data
column.
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 49 / 146
Example 3.5
Lecture 3:
Programming
statistical
The following code can be used to identify the individual trees
graphics
(Figure 3.12).
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The cex parameter controls the size of the plotting character.
dot charts
Pie charts
Histograms
The pch parameter has been assigned the levels of the T ree
Boxplots
Scatterplots
Plotting data
column.
from data
frames
QQ plots
because T ree is a factor, care must be taken in order that the
Low level
graphics
functions
level values are used, and not the factor codes, hence the use
Adding to plots
Graphics as a
language:
of as.character().
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 49 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
There are many more optional arguments to the plot() function,
Simple high level
plots described on the ?plot and ?par help pages.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 50 / 146
QQ plots
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
Quantile-quantile plots ( QQ plots) are a type of scatterplot used
graphics
Simple high level
plots
to compare the distributions of two groups
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 51 / 146
QQ plots
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
Quantile-quantile plots ( QQ plots) are a type of scatterplot used
graphics
Simple high level
plots
to compare the distributions of two groups
Bar charts and
dot charts
Pie charts
or to compare a sample with a reference distribution.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 51 / 146
Lecture 3:
Programming
statistical
graphics
In the case where there are two groups of equal size, the QQ
Professor plot is obtained by first sorting the observations in each group:
Amany E. Aly
X[1] 6 ... 6 X[n] and Y [1] 6 ... 6 Y [n].
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 52 / 146
Lecture 3:
Programming
statistical
graphics
In the case where there are two groups of equal size, the QQ
Professor plot is obtained by first sorting the observations in each group:
Amany E. Aly
X[1] 6 ... 6 X[n] and Y [1] 6 ... 6 Y [n].
Programming
statistical
graphics Next, draw a scatterplot of (X[i], Y [i]), for i = 1, ..., n.
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 52 / 146
Lecture 3:
Programming
statistical
graphics
In the case where there are two groups of equal size, the QQ
Professor plot is obtained by first sorting the observations in each group:
Amany E. Aly
X[1] 6 ... 6 X[n] and Y [1] 6 ... 6 Y [n].
Programming
statistical
graphics Next, draw a scatterplot of (X[i], Y [i]), for i = 1, ..., n.
Simple high level
plots
Bar charts and
dot charts
When the groups are of different sizes, some scheme must be
Pie charts
Histograms used to artificially match them.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 52 / 146
Lecture 3:
Programming
statistical
graphics
In the case where there are two groups of equal size, the QQ
Professor plot is obtained by first sorting the observations in each group:
Amany E. Aly
X[1] 6 ... 6 X[n] and Y [1] 6 ... 6 Y [n].
Programming
statistical
graphics Next, draw a scatterplot of (X[i], Y [i]), for i = 1, ..., n.
Simple high level
plots
Bar charts and
dot charts
When the groups are of different sizes, some scheme must be
Pie charts
Histograms used to artificially match them.
Boxplots
Scatterplots
Plotting data
from data
R reduces the size of the larger group to the size of the small-
frames
QQ plots
Low level
er one by keeping the minimum and maximum values, and
graphics
functions
Adding to plots
choosing equally spaced quantiles between.
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 52 / 146
Lecture 3:
Programming Example: if there were five X values but 20 Y values, then
statistical
graphics
the X values would be plotted against the minimum, lower
Professor
Amany E. Aly quartile, median, upper quartile and maximum of the Y values.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 53 / 146
Lecture 3:
Programming Example: if there were five X values but 20 Y values, then
statistical
graphics
the X values would be plotted against the minimum, lower
Professor
Amany E. Aly quartile, median, upper quartile and maximum of the Y values.
Programming
statistical
When plotting a single sample against a reference distribution,
graphics
Simple high level
theoretical quantiles are used for one coordinate.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 53 / 146
Lecture 3:
Programming Example: if there were five X values but 20 Y values, then
statistical
graphics
the X values would be plotted against the minimum, lower
Professor
Amany E. Aly quartile, median, upper quartile and maximum of the Y values.
Programming
statistical
When plotting a single sample against a reference distribution,
graphics
Simple high level
theoretical quantiles are used for one coordinate.
plots
Bar charts and
dot charts R normally puts the theoretical quantiles on the x-axis and
Pie charts
Histograms
Boxplots
the data on the y-axis, but some authors make the opposite
Scatterplots
Plotting data
from data
choice.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 53 / 146
Lecture 3:
Programming Example: if there were five X values but 20 Y values, then
statistical
graphics
the X values would be plotted against the minimum, lower
Professor
Amany E. Aly quartile, median, upper quartile and maximum of the Y values.
Programming
statistical
When plotting a single sample against a reference distribution,
graphics
Simple high level
theoretical quantiles are used for one coordinate.
plots
Bar charts and
dot charts R normally puts the theoretical quantiles on the x-axis and
Pie charts
Histograms
Boxplots
the data on the y-axis, but some authors make the opposite
Scatterplots
Plotting data
from data
choice.
frames
QQ plots
Low level
To avoid biases, quantiles are chosen corresponding to prob-
graphics
functions
Adding to plots
abilities (i − 1/2)/n: these are centered evenly between zero
Graphics as a
language:
ggplot2 and one.
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 53 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
When the distributions of X and Y match, the points in the
Programming
QQ plot will lie near the line y = x.
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 54 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
When the distributions of X and Y match, the points in the
Programming
QQ plot will lie near the line y = x.
statistical
graphics
Simple high level
We will see a different straight line if one distribution is a linear
plots
Bar charts and
dot charts
transformation of the other.
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 54 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
When the distributions of X and Y match, the points in the
Programming
QQ plot will lie near the line y = x.
statistical
graphics
Simple high level
We will see a different straight line if one distribution is a linear
plots
Bar charts and
dot charts
transformation of the other.
Pie charts
Histograms
Boxplots
On the other hand, if the two distributions are not the same,
Scatterplots
Plotting data
from data
we will see systematic patterns in the QQ plot.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 54 / 146
Several examples of QQ plots.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 55 / 146
Example 3.6
Lecture 3:
Programming
statistical
The following code illustrates some common patterns see the pre-
graphics
vious figure, using simulated data.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 56 / 146
Lecture 3:
Programming The mf row parameter of the par() function is giving a 1 × 4
statistical
graphics layout.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 57 / 146
Lecture 3:
Programming The mf row parameter of the par() function is giving a 1 × 4
statistical
graphics layout.
Professor
Amany E. Aly The first plot is based on identical normal distributions.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 57 / 146
Lecture 3:
Programming The mf row parameter of the par() function is giving a 1 × 4
statistical
graphics layout.
Professor
Amany E. Aly The first plot is based on identical normal distributions.
Programming The second plot is based on normal distributions having dif-
statistical
graphics
Simple high level
ferent means and standard deviations.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 57 / 146
Lecture 3:
Programming The mf row parameter of the par() function is giving a 1 × 4
statistical
graphics layout.
Professor
Amany E. Aly The first plot is based on identical normal distributions.
Programming The second plot is based on normal distributions having dif-
statistical
graphics
Simple high level
ferent means and standard deviations.
plots
Bar charts and
dot charts
The third plot is based on a standard normal and a t distribu-
Pie charts
Histograms
Boxplots
tion on 2 degrees of freedom.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 57 / 146
Lecture 3:
Programming The mf row parameter of the par() function is giving a 1 × 4
statistical
graphics layout.
Professor
Amany E. Aly The first plot is based on identical normal distributions.
Programming The second plot is based on normal distributions having dif-
statistical
graphics
Simple high level
ferent means and standard deviations.
plots
Bar charts and
dot charts
The third plot is based on a standard normal and a t distribu-
Pie charts
Histograms
Boxplots
tion on 2 degrees of freedom.
Scatterplots
Plotting data
from data
The fourth plot is based on a standard normal compared with
frames
QQ plots
Low level
an exponential distribution.
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 57 / 146
Lecture 3:
Programming The mf row parameter of the par() function is giving a 1 × 4
statistical
graphics layout.
Professor
Amany E. Aly The first plot is based on identical normal distributions.
Programming The second plot is based on normal distributions having dif-
statistical
graphics
Simple high level
ferent means and standard deviations.
plots
Bar charts and
dot charts
The third plot is based on a standard normal and a t distribu-
Pie charts
Histograms
Boxplots
tion on 2 degrees of freedom.
Scatterplots
Plotting data
from data
The fourth plot is based on a standard normal compared with
frames
QQ plots
Low level
an exponential distribution.
graphics
functions
Adding to plots
Since we used simulated random numbers here, you’ll likely
Graphics as a
language:
ggplot2
see slightly different results if you run the same code.
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 57 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
Solve Exercises from 1 to 4 page 66 and 67.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 58 / 146
Low level graphics functions
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
Functions like barplot(), dotchart(), and plot() do their work by
graphics
Simple high level
plots
using low level graphics functions to draw lines and points, to
Bar charts and
dot charts
Pie charts
establish where they will be placed on a page, and so on.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 59 / 146
Adding to plots
Lecture 3:
Programming
statistical
Several functions exist to add components to existing graphs:
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions The optional arguments to these functions specify the color, size,
Adding to plots
Graphics as a
language:
ggplot2
and other characteristics of the items being added.
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 60 / 146
Example 3.7
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
Consider the Orange data frame again. In addition to using dif-
statistical
graphics ferent plotting characters for the different trees, we will pass lines
Simple high level
plots
Bar charts and
dot charts
of best fit (i.e. least-squares regression lines) through the points
Pie charts
Histograms corresponding to each tree.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 61 / 146
Lecture 3:
Programming
statistical
graphics
The basic scatterplot is obtained from
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts The best-fit lines for the five trees can be obtained using the lm()
Pie charts
Histograms
Boxplots
function which relates circumference to age for each tree.
Scatterplots
Plotting data
from data
A legend has been added to identify which data points come from
frames
QQ plots
Low level
the different trees.
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 62 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 63 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
In these plots lty gives the line type, and lwd gives the line width.
Programming
statistical The left panel of next figure shows the resulting lines. The right
graphics
Simple high level
plots
panel shows an alternative way of displaying the data, using the
Bar charts and
dot charts
Pie charts
lines() function and without obtaining the least-squares regres-
Histograms
Boxplots
Scatterplots
sion lines.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 64 / 146
A scatterplot of circumference versus age for five orange trees,
with overlaid trend lines, using different line types for each tree.
Left panel: best-fit straight lines have been obtained using
Lecture 3:
Programming least-squares; right panel: broken lines are used to interpolate the
statistical
graphics data.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 65 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
Solve Exercises from 1 to 5 on page 70.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 66 / 146
Graphics as a language: ggplot2
Lecture 3:
Programming
statistical The ggplot2 package by Hadley Wickham is based on a ”gram-
graphics
Professor
mar of graphics”: the idea that statistical graphics is a lan-
Amany E. Aly
guage that conveys meaning; the words of this language are
Programming
statistical charts and components of the charts.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 67 / 146
Graphics as a language: ggplot2
Lecture 3:
Programming
statistical The ggplot2 package by Hadley Wickham is based on a ”gram-
graphics
Professor
mar of graphics”: the idea that statistical graphics is a lan-
Amany E. Aly
guage that conveys meaning; the words of this language are
Programming
statistical charts and components of the charts.
graphics
Simple high level
plots Rather than describing the ink that goes on the page, the code
Bar charts and
dot charts
Pie charts describes the message you want to send.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 67 / 146
Graphics as a language: ggplot2
Lecture 3:
Programming
statistical The ggplot2 package by Hadley Wickham is based on a ”gram-
graphics
Professor
mar of graphics”: the idea that statistical graphics is a lan-
Amany E. Aly
guage that conveys meaning; the words of this language are
Programming
statistical charts and components of the charts.
graphics
Simple high level
plots Rather than describing the ink that goes on the page, the code
Bar charts and
dot charts
Pie charts describes the message you want to send.
Histograms
Boxplots
Scatterplots The ggplot2 package gives a somewhat abstract but very rich
Plotting data
from data
frames way to describe graphics. The next example show how to re-
QQ plots
Low level
graphics
functions
draw the bar chart of the world telephones of Example 3.1,
Adding to plots
Graphics as a
language:
adding more detail as we proceed.
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 67 / 146
Example 3.8
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly To plot the world phone data, we would write
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 68 / 146
Lecture 3:
Programming
statistical
The first lines of this snippet are needed to load the plotting
graphics
package and to prepare a data frame consisting of the tele-
Professor
Amany E. Aly
phone counts that correspond to the various world regions.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 69 / 146
Lecture 3:
Programming
statistical
The first lines of this snippet are needed to load the plotting
graphics
package and to prepare a data frame consisting of the tele-
Professor
Amany E. Aly
phone counts that correspond to the various world regions.
Programming
statistical The new feature is in the ggplot invocation where aes says
graphics
Simple high level
plots
that we want the Region names on the x-axis in their original
Bar charts and
dot charts
Pie charts
order, and the telephone counts on the y-axis.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 69 / 146
Lecture 3:
Programming
statistical
The first lines of this snippet are needed to load the plotting
graphics
package and to prepare a data frame consisting of the tele-
Professor
Amany E. Aly
phone counts that correspond to the various world regions.
Programming
statistical The new feature is in the ggplot invocation where aes says
graphics
Simple high level
plots
that we want the Region names on the x-axis in their original
Bar charts and
dot charts
Pie charts
order, and the telephone counts on the y-axis.
Histograms
Boxplots
Scatterplots
We want to display the data using bars, hence the use of the
Plotting data
from data
frames
geom col function.
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 69 / 146
Lecture 3:
Programming
statistical
The first lines of this snippet are needed to load the plotting
graphics
package and to prepare a data frame consisting of the tele-
Professor
Amany E. Aly
phone counts that correspond to the various world regions.
Programming
statistical The new feature is in the ggplot invocation where aes says
graphics
Simple high level
plots
that we want the Region names on the x-axis in their original
Bar charts and
dot charts
Pie charts
order, and the telephone counts on the y-axis.
Histograms
Boxplots
Scatterplots
We want to display the data using bars, hence the use of the
Plotting data
from data
frames
geom col function.
QQ plots
Low level
graphics
functions
The statistic to display is the identity, i.e. just the value itself.
Adding to plots
Graphics as a
language:
The result is shown in the next figure.
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 69 / 146
Re-doing Figure 3.1 using ggplot2.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 70 / 146
Lecture 3:
Programming
statistical
The general idea in ggplot2 is that plots are described by a
graphics
sum of objects produced by function calls.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 71 / 146
Lecture 3:
Programming
statistical
The general idea in ggplot2 is that plots are described by a
graphics
sum of objects produced by function calls.
Professor
Amany E. Aly
As with any addition in R, we use +, but you should think
Programming
statistical of the whole expression as a way to describe the plot as a
graphics
Simple high level
plots
combination of different components.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 71 / 146
Lecture 3:
Programming
statistical
The general idea in ggplot2 is that plots are described by a
graphics
sum of objects produced by function calls.
Professor
Amany E. Aly
As with any addition in R, we use +, but you should think
Programming
statistical of the whole expression as a way to describe the plot as a
graphics
Simple high level
plots
combination of different components.
Bar charts and
dot charts
Pie charts
The main components of a plot are shown in Table 3.1.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 71 / 146
Lecture 3:
Programming
statistical
The general idea in ggplot2 is that plots are described by a
graphics
sum of objects produced by function calls.
Professor
Amany E. Aly
As with any addition in R, we use +, but you should think
Programming
statistical of the whole expression as a way to describe the plot as a
graphics
Simple high level
plots
combination of different components.
Bar charts and
dot charts
Pie charts
The main components of a plot are shown in Table 3.1.
Histograms
Boxplots
Scatterplots
Most ggplot2 plot expressions start with a call to the ggplot()
Plotting data
from data
frames function.
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 71 / 146
Lecture 3:
Programming
statistical
The general idea in ggplot2 is that plots are described by a
graphics
sum of objects produced by function calls.
Professor
Amany E. Aly
As with any addition in R, we use +, but you should think
Programming
statistical of the whole expression as a way to describe the plot as a
graphics
Simple high level
plots
combination of different components.
Bar charts and
dot charts
Pie charts
The main components of a plot are shown in Table 3.1.
Histograms
Boxplots
Scatterplots
Most ggplot2 plot expressions start with a call to the ggplot()
Plotting data
from data
frames function.
QQ plots
Low level
graphics
functions
Its first argument is data, and that’s where we specify the data
Adding to plots
Graphics as a
language:
component of the plot, which is always a data frame or tibble.
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 71 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 72 / 146
Details of the ggplot2 grammar
Lecture 3:
Programming
statistical
graphics
ggplot2 plots are usually created as a sum of function calls.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 73 / 146
Details of the ggplot2 grammar
Lecture 3:
Programming
statistical
graphics
ggplot2 plots are usually created as a sum of function calls.
Professor
Amany E. Aly
Each of those function calls produces a special object, which
Programming
statistical
the ggplot2 code knows how to combine, provided you follow
graphics
Simple high level
plots
certain rules.
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 73 / 146
Details of the ggplot2 grammar
Lecture 3:
Programming
statistical
graphics
ggplot2 plots are usually created as a sum of function calls.
Professor
Amany E. Aly
Each of those function calls produces a special object, which
Programming
statistical
the ggplot2 code knows how to combine, provided you follow
graphics
Simple high level
plots
certain rules.
Bar charts and
dot charts
Pie charts
First, you need to start with a ”ggplot” object.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 73 / 146
Details of the ggplot2 grammar
Lecture 3:
Programming
statistical
graphics
ggplot2 plots are usually created as a sum of function calls.
Professor
Amany E. Aly
Each of those function calls produces a special object, which
Programming
statistical
the ggplot2 code knows how to combine, provided you follow
graphics
Simple high level
plots
certain rules.
Bar charts and
dot charts
Pie charts
First, you need to start with a ”ggplot” object.
Histograms
Boxplots
Scatterplots This can be produced by a call to ggplot() or to some other
Plotting data
from data
frames
QQ plots
function that calls it, and it can be saved in a variable and
Low level
graphics
functions
used later in a different plot.
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 73 / 146
Lecture 3:
Programming The ”ggplot” object sets certain defaults which can be used
statistical
graphics
by the layers of the plot.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 74 / 146
Lecture 3:
Programming The ”ggplot” object sets certain defaults which can be used
statistical
graphics
by the layers of the plot.
Professor
Amany E. Aly Normally the first argument specifies a data frame or tibble,
Programming
statistical
and that data can be used in all layers of the plot.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 74 / 146
Lecture 3:
Programming The ”ggplot” object sets certain defaults which can be used
statistical
graphics
by the layers of the plot.
Professor
Amany E. Aly Normally the first argument specifies a data frame or tibble,
Programming
statistical
and that data can be used in all layers of the plot.
graphics
Simple high level
It’s also common to specify aesthetics as the second argument,
plots
Bar charts and
dot charts and again, these will be used everywhere unless overridden by
Pie charts
Histograms
Boxplots
other settings.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 74 / 146
Lecture 3:
Programming The ”ggplot” object sets certain defaults which can be used
statistical
graphics
by the layers of the plot.
Professor
Amany E. Aly Normally the first argument specifies a data frame or tibble,
Programming
statistical
and that data can be used in all layers of the plot.
graphics
Simple high level
It’s also common to specify aesthetics as the second argument,
plots
Bar charts and
dot charts and again, these will be used everywhere unless overridden by
Pie charts
Histograms
Boxplots
other settings.
Scatterplots
Plotting data
from data
Thus a first component to a ggplot2 plot could be produced
frames
QQ plots
Low level
as
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 74 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Because we assigned the result to g1, it is not printed, and no
Programming
statistical
graphics
graph is displayed. To display it, we can print that object:
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 75 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 76 / 146
A call to ggplot() with no layers added.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 77 / 146
Example
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
To display a pie chart in ggplot2, you display a bar plot in polar
Programming
statistical coordinates:
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 78 / 146
Pie chart drawn by ggplot2.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 79 / 146
Layers in ggplot2
Lecture 3:
Programming
statistical
There are many ways to display data, and ggplot2 puts ”ways to
graphics
display data” into the geom? () layer functions.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 80 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Professor
Amany E. Aly
Professor
Amany E. Aly
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
linetype
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
linetype
Bar charts and
dot charts
Pie charts
size
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
linetype
Bar charts and
dot charts
Pie charts
size
Histograms
Boxplots
Scatterplots
fill: the fill color
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
linetype
Bar charts and
dot charts
Pie charts
size
Histograms
Boxplots
Scatterplots
fill: the fill color
Plotting data
from data
frames weight: the statistical weight to give to each observation
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
linetype
Bar charts and
dot charts
Pie charts
size
Histograms
Boxplots
Scatterplots
fill: the fill color
Plotting data
from data
frames weight: the statistical weight to give to each observation
QQ plots
Low level
graphics
functions
shape: the shape of point to plot
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Lecture 3:
Programming Others that are commonly supported are
statistical
graphics
alpha: transparency
Professor
Amany E. Aly color (or colour)
Programming
statistical
group: group
graphics
Simple high level
plots
linetype
Bar charts and
dot charts
Pie charts
size
Histograms
Boxplots
Scatterplots
fill: the fill color
Plotting data
from data
frames weight: the statistical weight to give to each observation
QQ plots
Low level
graphics
functions
shape: the shape of point to plot
Adding to plots
Graphics as a
language:
stroke: the thickness of parts of points being plotted
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 82 / 146
Example 3.9
Lecture 3:
Programming
statistical
graphics
Professor
We produced the bar plot in Figure 3.15 using the layer function
Amany E. Aly
geom col(). If we had used geom point() or geom line() instead,
Programming
statistical we would see points or lines respectively. In fact, Figure 3.18 shows
graphics
Simple high level
plots
how we can use all three:
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 83 / 146
Lecture 3:
Programming
statistical
The ggplot() call sets up the scales and coordinate system,
graphics
and sets the default theme.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 84 / 146
Lecture 3:
Programming
statistical
The ggplot() call sets up the scales and coordinate system,
graphics
and sets the default theme.
Professor
Amany E. Aly The geom col() call draws the bars in the plot as before.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 84 / 146
Lecture 3:
Programming
statistical
The ggplot() call sets up the scales and coordinate system,
graphics
and sets the default theme.
Professor
Amany E. Aly The geom col() call draws the bars in the plot as before.
Programming
statistical
geom point(col = ”red”) draws points in red.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 84 / 146
Lecture 3:
Programming
statistical
The ggplot() call sets up the scales and coordinate system,
graphics
and sets the default theme.
Professor
Amany E. Aly The geom col() call draws the bars in the plot as before.
Programming
statistical
geom point(col = ”red”) draws points in red.
graphics
Simple high level
plots
The middle layer
Bar charts and
dot charts
Pie charts geom line(col = ”blue”, aes(x = as.numeric(Region)))
Histograms
Boxplots
Scatterplots
Plotting data
draws the blue line.
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 84 / 146
Lecture 3:
Programming
statistical
The ggplot() call sets up the scales and coordinate system,
graphics
and sets the default theme.
Professor
Amany E. Aly The geom col() call draws the bars in the plot as before.
Programming
statistical
geom point(col = ”red”) draws points in red.
graphics
Simple high level
plots
The middle layer
Bar charts and
dot charts
Pie charts geom line(col = ”blue”, aes(x = as.numeric(Region)))
Histograms
Boxplots
Scatterplots
Plotting data
draws the blue line.
from data
frames
QQ plots
The x aesthetic was replaced by a numeric version, because
Low level
graphics
functions Region is a factor, and by default ggplot2 won’t join lines
Adding to plots
Graphics as a
language:
ggplot2
across its levels.
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 84 / 146
Lecture 3:
Programming The layers are drawn in the order specified, so we can see the
statistical
graphics
lines on top of the bars, and the points on top of the lines.
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 85 / 146
Lecture 3:
Programming The layers are drawn in the order specified, so we can see the
statistical
graphics
lines on top of the bars, and the points on top of the lines.
Professor
Amany E. Aly To gain a clearer view of the lines and points, we might in-
Programming
statistical
crease the transparency of the bars, by replacing the line of
graphics
Simple high level
code
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames with
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 85 / 146
Plot of world phone usage using geomc ol(), geomp oint(), and
geoml ine().
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 86 / 146
Example 3.10
Lecture 3:
Programming
statistical
graphics In Figure 3.8 we showed boxplots of the iris data using base graph-
Professor
Amany E. Aly ics. A similar plot could be produced in ggplot2 with this code:
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 87 / 146
Example 3.10
Lecture 3:
Programming
statistical
graphics In Figure 3.8 we showed boxplots of the iris data using base graph-
Professor
Amany E. Aly ics. A similar plot could be produced in ggplot2 with this code:
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Another view of the same data (Figure 3.19) using violin plots is
Histograms
Boxplots
Scatterplots
produced by the similar code:
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 87 / 146
Violin plot of the iris data.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 88 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
In a violin plot, a rough estimate of the PDF is used to obtain
Programming
statistical the width of the plot. This gives somewhat more information
graphics
Simple high level
plots
about the distribution than we saw in the boxplot: for example,
Bar charts and
dot charts
Pie charts
we can see that setosa appears to have a high concentration of
Histograms
Boxplots
Scatterplots
sepal lengths very close to 5 cm.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 89 / 146
Setting colors
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly There are several different ways to identify colors in R. They can be
Programming specified by name; the function colors() lists hundreds of names
statistical
graphics
Simple high level
recognized by R:
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 90 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
They can also be constructed using hexadecimal (base 16) codes
Programming
statistical for the levels of red, green, and blue. For example, red would be
graphics
Simple high level
plots
specified as ”#FF0000”, where FF, the base 16 representation of
Bar charts and
dot charts
Pie charts
255, is the maximum level of red, and both green and blue have
Histograms
Boxplots
Scatterplots
zero contribution.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 91 / 146
Lecture 3:
Programming R also maintains a palette of a small number of colors that can
statistical
graphics be referenced by number. Since version 4.0.0, there have been
Professor
Amany E. Aly several choices of palettes, choosable by name:
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 92 / 146
Lecture 3:
Programming
statistical
graphics
Professor
With this choice of colors (the same as in previous versions of R),
Amany E. Aly
we see
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
For example, with this palette active, the color ”red” would be
from data
frames
QQ plots
selected by specifying color number 2.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 93 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
In ggplot2, colors can be used in two ways: they can be specified
explicitly, or color can be used as an aesthetic. Figure 3.18 is an
Programming
statistical
graphics
example of specifying a color explicitly. We used to choose red
Simple high level
plots
Bar charts and
points.
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 94 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Any of the standard R specifications for red would have worked
Programming
statistical equally well: ”red”, ”#F F 0000”, or, assuming we are using the
graphics
Simple high level
plots
”R3” palette, the number 2. For the bars in a geom col() layer,
Bar charts and
dot charts
Pie charts
col controls the outline color, and argument f ill controls the fill
Histograms
Boxplots
Scatterplots
color.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 95 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The second way to specify color in ggplot2 is to use the col or
statistical
graphics f ill aesthetic. In this case, determining the final result is more
Simple high level
plots
Bar charts and
complicated, as it depends on the type of variable mapped to the
dot charts
Pie charts
Histograms
aesthetic.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 96 / 146
Lecture 3:
Programming
statistical
graphics
Professor
For a discrete variable like phones51$Region, ggplot2 will con-
Amany E. Aly
struct a palette of values and map each level of the variable
Programming
statistical to a different color.
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 97 / 146
Lecture 3:
Programming
statistical
graphics
Professor
For a discrete variable like phones51$Region, ggplot2 will con-
Amany E. Aly
struct a palette of values and map each level of the variable
Programming
statistical to a different color.
graphics
Simple high level
plots
Bar charts and
By default, the palette is a set of colors evenly spaced around
dot charts
Pie charts
Histograms
the color wheel.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 97 / 146
Lecture 3:
Programming
statistical
graphics
Professor
For a discrete variable like phones51$Region, ggplot2 will con-
Amany E. Aly
struct a palette of values and map each level of the variable
Programming
statistical to a different color.
graphics
Simple high level
plots
Bar charts and
By default, the palette is a set of colors evenly spaced around
dot charts
Pie charts
Histograms
the color wheel.
Boxplots
Scatterplots
Plotting data
If you don’t want to use that palette (perhaps because it isn’t
from data
frames
QQ plots
a good choice for color-blind viewers), you can choose another.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 97 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
For example, to use one of the palettes discussed in Section 3.2,
Programming
statistical
use scale f ill brewer():
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 98 / 146
Plot of world phone usage using the RColorBrewerSet2 palette
for Region.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 99 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly When the mapped variable is continuous, ggplot2 will default
Programming to a gradient scale from light blue to dark blue, produced by the
statistical
graphics
Simple high level
scale f ill gradient() function. For example,
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 100 / 146
Plot of world phone usage using the gradient palette for
T elephones.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 101 / 146
Customizing the look of a graph
Lecture 3:
Programming
statistical
graphics
Professor There are several functions to change the labeling on the graph.
Amany E. Aly
The ggtitle() function sets a title at the top
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 102 / 146
Customizing the look of a graph
Lecture 3:
Programming
statistical
graphics
Professor There are several functions to change the labeling on the graph.
Amany E. Aly
The ggtitle() function sets a title at the top
Programming
statistical
graphics xlab() and ylab() set titles on the axes.
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 102 / 146
Customizing the look of a graph
Lecture 3:
Programming
statistical
graphics
Professor There are several functions to change the labeling on the graph.
Amany E. Aly
The ggtitle() function sets a title at the top
Programming
statistical
graphics xlab() and ylab() set titles on the axes.
Simple high level
plots
Bar charts and
dot charts
These are all just convenient ”wrappers” for the labs() function,
Pie charts
Histograms
Boxplots
which can set labels in all of these places, as well as on any legends
Scatterplots
Plotting data
from data
shown in the graph if you assign the label to the corresponding
frames
QQ plots
Low level
aesthetic name.
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 102 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The theme() and theme ? () functions can be used to change
Amany E. Aly
many details of the overall look of a graph.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 103 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The theme() and theme ? () functions can be used to change
Amany E. Aly
many details of the overall look of a graph.
Programming
statistical
graphics
The scale ? () functions can be used to customize the mapping
Simple high level
plots
Bar charts and
for each aesthetic.
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 103 / 146
Lecture 3:
Programming
statistical
graphics
Professor
The theme() and theme ? () functions can be used to change
Amany E. Aly
many details of the overall look of a graph.
Programming
statistical
graphics
The scale ? () functions can be used to customize the mapping
Simple high level
plots
Bar charts and
for each aesthetic.
dot charts
Pie charts
Histograms
The annotate() function is interesting. It works like a layer
Boxplots
Scatterplots
Plotting data
function, but with fixed vectors of aesthetics, not values taken
from data
frames
QQ plots
from the data set for the plot.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 103 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
For example, to add some text to a plot at location x = 1, y = 2,
Programming
statistical one could use annotate (”text”, x = 1, y = 2, text = ”Label”).
graphics
Simple high level
plots
The first argument (”text” in this case) is part of the name of
Bar charts and
dot charts
Pie charts
the ”geom” to use. Geoms have names like GeomT ext and cor-
Histograms
Boxplots respond to a subset of the geom ? () functions.
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 104 / 146
Example 3.11
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The data set windW in80 from the M P V package contains pairs
statistical
graphics of observations of wind speed at the Winnipeg International Air-
Simple high level
plots
Bar charts and port. The columns give the wind speed in km per hour at midnight
dot charts
Pie charts
Histograms
(h0) and noon (h12).
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 105 / 146
Lecture 3:
Programming
statistical
A proposed model for the joint probability density function of these
graphics
Professor
two measurements is
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 106 / 146
Lecture 3:
Programming
statistical A first attempt at this plot can be obtained using the following
graphics
Professor
code.
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 107 / 146
Lecture 3:
Programming
statistical
graphics
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical After your first attempt at drawing a plot, you should look for
graphics
Simple high level
plots
ways to improve it. In the case of Figure 3.22, we can see these
Bar charts and
dot charts
Pie charts
issues:
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 109 / 146
First attempt at plot described in Example 3.11.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 110 / 146
Lecture 3:
Programming
statistical
The points appear to fall on a regular grid of values, because
graphics
Professor
the original data were rounded to the nearest integer. This
Amany E. Aly
might hide some points if they exactly overlap. A way to
Programming
statistical deal with overlapping points is jittering: move each point
graphics
Simple high level
plots
slightly by a random amount. If two points were overlapping
Bar charts and
dot charts
Pie charts
in the original, they probably won’t be overlapping after each
Histograms
Boxplots is randomly moved. The geom jitter() layer function does
Scatterplots
Plotting data
from data
frames
this for us in ggplot2.
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 111 / 146
Lecture 3:
Programming
statistical
The points appear to fall on a regular grid of values, because
graphics
Professor
the original data were rounded to the nearest integer. This
Amany E. Aly
might hide some points if they exactly overlap. A way to
Programming
statistical deal with overlapping points is jittering: move each point
graphics
Simple high level
plots
slightly by a random amount. If two points were overlapping
Bar charts and
dot charts
Pie charts
in the original, they probably won’t be overlapping after each
Histograms
Boxplots is randomly moved. The geom jitter() layer function does
Scatterplots
Plotting data
from data
frames
this for us in ggplot2.
QQ plots
Low level
graphics
The labels h0 and h12 on the plot mean should be made more
functions
Adding to plots
Graphics as a
informative using xlab() and ylab().
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 111 / 146
Lecture 3:
Programming
statistical
graphics
There appear to be too many points outside the widest contour
Professor
Amany E. Aly line, and too few inside the smallest ones where the peak of
Programming
statistical
the PDF occurs. This could be a subtle problem with the
graphics
Simple high level
plots
model, so it is worth pointing it out to the viewer. To do
Bar charts and
dot charts this, we could add an estimated PDF function to the plot
Pie charts
Histograms
Boxplots
using geom density2d(): this will estimate the PDF from the
Scatterplots
Plotting data
from data
points. We could then compare the two densities to see if they
frames
QQ plots
Low level
agree approximately.
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 112 / 146
Lecture 3:
Programming
statistical
graphics
Adding two sets of contours to the same plot would be con-
Professor
Amany E. Aly fusing. We’ll need to draw them in different colors. We’ll use
Programming
statistical
white in geom density2d(), since we are only adding it for
graphics
Simple high level
reference: it’s not the main point of the plot.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 113 / 146
Lecture 3:
Programming
statistical
graphics
Adding two sets of contours to the same plot would be con-
Professor
Amany E. Aly fusing. We’ll need to draw them in different colors. We’ll use
Programming
statistical
white in geom density2d(), since we are only adding it for
graphics
Simple high level
reference: it’s not the main point of the plot.
plots
Bar charts and
dot charts
Pie charts
The grid lines are going to make the plot too busy, so we can
Histograms
Boxplots remove them using a call to theme().
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 113 / 146
Lecture 3:
Programming
statistical
graphics
Adding two sets of contours to the same plot would be con-
Professor
Amany E. Aly fusing. We’ll need to draw them in different colors. We’ll use
Programming
statistical
white in geom density2d(), since we are only adding it for
graphics
Simple high level
reference: it’s not the main point of the plot.
plots
Bar charts and
dot charts
Pie charts
The grid lines are going to make the plot too busy, so we can
Histograms
Boxplots remove them using a call to theme().
Scatterplots
Plotting data
from data
frames
Finally, we’ll add some annotations to point out our areas of
QQ plots
Low level
graphics
concern.
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 113 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 114 / 146
Annotated, jittered, and improved plot described in Example 3.11.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 115 / 146
Lecture 3:
Programming
statistical
graphics
Professor To remove the grid, we set it to the special value element blank().
Amany E. Aly
We drew our text annotations outside the cloud of points to make
Programming
statistical
graphics
them more visible, then added line segments to point to the region
Simple high level
plots we were actually talking about. The hjust = 0 argument in
Bar charts and
dot charts
Pie charts the first one puts the text to the right of the reference point;
Histograms
Boxplots
Scatterplots
vjust = −1 puts the second text one line above its reference
Plotting data
from data
frames point.
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 116 / 146
Faceting
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly A strategy for displaying relations among three or more vari-
Programming ables is to divide the data into subsets using the values of some
statistical
graphics
Simple high level
of the variables, and then draw multiple plots of the values of
plots
Bar charts and
dot charts
the other variables in each of those subsets.
Pie charts
Histograms
Boxplots
In ggplot2 this is called ”faceting,” and the f acet wrap() and
Scatterplots
Plotting data
from data
f acet grid() functions are used to implement it.
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 117 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The f acet wrap() function draws these plots side-by-side, wrap-
Programming
statistical ping the display to the next line once there are more than a few.
graphics
Simple high level
plots
Its first argument describes the variables to use for subsetting.
Bar charts and
dot charts
Pie charts
This can be expressed by listing them in the vars() function, or
Histograms
Boxplots
Scatterplots
giving a formula using ∼.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 118 / 146
Example 3.12
Lecture 3:
Programming
statistical
graphics
To study the trends over time in the W orldP hones data, we first
Professor
Amany E. Aly
need to convert it to a data frame.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
If we are particularly interested in the changes in the distribution
Scatterplots
Plotting data
from data
between regions over time, we might plot similar bar charts for
frames
QQ plots
Low level
each year by specifying f acet wrap(vars(Y ear)) or f acet wrap(∼
graphics
functions
Adding to plots
Y ear).
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 119 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
The results are shown in Figure 3.24. In this plot, we suppressed
Boxplots
Scatterplots
Plotting data
the x−axis labels and tick marks, as the legend seemed sufficient
from data
frames
QQ plots
to identify the regions.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 120 / 146
Trends in phone usage.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
The results are shown in Figure 3.24. In this plot, we suppressed
graphics
Simple high level
plots
the x−axis labels and tick marks, as the legend seemed sufficient
Bar charts and
dot charts
Pie charts
to identify the regions.
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 121 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 122 / 146
Lecture 3:
Programming
statistical
graphics
Professor The f acet grid() function arranges the plots in a rectangle whose
Amany E. Aly
rows define one set of variables for subsetting, and whose column-
Programming
statistical
graphics
s represent another. These can be specified using vars() in the
Simple high level
plots
Bar charts and
rows and cols arguments, or using a formula for rows and skip-
dot charts
Pie charts
Histograms
ping cols. It also has an argument margins, which adds a row
Boxplots
Scatterplots ignoring the cols variables, and a column ignoring the rows vari-
Plotting data
from data
frames ables
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 123 / 146
Example 3.13
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The mpg data frame in ggplot2 contains fuel economy data for
Programming
statistical 38 models of cars from 1999 to 2008.
graphics
Simple high level
plots The cty and hwy columns measure miles per gallon in city and
Bar charts and
dot charts
Pie charts
highway, respectively. Engine displacement in litres is given in the
Histograms
Boxplots
Scatterplots
displ column, and cyl holds the number of cylinders.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 124 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
One would expect that fuel economy is worse in larger engines and
Programming
statistical with more cylinders, but it’s not obvious how these would affect
graphics
Simple high level
plots
the relation between city and highway efficiency. To study this,
Bar charts and
dot charts
Pie charts
we could subset the data according to values of displ and cyl and
Histograms
Boxplots
Scatterplots
draw scatterplots of cyl versus hwy for each.
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 125 / 146
Lecture 3:
Programming
statistical
graphics
However, displ is a continuous variable, and even though it is
Professor
Amany E. Aly
rounded to one decimal place, there are still 35 different values,
Programming
so the subsets would be too small.
statistical
graphics A way to address this is to break displ into a smaller number
Simple high level
plots
Bar charts and
of subsets based on ranges of values. The cut ? () functions in
dot charts
Pie charts
Histograms
ggplot2 do this in a few different ways: cut number() gives equal
Boxplots
Scatterplots
Plotting data
numbers of cases per subset, cut interval() gives equal ranges of
from data
frames
QQ plots
values per subset, and cut width() gives ranges with a specified
Low level
graphics
functions
width. We’ll use cut number().
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 126 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
The result is shown in next figure. We can see in the plot that
dot charts
Pie charts
Histograms
there is a change in the relation of cty and hwy with size of engine
Boxplots
Scatterplots
Plotting data
(smaller engines perform better in the city), but since displ and
from data
frames
QQ plots
cyl are quite closely related, we can,t determine whether this effect
Low level
graphics
functions
is more closely related to one of them than the other.
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 127 / 146
Plot of city versus highway mileage by engine characteristic.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 128 / 146
Groups in ggplot2
Lecture 3:
Programming
statistical
graphics
Professor
Some layer functions (e.g. geom point()) draw one thing for
Amany E. Aly
each observation, while others (e.g. geom histogram()) look
Programming
statistical at groups of points and compute what to draw once per group.
graphics
Simple high level
plots
There are two ways that ggplot2 uses to determine groups. The
Bar charts and
dot charts
Pie charts
usual way is to look at all of the discrete variables mentioned in
Histograms
Boxplots the plot, and creating a group out of every unique combination of
Scatterplots
Plotting data
from data
frames
levels. Groups can also be set explicitly, by specifying the group
QQ plots
Low level
graphics
aesthetic.
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 129 / 146
Plotting histograms separately by group.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 130 / 146
Lecture 3:
Programming
statistical
graphics For example, with the mpg data set,
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts The result (shown in Figure 3.26) draws each group’s histogram
Histograms
Boxplots
Scatterplots
in a different color, and stacks them in the plot.
Plotting data
from data
frames
The geom histogram() parameter binwidth sets the size of each
QQ plots
Low level
graphics
bin.
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 131 / 146
Lecture 3:
Programming
statistical
graphics
Professor To draw the bars side-byside, you could use the parameter
Amany E. Aly
position = ”dodge”. That doesn’t look very good in this data
Programming
statistical
graphics
set (try it!), but can be a good strategy with just two or three
Simple high level
plots
Bar charts and
groups. If we had used aes( hwy, group = cyl) we would get
dot charts
Pie charts
Histograms
the same grouping (the variable used with group doesn’t need to
Boxplots
Scatterplots be a factor), but it would be invisible, since all groups would be
Plotting data
from data
frames drawn in the default color.
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 132 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
Solve Exercises 1, 2 and 3 on pages 86 and 87.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 133 / 146
Other graphics systems
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Besides base graphics and ggplot2, there are many other graphics
Simple high level
plots
Bar charts and
systems supported within R.
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 134 / 146
The lattice package
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
The lattice package is a high level graphics system for R that
statistical
graphics predated ggplot2. Ideas similar to faceting are central to it: con-
Simple high level
plots
Bar charts and
dot charts
ditioning scatterplots reduce the effort required to repeat similar
Pie charts
Histograms plots for multiple subsets of data.
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 135 / 146
Example 3.14
Lecture 3:
Programming
statistical
graphics To show the trends over time in the W orldP hones data as in
Professor
Amany E. Aly Figure 3.27, we could use
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots We first convert the matrix to a data frame. The formula
Plotting data
from data
frames T elephones ∼ Y ear|Region says to plot T elephones against
QQ plots
Low level
graphics
functions
Y ear conditional on the Region; Figure 3.27 shows the result.
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 136 / 146
The grid package
Lecture 3:
Programming
statistical
graphics
Both ggplot2 and lattice are implemented using the grid pack-
Professor
Amany E. Aly
age. This package uses a different mental model of graphics than
Programming
statistical
the base graphics we discussed earlier. A chart is thought of as
graphics
Simple high level
plots
a hierarchy of graphical objects (which grid calls ”grobs”) within
Bar charts and
dot charts
Pie charts
rectangular regions with associated coordinate systems (”view-
Histograms
Boxplots ports”). This mental model makes transformations and modifica-
Scatterplots
Plotting data
from data
frames
tions of the display possible, something that can’t really be done
QQ plots
Low level in base graphics.
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 137 / 146
Trends in telephone usage plotted using the lattice package.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 138 / 146
Example 3.15
Lecture 3:
Programming
statistical
graphics The following code illustrates the basic use of viewports. The
Professor
Amany E. Aly
result is shown in Figure 3.28.
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 139 / 146
Lecture 3:
Programming
statistical
graphics
First, a rectangle is drawn, containing the original viewport (a
Professor
Amany E. Aly 1 × 1 square). Aviewport having height 0.4 and width 0.6, and
Programming
statistical
rotated at an angle of 6̇0, is then pushed. A red rectangle is drawn
graphics
Simple high level
showing the outline of the viewport. Finally, the same operation is
plots
Bar charts and
dot charts repeated, creating a viewport, rotated by a further 60 and which
Pie charts
Histograms
Boxplots
has a height and width which are 40% and 60% of the size of the
Scatterplots
Plotting data
from data
previously drawn viewport. The small blue rectangle shows the
frames
QQ plots
Low level
outline of the new viewport.
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 140 / 146
Simple demonstration of the effect of rotating viewports using the
grid package.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 141 / 146
Interactive graphics
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The graphics systems that we have discussed so far are de-
signed for static graphics, typically printed on a page. Pack-
Programming
statistical
graphics
ages within R also support many kinds of interactive graphics.
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 142 / 146
Interactive graphics
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
The graphics systems that we have discussed so far are de-
signed for static graphics, typically printed on a page. Pack-
Programming
statistical
graphics
ages within R also support many kinds of interactive graphics.
Simple high level
plots
Bar charts and
The rgl package is designed for three-dimensional, rotatable
dot charts
Pie charts
Histograms
displays. It includes functions modeled on the base graphics
Boxplots
Scatterplots
Plotting data
functions to set up plots, and also ways to display them on
from data
frames
QQ plots
screen or on a web page.
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 142 / 146
Lecture 3:
Programming
statistical
graphics
Lecture 3:
Programming
statistical
graphics
Executing this code:
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots in RStudio will result in the display of an interactive map with a
Scatterplots
Plotting data
from data
frames
marker in Auckland, New Zealand. Figure 3.29 shows the initial
QQ plots
Low level
graphics
display.
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 144 / 146
A map showing the birthplace of R.
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 145 / 146
Lecture 3:
Programming
statistical
graphics
Professor
Amany E. Aly
Programming
statistical
graphics
Simple high level
Solve Chapter exercises from 1 to 5 on pages 90 and 91.
plots
Bar charts and
dot charts
Pie charts
Histograms
Boxplots
Scatterplots
Plotting data
from data
frames
QQ plots
Low level
graphics
functions
Adding to plots
Graphics as a
language:
ggplot2
Professor
Details ofAmany
the E. Aly ( Department of Mathematics, FacultyLecture
of Science,
3: Programming
Helwan University,
statistical
Aingraphics
Helwan, Cairo, Egypt.) 14 / 10 / 2024 146 / 146