data visualization with ggplot2

Post on 09-Dec-2021

29 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Data visualization with ggplot2

R.W. Oldford

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

Have some function/module which takes some input, performs some actions on it(transformations, summarizing, adding information, etc.) and produces output:

If we have several of these, we can connect them one to another in sequence to producea “pipeline” of modules or steps in the processing of the original input:

The connected components form a “pipeline” through which the original input “flows”,with some processing/transformation of the data occurring at each step.

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf" | sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf" | sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes

| grep ".pdf" | sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf"

| sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf" | sort

| more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf" | sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf" | sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Computational pipelines

A simple metaphor (viz. that of laying pipes end to end):

- data passes through and is processed by a set of computational steps serially linked sothat the output of one becomes the input of the next

- the Unix “pipe” | is called a “pipe”: ls -R Notes | grep ".pdf" | sort | more

- a graphics rendering pipeline (from Kaufman, Fan and Petkov (2009) Implementing the lattice Boltzmann model oncommodity graphics hardware J. Stat. Mech.)]

Wilkinson’s Grammar of Graphics pipeline

Lee Wilkinson’s monumental The Grammar of Graphics begins with a pipelinemodel for constructing statistical graphics:

Each step in the pipeline transforms its input to produce output for the next step.

The order of steps is essential, though not all need be there for every plot.

Because the pipeline consists of separate components, the final graphic that isrendered can be simply and sometimes dramatically changed by making changesto a single component in the pipeline.

ggplot2 – a grammar of graphics for R

Inspired by Wilkinson’s “Grammar of Graphics”, Hadley Wickham (in his 2008 IowaState PhD thesis: Practical tools for exploring data and models) developed a “layeredgrammar of graphics.” This is implemented as ggplot2 in R.

library(ggplot2)

Much like Wilkinson’s original grammar, ggplot2 uses a pipeline model for its graphicsconstruction in that a plot is built in an ordered series of steps, where each stepoperates on the output of its immediate predecessor in the line. Departing from thegrammar, ggplot2 slightly mixes metaphors in that each step in the pipeline can(typically) be thought of as adding a layer to all that preceded it.

From the ggplot2 book:

"The layered grammar of graphics (Wickham 2009) builds on Wilkinson’s grammar,focussing on the primacy of layers and adapting it for embedding within R. In brief, thegrammar tells us that a statistical graphic is a mapping from data to aesthetic attributes(colour, shape, size) of geometric objects (points, lines, bars). The plot may also containstatistical transformations of the data and is drawn on a specific coordinate system.Facetting can be used to generate the same plot for different subsets of the dataset. Itis the combination of these independent components that make up a graphic."

Notationally, the components of the pipeline appear in sequence connected one to thenext via an intervening + sign, thus emphasizing each as an addition of a layer (or ofsome further processing of the plot).

Data - South African heart disease

Consider the ‘SAheart‘ data from the package‘ElemStatLearn‘.This is a sample from a retrospective study of heart disease inmales from a high-risk region of the Western Cape, SouthAfrica.There are 462 cases and 10 variates. The first fewobervations (cases) are shown below.

sbp tobacco ldl adiposity f amhist typea obesity alcohol age chd160 12.00 5.73 23.11 Present 49 25.30 97.20 52 1144 0.01 4.41 28.61 Absent 55 28.87 2.06 63 1118 0.08 3.48 32.28 Present 52 29.14 3.81 46 0170 7.50 6.41 38.03 Present 51 31.99 24.26 58 1134 13.60 3.50 27.78 Present 60 25.99 57.34 49 1132 6.20 6.47 36.21 Present 62 30.77 14.14 45 0

For example, sbp denotes “systolic blood pressure”, sbp “low density lipoproteincholesterol”. famhist “family history of heart disease”, age “age at onset” (in years),and chd indicates whether the patient has coronary heart disease or not (a response).(see help(SAheart, package="ElemStatLearn") for details)

Constructing a plot - the pipeline

In the grammar of graphics, a plot processes each component in turn

ggplot(data = SAheart)

First the data

Constructing a plot - pipeline

In the grammar of graphics, a plot processes each component in turn

ggplot(data = SAheart) + aes( x = age, y = chd)

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Then the mapping of the data to plot “aesthetics”

Constructing a plot - pipeline

In the grammar of graphics, a plot processes each component in turn

ggplot(data = SAheart) + aes( x = age, y = chd) + geom_point()

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Then the geometry.

Constructing a plot - pipeline

In the grammar of graphics, a plot processes each component in turn

ggplot(data = SAheart) + aes( x = age, y = chd) + geom_point() + geom_smooth()

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Which can have several further steps in the pipeline

Constructing a plot

Alternatively, in the grammar of ggplot2, a plot is also a sum of component layers.

ggplot(data = SAheart, mapping = aes(x = age, y = chd))

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

The base display with mapping.

Constructing a plot

Alternatively, in the grammar of ggplot2, a plot is also a sum of component layers.

ggplot(data = SAheart, mapping = aes(x = age, y = chd)) +geom_point()

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Here the + is adding layers.

Constructing a plot

Alternatively, in the grammar of ggplot2, a plot is also a sum of component layers.

ggplot(data = SAheart, mapping = aes(x = age, y = chd)) +geom_point() + geom_smooth()

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Here the + is adding layers.

Constructing a plot - separate mappings

Alternatively, we could deliberately associate only the data with the plot, forcing the mapping of thedata to aesthetics within each individual component layer:

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd))

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

The mapping is explicit for each layer.

Constructing a plot - separate mappings

What would the following plot look like?

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd)) +geom_smooth()

It fails . . . why?

How could it be fixed?

Cautionary note: the ggplot2 grammar mixes the two metaphors of “layers” and“pipes”.

Just because an aesthetic precedes a component in the pipeline does not mean that it isavailable for use.

Constructing a plot - separate mappings

What would the following plot look like?

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd)) +geom_smooth()

It fails . . . why?

How could it be fixed?

Cautionary note: the ggplot2 grammar mixes the two metaphors of “layers” and“pipes”.

Just because an aesthetic precedes a component in the pipeline does not mean that it isavailable for use.

Constructing a plot - separate mappings

What would the following plot look like?

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd)) +geom_smooth()

It fails . . . why?

How could it be fixed?

Cautionary note: the ggplot2 grammar mixes the two metaphors of “layers” and“pipes”.

Just because an aesthetic precedes a component in the pipeline does not mean that it isavailable for use.

Constructing a plot - separate mappings

Solution 1: explicitly, give the mapping for each layer:

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd)) +geom_smooth(mapping = aes(x = age, y = chd))

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Constructing a plot - separate mappings

Solution 2: provide aesthetics upstream:

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd)) +aes(x = age, y = chd) +geom_smooth()

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

Constructing a plot - separate mappings

ggplot(data = SAheart) +geom_point(mapping = aes(x = age, y = chd, col = famhist)) +geom_smooth(mapping = aes(x = age, y = chd))

0.00

0.25

0.50

0.75

1.00

20 30 40 50 60

age

chd

famhist

Absent

Present

Constructing a plot - shared and separate mappings

ggplot(data = SAheart) + aes(group = famhist) +geom_point(mapping = aes(x = age, y = chd)) +geom_smooth(mapping = aes(x = age, y = chd))

0.0

0.5

1.0

20 30 40 50 60

age

chd

Constructing a plot - shared and separate mappings

ggplot(data = SAheart, mapping = aes(group = famhist)) +geom_point(mapping = aes(x = age, y = chd, col = famhist)) +geom_smooth(mapping = aes(x = age, y = chd))

0.0

0.5

1.0

20 30 40 50 60

age

chd

famhist

Absent

Present

Constructing a plot - shared and separate mappings

ggplot(data = SAheart, mapping = aes(group = famhist, col = famhist)) +geom_point(mapping = aes(x = age, y = chd)) +geom_smooth(mapping = aes(x = age, y = chd))

0.0

0.5

1.0

20 30 40 50 60

age

chd

famhist

Absent

Present

Constructing a plot

Alternatively, we could split the plot into two pieces by facetting:

ggplot(data = SAheart, mapping = aes(x = age, y = chd)) +geom_point(col="steelblue", size = 3, alpha = 0.4) +geom_smooth(method = "loess", col = "steelblue") +facet_wrap(~famhist)

Absent Present

20 30 40 50 60 20 30 40 50 60

0.0

0.5

1.0

age

chd

Components of the layered grammar

In the grammar of ggplot2, a plot is a structured combination of:

I a dataset,I a set of mappings from variates to aesthetics,I one or more layers, each composed ofI a geometric object,I a statistical transformation,I a position adjustment, andI (optionally) its own dataset and aesthetic mappingsI a scale for each aesthetic mapping,I a coordinate system,I a facetting specification

Geometric objects

There are a variety of geometric objects that can be added to a plot

I geom_abline(), geom_hline(),geom_vline(), geom_curve(),geom_segment(), geom_step()

I geom_label(), geom_text()I geom_point(), geom_smooth(), geom_crossbar(), geom_errorbar(),

geom_errorbarh(), geom_linerange(), geom_pointrange(),I geom_rect(), geom_raster(), geom_area(), geom_ribbon(),

geom_tile(),I geom_bar(), geom_col(),I geom_dotplot(), geom_boxplot(), geom_histogram(),

geom_freqpoly(), geom_density(), geom_violin(), geom_quantile(),geom_qq()

I geom_bin2d(), geom_density2d(), geom_hex(),I geom_contour(),I geom_map(), geom_polygon()

Each of these will have their own arguments including mapping, data, stat, etcetera.

Geometric objects - adding to plotsBeginning with a plot different geometric objects may be added. For example:

p <- ggplot(data = SAheart, mapping = aes(x = tobacco, y = sbp))p

100

125

150

175

200

0 10 20 30

tobacco

sbp

Geometric objects - points and density

Beginning with a plot different geometric objects may be added. For example:

p + geom_point() + geom_density_2d(lwd = 1.5, col = "steelblue")

100

125

150

175

200

0 10 20 30

tobacco

sbp

Geometric objects - histogram

h <- ggplot(data = SAheart, mapping = aes(x = adiposity))h + geom_histogram(bins = 10, fill = "steelblue",

col = "black", alpha = 0.5)

0

25

50

75

10 20 30 40

adiposity

coun

t

Note that had we tried to layer the histogram on top of p, it would have inherited from p a yaesthetic (namely y = sbp) which does not make sense for a histogram.

Geometric objects - histogram

h + geom_histogram(mapping = aes(y = ..density..),bins = 10, fill = "steelblue",col = "black", alpha = 0.5)

0.00

0.01

0.02

0.03

0.04

0.05

10 20 30 40

adiposity

dens

ity

A y aesthetic that does make sense for a histogram is ..density.. which forces the scaling of thevertical axis so that the histogram has unit area.

Note that the x aesthetic was inherited from h.

Geometric objects - density scale histogramProvided we provide a y aesthetic mapping, a histogram could therefore be added to p as well.

p + geom_histogram(mapping = aes(x = adiposity, y = ..density..),bins = 10, fill = "steelblue",col = "black", alpha = 0.5)

0.00

0.01

0.02

0.03

0.04

0.05

10 20 30 40

tobacco

sbp

Note:

I the change in vertical scale matches the histogramI the axes labels do not match the aesthetics of the histogram (though the tick marks and

values happen to)

Because this is only a grammar, it is as easy to make silly visualizations as it is silly sentences.

Geometric objects - layering effect

The order of layering (on top of h now) matters:

h + geom_histogram(mapping = aes(y = ..density..),bins = 10, fill = "steelblue",col = "black", alpha = 0.5) +

geom_density(mapping = aes(y = ..density..),fill = "grey", alpha = 0.5)

0.00

0.01

0.02

0.03

0.04

0.05

10 20 30 40

adiposity

dens

ity

Note that the y aesthetic had to be repeated here . . .

Geometric objects - layering effect

Switch the order of addition:

h + geom_density(mapping = aes(y = ..density..),fill = "grey", alpha = 0.5) +

geom_histogram(mapping = aes(y = ..density..),bins = 10, fill = "steelblue",col = "black", alpha = 0.5)

0.00

0.01

0.02

0.03

0.04

0.05

10 20 30 40

adiposity

dens

ity

Note that the aesthetics need to be repeated here . . .

Geometric objects - bar charts

ggplot(SAheart) + geom_bar(mapping = aes(x = factor(chd), fill = famhist)) +labs(x = "chd", title ="South African heart disease") + coord_flip()

0

1

0 100 200 300

count

chd

famhist

Absent

Present

South African heart disease

Which makes you wonder how the data were collected.

Geometric objects

A different scatterplot

p2 <- ggplot(data = SAheart, mapping = aes(x = sqrt(age), y = sbp))p2 + geom_point()

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

Geometric objects

Note that each geometric object has its own arguments and properties that canbe set.

p2 + geom_point(col = "red", size = 3, pch = 21,fill = "yellow", alpha = 0.5) +

geom_smooth(method = "loess", col = "steelblue",lty = 2, lwd = 1.5, alpha = 0.2)

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

Geometric objects

Aesthetics apply to every point individually.

p2 + geom_point(mapping = aes(size = obesity), fill = "steelblue",col = "black", pch = 21, alpha = 0.4) +

geom_smooth(method = "loess", col = "yellow",lty = 2, lwd = 1.5, alpha = 0.2)

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

obesity

20

30

40

Geometric objects

Aesthetics apply to every point individually.

p2 + geom_point(mapping = aes(size = obesity, fill = tobacco),col = "black", pch = 21, alpha = 0.4) +

geom_smooth(method = "loess", col = "yellow",lty = 2, lwd = 1.5, alpha = 0.2)

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

0

10

20

30tobacco

obesity

20

30

40

Geometric objects

The data may change with each layer

heartAttack <- SAheart[, "chd"] == 1hAplot <- p2 + geom_point(data = SAheart[heartAttack, ],

mapping = aes(size = obesity), alpha = 0.4,col = "black", pch = 21, fill = "steelblue")

hAplot

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

obesity

15

20

25

30

35

40

45

Geometric objects

The data may change with each layerqboth <- hAplot +

geom_point(data = SAheart[!heartAttack, ], # Not heartAttackmapping = aes(size = obesity), alpha = 0.4,col = "black", pch = 21, fill = "firebrick")

qboth

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

obesity

20

30

40

Geometric objects

The data may change with each layerqboth +

geom_smooth(data = SAheart[heartAttack, ],method = "loess", col = "steelblue", alpha = 0.4) +

geom_smooth(data = SAheart[!heartAttack, ],method = "loess", col = "firebrick", alpha = 0.4)

120

160

200

4 5 6 7 8

sqrt(age)

sbp

obesity

20

30

40

Geometric objects

The data may change with each layer

qboth + geom_smooth(method = "loess")

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

obesity

20

30

40

Note smooth is using all of the data here.

Geometric objects

The data may change with each layer

qboth + geom_smooth(mapping = aes(color = factor(chd)), method = "loess")

120

160

200

4 5 6 7 8

sqrt(age)

sbp

factor(chd)

0

1

obesity

20

30

40

Here the smooth is separate for each colour given by chd as factor. Note ggplot’s default colours.

Geometric objectsThe colours can be coordinated by relying on the original data and using chd asa factor:

p2 + geom_point(mapping = aes(size = obesity, fill = factor(chd)),col = "black", pch = 21, alpha = 0.4) +

geom_smooth(mapping = aes(col = factor(chd)),method = "loess", lwd = 1.5, alpha = 0.2)

120

160

200

4 5 6 7 8

sqrt(age)

sbp

factor(chd)

0

1

obesity

20

30

40

Here the smooth is separate for each colour given by chd as factor.

ScalesA map from the domain of data values to the range of some aesthetic(e.g. colour, size, axis ranges, . . . ).

p2 + geom_point(mapping = aes(size = obesity, fill = factor(chd)),col = "black", pch = 21, alpha = 0.4) +

geom_smooth(mapping = aes(col = factor(chd)),method = "loess", lwd = 1.5, alpha = 0.2) +

scale_fill_manual("chd", values=c("steelblue", "firebrick"))+scale_color_manual("chd", values=c("steelblue", "firebrick"))

120

160

200

4 5 6 7 8

sqrt(age)

sbp

chd

0

1

obesity

20

30

40

. . . gets your own “scale” values for colour and for fill.

ScalesA map from the domain of data values to the range of some aesthetic(e.g. colour, size, axis ranges, . . . ).p2 + geom_point(mapping = aes(size = obesity, fill = factor(chd)),

col = "black", pch = 21, alpha = 0.4) +geom_smooth(mapping = aes(col = factor(chd)),

method = "loess", lwd = 1.5, alpha = 0.2) +scale_fill_manual("chd", values=c("steelblue", "firebrick"))+scale_color_manual("chd", values=c("steelblue", "firebrick")) +scale_size("obesity", breaks = seq(0,100,5))

120

160

200

4 5 6 7 8

sqrt(age)

sbp

chd

0

1

obesity

15

20

25

30

35

40

45

. . . additonally gets your own “scale” values for point size (which is proportional to area).

ScalesA map from the domain of data values to the range of some aesthetic(e.g. colour, size, axis ranges, . . . ).p2 + geom_point(mapping = aes(size = obesity, fill = factor(chd)),

col = "black", pch = 21, alpha = 0.4) +geom_smooth(mapping = aes(col = factor(chd)),

method = "loess", lwd = 1.5, alpha = 0.2) +scale_fill_manual("chd", values=c("steelblue", "firebrick"))+scale_color_manual("chd", values=c("steelblue", "firebrick")) +scale_size_area("obesity", breaks = seq(0,100,5))

120

160

200

4 5 6 7 8

sqrt(age)

sbp

chd

0

1

obesity

15

20

25

30

35

40

45

. . . Now a zero value gives a zero area.

Position scales

There are two position scales: horizontal (x) and vertical (y).

p + geom_point(alpha = 0.5, size = 1) +scale_x_continuous(limits = c(0,40)) +scale_y_continuous(limits = c(75,225))

100

150

200

0 10 20 30 40

tobacco

sbp

Position scales

There are two position scales: horizontal (x) and vertical (y).

p + geom_point(alpha = 0.5, size = 1) + xlim(0,40) + ylim(75,225)

100

150

200

0 10 20 30 40

tobacco

sbp

Position scales

There are two position scales: horizontal (x) and vertical (y).

p + aes(x = tobacco + 1) + geom_point(alpha = 0.5, size = 1) + scale_x_log10()

100

125

150

175

200

1 10

tobacco + 1

sbp

Coordinates

This is the coordinate system in which the positions are to be plotted. We havealready seen coord_flip() which swaps the x and y axes. There are manyothers; the aspect ratio, for example, is fixed using coord_fixed():

ggplot(SAheart, aes(obesity, adiposity)) + geom_point() + coord_fixed(ratio = 1)

10

20

30

40

20 30 40

obesity

adip

osity

Here the aspect ratio is fixed so that one unit change in the x direction produces only one unit change in the ydirection.

Coordinates

This is the coordinate system in which the positions are to be plotted. We havealready seen coord_flip() which swaps the x and y axes. There are manyothers; the aspect ratio, for example, is fixed using coord_fixed():

ggplot(SAheart, aes(obesity, adiposity)) + geom_point() + coord_fixed(ratio = 0.5)

10

20

30

40

20 30 40

obesity

adip

osity

Here the aspect ratio is fixed so that one unit change in the x direction produces only half a unit change in the ydirection.

CoordinatesOne coordinate system that is used is called coord_polar() which, unlike itsname suggests, does not calculate a polar coordinate transformation but rathertreats one of the two positions as defining an angle and the other as defining theradius.ggplot(SAheart, aes(obesity, adiposity)) + geom_point() + geom_smooth() +

coord_polar(theta = "x")

20

30

40

10

20

30

40

obesity

adip

osity

which, arguably, is a pretty weird plot but does demonstrate how coordinate systems are abstractedout as part of the grammar. Consequently coord_polar() should be used with considerable caution

Coordinates

Arguably overly complicated, one use of coord_polar() is to construct a piechart.

This is just a bar chart expressed using coord_polar(). First the bar chart

barChart <- ggplot(SAheart, aes(x = factor(1), fill = famhist)) +geom_bar(width = 1) + xlab("")

barChart

0

100

200

300

400

1

coun

t famhist

Absent

Present

Coordinates

Arguably overly complicated, one use of coord_polar() is to construct a piechart.

This is just a bar chart expressed using coord_polar(). Now the pie chart

barChart + coord_polar(theta = "y")

0

100

200

300

400

1

count

famhist

Absent

Present

Coordinates

What’s going on here?

barChart + coord_polar(theta = "x")

0

100

200

300

400co

unt famhist

Absent

Present

Perhaps a little “too clever by half”?

. . . Be careful with coord_polar(); it’s easy to have it make avery difficult to interpret plot.

Coordinates

What’s going on here?

barChart + coord_polar(theta = "x")

0

100

200

300

400co

unt famhist

Absent

Present

Perhaps a little “too clever by half”? . . . Be careful with coord_polar(); it’s easy to have it make avery difficult to interpret plot.

Positions

A bar chart with two variates. Default position is “stack”

barChart2 <- ggplot(SAheart, aes(x = factor(chd), fill = famhist)) +geom_bar(position="stack") + xlab("chd")

barChart2

0

100

200

300

0 1

chd

coun

t famhist

Absent

Present

which stacks the two colours in the same bar.

Positions

What should this look like?

barChart2 + coord_polar(theta = "y")

0

100200

300

0

1

count

chd

famhist

Absent

Present

Thickness is the bar width, each ring is chd, arc length is count. Again, coord_polar() can beconfusing.

Positions

What should this look like?

barChart2 + coord_polar(theta = "y")

0

100200

300

0

1

count

chd

famhist

Absent

Present

Thickness is the bar width, each ring is chd, arc length is count. Again, coord_polar() can beconfusing.

Positions

What should this look like?

barChart2 + coord_polar(theta = "x")

010

100

200

300

chd

coun

t famhist

Absent

Present

Angle is the now the bar width, each wedge is chd, thickness is count. Again, coord_polar() canbe confusing.

Positions

What should this look like?

barChart2 + coord_polar(theta = "x")

010

100

200

300

chd

coun

t famhist

Absent

Present

Angle is the now the bar width, each wedge is chd, thickness is count. Again, coord_polar() canbe confusing.

Positions

To place the colours beside each other rather than stack them, change theposition to “dodge”

barChart3 <- ggplot(SAheart, aes(x = factor(chd), fill = famhist)) +geom_bar(position="dodge") + xlab("chd")

barChart3

0

50

100

150

200

0 1

chd

coun

t famhist

Absent

Present

which stacks the two colours in the same bar.

Positions

What should this look like?

barChart3 + coord_polar(theta = "y")

0

50

100

150

200

0

1

count

chd

famhist

Absent

Present

Explain.

Positions

What should this look like?

barChart3 + coord_polar(theta = "y")

0

50

100

150

200

0

1

count

chd

famhist

Absent

Present

Explain.

Positions

Now what should this look like?

barChart3 + coord_polar(theta = "x")

010

50

100

150

200

chd

coun

t famhist

Absent

Present

Explain.

Positions

Now what should this look like?

barChart3 + coord_polar(theta = "x")

010

50

100

150

200

chd

coun

t famhist

Absent

Present

Explain.

Positions and facets

A bar chart with two variates. Use facets

barChart4 <- ggplot(SAheart, aes(x = factor(chd), fill = famhist)) +geom_bar(position = "stack") + xlab("chd") + facet_wrap(~chd)

barChart4

0 1

0 1 0 1

0

100

200

300

chd

coun

t famhist

Absent

Present

Exercise: What should barChart4 + coord_polar(theta = "y") look like? What aboutbarChart4 + coord_polar(theta = "x")?

Positions and facets

A bar chart with two variates. Use facets

barChart5 <- ggplot(SAheart, aes(x = factor(chd), fill = famhist)) +geom_bar(position = "dodge") + xlab("chd") + facet_wrap(~chd)

barChart5

0 1

0 1 0 1

0

50

100

150

200

chd

coun

t famhist

Absent

Present

Exercise: What should barChart5 + coord_polar(theta = "y") look like? What aboutbarChart5 + coord_polar(theta = "x")?

Positions and facets

A bar chart with two variates. Use both variates as facets

barChart6 <- ggplot(SAheart, aes(x = factor(chd), fill = famhist)) +geom_bar(position = "dodge") + xlab("chd") + facet_wrap(famhist~chd)

barChart6

Present

0

Present

1

Absent

0

Absent

1

0 1 0 1

0

50

100

150

200

0

50

100

150

200

chd

coun

t famhist

Absent

Present

Exercise: What should barChart6 + coord_polar(theta = "y") look like?

What about barChart6 + coord_polar(theta = "x")?

Statistical transformations - stat

These transformations often summarize data in some manner (e.g. by counting,by averaging, etc.). Some statistical functions operate “behind the scenes”:

I stat_bin() in geom_bar(), geom_freqpoly(), geom_histogram()I stat_bin2d() in geom_bin2d()I stat_bindot() in geom_dotplot()I stat_binhex() in geom_hex()I stat_boxplot() in geom_boxplot()I stat_contour() in geom_contour()I stat_quantile() in geom_quantile()I stat_smooth() in geom_smooth()I stat_sum() in geom_count()

but may also be called directly (outside the geom_)

Statistical transformations - stat

Other stats have no corresponding geom_ function:

I stat_ecdf(): compute a empirical cumulative distribution plot.I stat_function(): compute y values from a function of x values.I stat_summary(): summarise y values at distinct x values.I stat_summary2d(), stat_summary_hex(): summarise binned values.I stat_qq(): perform calculations for a quantile-quantile plot.I stat_spoke(): convert angle and radius to position.I stat_unique(): remove duplicated rows.

Statistical transformations - example

Adding some statistical summary information to the scatterplot p2

p2 + geom_point(mapping = aes(size = obesity, fill = factor(chd)),col = "black", pch = 21, alpha = 0.4) +

stat_summary(geom = "point", fun.y = "median",col = "yellow", size = 2, pch = 19)

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

factor(chd)

0

1

obesity

20

30

40

Adds the median of the ys at each observed x.

Statistical transformations - exampleAlternatively use stat = "summary" in geom_point(). Also add connectinglines to the scatterplot p2

p2 + geom_point(mapping = aes(size = obesity, fill = factor(chd)),col = "black", pch = 21, alpha = 0.4) +

geom_point(stat = "summary", fun.y = "median",col = "yellow", size = 2, pch = 19) +

stat_summary(geom = "line", fun.y = "median",col = "yellow", size = 1, pch = 19)

100

125

150

175

200

4 5 6 7 8

sqrt(age)

sbp

factor(chd)

0

1

obesity

20

30

40

Adds the median of the ys at each observed x.

Miscellaneous

I Can also facet in a matrix grid using facet_grid()I position can also be “jitter” (best for scatterplots)I there is a function called theme() which is how the appearance of all

non-data plot components are changed.I E.g. it is possible to turn that grey background grid off via theme() (though

it seems a lot of work)I there is a function qplot() or quickplot() which is more like a base

graphics plot() call and so may be easier to use than following theggplot2 grammar via ggplot() + ...

I ggsave() will save the most recent ggplot.

Miscellaneous

Note: to plot time series (objects of class ts) you need the ggfortify package andthen use autoplot().

library(ggfortify)autoplot(sunspots)

0

50

100

150

200

250

1750 1800 1850 1900 1950

Similarly, library(ggmap) for raster maps from get_map()

top related