Graphical Exploration II: ggplot

[Pages:34]Graphical Exploration II: ggplot



Exploring Data With Graphs

R Packages Used in this Chapter

ggplot2

Chapter 4

install.packages("ggplot2") library(ggplot2)

Functions in R Package ggplot2

Introduction: ggplot2 involves two separate functions ? ggplot creates more versatile graphs. ? qplot provides faster options.

qplot is the first ggplot2 function, short for quick plot.

qplot makes it easy to produce complex plots, often requiring several lines of code using other plotting systems, in one line.

qplot can do this because it is based on the grammar of graphics, which allows the user to create a simple, yet expressive, description of the plot.

Graphs with R Package ggplot2

Introduction: ggplot2 creates a series of stacked layers

Plot

ggplot creates more versatile graphs.

Layers

Graphs with R Package ggplot2

The anatomy of a graph:

Plot

(The finished product)

Layers

Geoms

(Bars, lines, text, points)

Aesthetics

(Colour, shape, size, Location)

Geometric elements: Basic graph elements (bars, lines, points, text) are known as geoms

Aesthetics elements: Control the appearance of the graph layers (color, size, shapes)

Geometric Objects in ggplot2

Geoms: (See Table 4.1 in Field Textbook) ? There is a variety of geometric objects (e.g., geom_point(), geom_line(), geom_bar(), ... ? Each geom is followed by brackets "()", indicating that they can accept aesthetics attributes.

Required aesthetics: the variables involved in the plot (others will take on the default values, unless specified)

Optional aesthetics: Over-ride default values, determine color of the geom, color to fill geom, type of line used... (See Optional Aesthetics Table 4.2 in Field Textbook)

Geometric Objects in ggplot2

Specifying aesthetics in ggplot2: ? Specific settings apply to entire figure; do not use aes() ? Variable settings defined by a changing variable; use aes()

Aesthetic

Data Colour

Size Shape

etc.

Specific

e.g., "Red"

2

Variable

e.g., gender, experimental group

Don't use aes()

e.g., colour = "Red"

linetype = 2

Use aes()

e.g., aes(colour = gender), aes(shape = group)

Layer/ Geom Plot

Anatomy of ggplot() function

1) Create object that specifies the plot. You can set any aesthetic properties that apply to all layers (geoms) in plot.

myGraph ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download