ggplot and the GRAMMAR OF GRAPHICS MAPPING vs SETTING AESTHETICS - - PowerPoint PPT Presentation

ggplot and the grammar of graphics mapping vs setting
SMART_READER_LITE
LIVE PREVIEW

ggplot and the GRAMMAR OF GRAPHICS MAPPING vs SETTING AESTHETICS - - PowerPoint PPT Presentation

ggplot and the GRAMMAR OF GRAPHICS MAPPING vs SETTING AESTHETICS p <- ggplot (data = gapminder, mapping = aes (x = gdpPercap, y = lifeExp, color = "purple")) p + geom_point () + geom_smooth (method = "loess") +


slide-1
SLIDE 1

ggplot and the GRAMMAR OF GRAPHICS

slide-2
SLIDE 2

MAPPING vs SETTING AESTHETICS

slide-3
SLIDE 3

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = "purple")) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()

slide-4
SLIDE 4

What has gone wrong here?

slide-5
SLIDE 5

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp) p + geom_point(color = "purple") + geom_smooth(method = "loess")) + scale_x_log10()

slide-6
SLIDE 6
slide-7
SLIDE 7

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(alpha = 0.3) + geom_smooth(color = "orange", se = FALSE, size = 2, method = "lm") + scale_x_log10()

Here, some elements are mapped to variables, while others are set to values geom functions can take many different arguments

slide-8
SLIDE 8
slide-9
SLIDE 9

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent, fill = continent)) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()

slide-10
SLIDE 10
slide-11
SLIDE 11

MAP or SET AESTHETICS per geom

slide-12
SLIDE 12

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(mapping = aes(color = continent)) + geom_smooth(method = "loess") + scale_x_log10()

slide-13
SLIDE 13
slide-14
SLIDE 14

PAY CLOSE ATTENTION TO WHICH SCALES AND GUIDES ARE DRAWN, AND WHY THAT HAPPENS

slide-15
SLIDE 15

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, color = continent, fill = continent)) p + geom_point() + geom_smooth(method = "loess") + scale_x_log10()

slide-16
SLIDE 16

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) p + geom_point(mapping = aes(color = continent)) + geom_smooth(method = "loess") + scale_x_log10()

slide-17
SLIDE 17

REMEMBER: EVERY MAPPED VARIABLE HAS A SCALE

slide-18
SLIDE 18
slide-19
SLIDE 19

IMPLEMENTS A GRAMMAR OF GRAPHICS ggplot

slide-20
SLIDE 20

The grammar is a set of rules for how produce graphics from data, taking pieces of data and mapping them to geometric objects (like points and lines) that have aesthetic attributes (like position, color and size), together with further rules for transforming the data if needed, adjusting scales, or projecting the results onto a coordinate system.

slide-21
SLIDE 21

Like other rules of syntax, the grammar limits what you can validly say, but it doesn’t make what you say sensible or meaningful.

slide-22
SLIDE 22

Grouped Data and the group aesthetic

slide-23
SLIDE 23

p <- ggplot(data = gapminder, mapping = aes(x = year, y = gdpPercap)) p + geom_line()

slide-24
SLIDE 24
slide-25
SLIDE 25
slide-26
SLIDE 26

p <- ggplot(data = gapminder, mapping = aes(x = year, y = gdpPercap)) p + geom_line(mapping = aes(group = country))

slide-27
SLIDE 27
slide-28
SLIDE 28

p <- ggplot(data = gapminder, mapping = aes(x = year, y = gdpPercap)) p + geom_line(mapping = aes(group = country)) + facet_wrap(~ continent)

A facet is not a

  • geom. It’s a way
  • f arranging geoms.

Facets use R’s ‘formula’ syntax. Read the ~ as “on” or “by”.

slide-29
SLIDE 29
slide-30
SLIDE 30

p + geom_line(color = "gray70", mapping = aes(group = country)) + geom_smooth(size = 1.1, method = "loess", se = FALSE) + scale_y_log10(labels=scales::dollar) + facet_wrap(~ continent, ncol = 5) + labs(x = "Year", y = "GDP per capita", title = "GDP per capita on Five Continents")

The labs() function lets you name labels, title, subtitle, etc.

slide-31
SLIDE 31