38
Visualization Basics Part I

Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Visualization Basics!Part I!

Page 2: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

"The simple graph has brought more information to the data analyst’s mind than any other device.”

-John Tukey

Page 3: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

A Grammar of Graphics

A small, concise language to describe the components of a graphic.

We can move beyond named graphics (e.g. scatterplot) and gain a deeper understanding.

www.gapminder.org

Page 4: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

mpg dataset

Page 5: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Question

Confer with your group.

What relationship do you expect to see between engine size (displ) and mileage (hwy)?

Page 6: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Your Turn #1

Write and run this code in your script to make a graph. Pay strict attention to spelling, capitalization, and parentheses! ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

Page 7: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

Page 8: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

1.  “Ini1alize”aplotwithggplot()2.  Addlayerswithgeom_func1ons ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

Page 9: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

data +beforenewline

typeoflayer aes() xvariable yvariable

Page 10: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

A Template ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

Page 11: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Mappings

Page 12: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))

WhydothesecarsgetbeEergasmileage?

Page 13: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Aesthetics

size shape color

Page 14: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Visual Space Data Spacecolor classredbrowngreenaquabluevioletpink

2seatercompactmidsizeminivanpickupsubcompactsuv

Page 15: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Aesthetics

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))

aesthe1cproperty

variabletomapitto

Page 16: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))

Legendaddedautoma1cally

Page 17: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Your Turn 2ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) In your script, edit the code above by adding color, size,alpha, and shape aesthetics your graph. Experiment.

Do different things happen when you map aesthetics to categorical and numerical variables?

What happens when you add more aesthetics?

Page 18: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Color

Size

Shape

Categorical Numerical

Page 19: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Set vs. Map

Page 20: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Howcanwemakethisplot?

Page 21: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))

Insidetheaes()mapsthe

aesthe1ctoavariable

Page 22: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = “blue”)

Outsidetheaes()setstheaesthe1ctoa

value

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))

Page 23: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = “blue”)

Page 24: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = “blue”))

Uh-oh!Thisshouldbe

movedoutsideaes()

Page 25: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = class)

Uh-oh!Thisshouldbe

movedinsideaes()

Errormessagescanbeweird.Inthiscase,classhasanothermeaninginRthat

leadstothiserror.

Page 26: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Geoms

Page 27: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

How are these plots similar?

Page 28: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

How are these plots similar?

The x variable, y variable, and data

Page 29: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

How are these plots different?

Page 30: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

How are these plots different?

The geometric object (geom). This is the visual object used to represent the data.

Page 31: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

geoms

ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

Page 32: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

geom_functions

Each geom_function requires a mapping

argument

Page 33: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

geom_ function aestheticsEach geom_ function has

aesthetics that can be mapped to variables, or set to values.

Some aesthetics are required. The rest are optional

{

Page 34: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Your Turn 3Withyourpartner,decidehowtoreplacethisscaEerplotwithonethatdrawsboxplots.Usethecheatsheet.Tryyourbestguess.

ggplot(data = mpg) + geom_point(mapping = aes(x = class, y = hwy))

Page 35: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_boxplot(mapping = aes(x = displ, y = hwy))

Page 36: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

Your Turn 4Withyourpartner,makethehistogramofhwybelow.Usethecheatsheet.Hint:donotsupplyayvariable.

Page 37: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_histogram(mapping = aes(x = hwy))

Page 38: Visualization Basicswebspace.ship.edu/lebryant/mat219/pdf/slides-02-vis-basics-I-part1.pdfPart I! "The simple graph has brought more ... -John Tukey. A Grammar of Graphics A small,

ggplot(data = mpg) + geom_histogram(mapping = aes(x = hwy), binwidth = 2)