39
DATA VISUALIZATION WITH GGPLOT2 Visible Aesthetics

Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

DATA VISUALIZATION WITH GGPLOT2

Visible Aesthetics

Page 2: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics?

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth

Type PropertyShape 4

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth

Type PropertyColour Red

●●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●●

●●

●●

●●

●●

●● ●

●●● ●

● ●●

●●

●●

●●

●●●

●●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth

Type PropertySize 10

A!ributes!

Type VariableColour Species

mapping Species on colour

Page 3: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth

Mapping

Page 4: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(col = "red")

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth

A!ribute

Page 5: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point()

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

Data frame column mapped onto visible aesthetic

Aesthetics in aes(), a!ributes in geom_()

Mapping onto color

Page 6: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

> ggplot(iris) + geom_point(aes(x = Sepal.Length, y = Sepal.Width, col = Species))

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

Mapping onto color (2)

Only if different data sources

Page 7: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Typical AestheticsAesthetic Description

x X axis position

y Y axis position

colour Colour of dots, outlines of other shapes

fill Fill colour

size Diameter of points, thickness of lines

alpha Transparency

linetype Line dash pa!ern

labels Text on a plot or axes

shape Shape

Page 8: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

Page 9: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

DATA VISUALIZATION WITH GGPLOT2

Aesthetics Best Practices

Page 10: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Which Aesthetic?● Be creative

● Clear guidelines

● Jacques Bertin

● The Semiology of Graphics, 1967

● William Cleveland

● Perception of visual elements (90s)

Page 11: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Explain

Informand

Persuade

Explore

Confirmand

Analyse

Form follows Function

Page 12: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetic Description

x X axis position

y Y axis position

colour Colour of dots, outlines of other shapes

fill Fill colour

size Diameter of points, thickness of lines

alpha Transparency

linetype Line dash pa!ern

labels Text on a plot or axes

shape Shape

Aesthetics

Page 13: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Continuous Variables> ggplot(iris.1, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point()

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

Page 14: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Continuous Variables> ggplot(iris.1, aes(col = Sepal.Length, y = Sepal.Width, x = Species)) + geom_point()

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

2.0

2.5

3.0

3.5

4.0

4.5

Setosa Versicolor VirginicaSpecies

Sepal.W

idth

5

6

7

Sepal.Length

Page 15: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Continuous VariablesAesthetic Description

x X axis position

y Y axis position

size Diameter of points, thickness of lines

alpha Transparency

colour Colour of dots, outlines of other shapes

fill Fill colour

Page 16: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Guide - Continuous Variables

Position -on a

common scale

Position -on same, but

unaligned, scale AreaGrey-ScaleSpectrum

ColourSpectrum

MonochromaticSpectrum Angle Length

Efficiency and Accuracy of DecodingHighLow

Image adapted from Wong, B, Nat Met, 7 (9), 2010, p665

Page 17: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Unaligned y axes

●●●

● ●

●●

●●●

●●

●●●

●●

●●●●

● ●● ●

●●

●●●

●● ●

●●

●●

●●

●●

●●

●●●●

●●

●●

●●

●●

●●

●●● ●●

●●● ●●

● ●● ●

● ● ● ●●●

● ●

●● ●●●

●●● ●●

●● ●

● ●●

●●

●● ●● ●

●●

● ●

●●

● ●

●●

●●

●●

● ●

●●

●●

● ●

●●

0

2

4

6

Length WidthMeasure

Setosa

Part●

PetalSepal

●●

●●●

●●

●●

●●

●●

●●●

●●

●● ●●

●●

●●

●●

●●

●● ●

●● ●

●●●

●● ●

●● ●●

●●

●●

●●●●

●●

●●●●

●● ●

●●

●●

● ●●●●

●● ●●

● ●●

●●

● ●

●●

●●●

●●

● ●

●●

●●

●●●

2

4

6

Length WidthMeasure

Versicolor Part

PetalSepal

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●● ●

●●

●●

●●

●● ●

●●

● ●

●●

●●

● ●

● ●●

●●

●●

●●

●●

●●

●●

●●

●● ●●

● ●●

●●

●●

●●

● ●●

●●

● ●●

● ●

2

4

6

8

Length WidthMeasure

Virginica Part

PetalSepal

Page 18: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Common y axisSetosa Versicolor Virginica

●●

●●

●●

●●

●● ●

●●

● ●

●●

●●

●●

●●

●●

●●

●●●

●●

●●

●●

● ●

●●

●●

●●

●●

●● ●

●●

●●

●●

●●

●●

●● ●● ●

●● ●●●●

●●●

●●

●●

●●

●●

●●●●

●●● ● ●●● ●

●●●

●● ●●

●●

● ●●

●●●● ●● ●

●●● ●●●●

●●●●●● ●

●●

● ●●● ●● ●

●● ●●● ●●● ●●●●

●●

● ●●● ●

●●

● ● ●

●●

●●

●●

●●

●●

●●●

●●

●●●

● ● ●

●● ●

●●

●●

●●●●

● ●●

●●

●●● ●

●●

●●

●●

●● ●

●●

●●●

●●

●●

●●

● ●

●●

●●

● ●●

●●

●●

● ●● ●

●● ●

●●

●●

●●●

●●

●●

●●●

●●●●

●● ●●

●● ●●

●●

● ●●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●●

●●

● ● ●●

●●

●●

●● ●

●●

●●●

●●

●●●

●●●

● ● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●●

●●●●

●●

●●

●●●

● ●●

●●

● ●

● ●

●●

●●

●●

● ●●

0

2

4

6

8

Length Width Length Width Length WidthMeasure

Value Part

PetalSepal

Page 19: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Categorical Variables> ggplot(iris.1, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point()

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

Page 20: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Categorical Variables> ggplot(iris.1, aes(x = Sepal.Length, y = Sepal.Width, shape = Species)) + geom_point()

● ●

●●

●●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

● SetosaVersicolorVirginica

Page 21: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Categorical Variables> ggplot(iris.1, aes(x = Sepal.Length, y = Sepal.Width, shape = Species, col = Species)) + geom_point()

● ●

●●

●●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

● SetosaVersicolorVirginica

Page 22: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Categorical VariablesAesthetic Description

labels Text on a plot or axes

fill Fill colour

shape Shape of point

alpha Transparency

linetype Line dash pa!ern

size Diameter of points, thickness of lines

Page 23: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Aesthetics - Categorical VariablesEfficiency in Decoding Separate Groups

HighLow

ShapeOutlines

FilledShapes

QualitativeColours

Hatching

SequentialColours

Labels

Line Width Line Type Line Colours

Image adapted from Wong, B, Nat Met, 7 (9), 2010, p665

Page 24: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!

Page 25: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

DATA VISUALIZATION WITH GGPLOT2

Modifying Aesthetics

Page 26: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Positions● identity

● dodge

● stack

● fill

● ji!er

● ji!erdodge

Page 27: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

position identity (default)> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point()

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

Page 28: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

position identity (default)> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point( )

● ●

●●

●●

●●

●●

●●

●●

● ●

● ●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

position = "identity"

Page 29: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

position ji!er> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point( )

●●

● ●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●

●● ●

● ●

● ●

●●

●●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

position = "jitter"

Page 30: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

position ji!er (2)> posn.j <- position_jitter(width = 0.1) > ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = posn.j)

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

● ●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7Sepal.Length

Sepal.W

idth Species

SetosaVersicolorVirginica

Set specific arguments for the position Consistency in ji!er across plots

Page 31: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Scale Functions● scale_x...

● scale_y...

● scale_color...

● scale_fill...

● scale_color...

● scale_shape...

● scale_linetype...

Page 32: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

Scale Functions● scale_x_continuous

● scale_y...

● scale_color_discrete

● scale_fill...

● scale_color...

● scale_shape...

● scale_linetype...

Page 33: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

scale_> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = "jitter") + scale_x_continuous("Sepal Length") + scale_color_discrete("Species")

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal Length

Sepa

l.Wid

th Species●

SetosaVersicolorVirginica

Page 34: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

limit> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = "jitter") + scale_x_continuous("Sepal Length", limits = c(2, 8)) + scale_color_discrete("Species")

● ●

●●

●●

●●

●●●

●●

●●

●●

●●

●●

●●

●● ●

●●

●●

● ●

●●

●● ●●

2.0

2.5

3.0

3.5

4.0

4.5

2 4 6 8Sepal Length

Sepa

l.Wid

th Species●

SetosaVersicolorVirginica

Page 35: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

breaks> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = "jitter") + scale_x_continuous("Sepal Length", limits = c(2, 8), breaks = seq(2, 8, 3)) + scale_color_discrete("Species")

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

● ●

●●

●● ●

●●

2.0

2.5

3.0

3.5

4.0

4.5

2 5 8Sepal Length

Sepa

l.Wid

th Species●

SetosaVersicolorVirginica

Page 36: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

expand> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = "jitter") + scale_x_continuous("Sepal Length", limits = c(2, 8), breaks = seq(2, 8, 3), expand = c(0, 0)) + scale_color_discrete("Species")

●●

● ●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

●●

● ●

●●

●●

●●

●●● ●

●●

2.0

2.5

3.0

3.5

4.0

4.5

2 5 8Sepal Length

Sepa

l.Wid

th Species●

SetosaVersicolorVirginica

Page 37: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

labels

●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

●● ●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal Length

Sepa

l.Wid

th Species●

SetosaVersicolorVirginica

> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = "jitter") + scale_x_continuous("Sepal Length", limits = c(2, 8), breaks = seq(2, 8, 3), expand = c(0, 0)) + scale_color_discrete("Species", labels = c("Setosa", "Versicolour", "Virginica")))

Page 38: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

Data Visualization with ggplot2

labs> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) + geom_point(position = "jitter") + labs(x = "Sepal Length", y = "Sepal Width", col = "Species")

●●

● ●

●●

●●

●●

● ●●

●●

●●●

●●

●●

●●

●●

●●

●●

● ●

●●

● ●

●●

●●

●●

●●

2.0

2.5

3.0

3.5

4.0

4.5

5 6 7 8Sepal Length

Sepa

l Wid

th Species●

SetosaVersicolorVirginica

Page 39: Visible Aesthetics - Amazon S3...Typical Aesthetics Aesthetic Description x X axis position y Y axis position colour Colour of dots, outlines of other shapes fill Fill colour size

DATA VISUALIZATION WITH GGPLOT2

Let’s practice!