ggplot and the grammar of graphics mapping vs setting
play

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") +


  1. ggplot and the GRAMMAR OF GRAPHICS

  2. MAPPING vs SETTING AESTHETICS

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

  4. What has gone wrong here?

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

  6. geom functions can take many di ff erent arguments 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

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

  8. MAP or SET AESTHETICS per geom

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

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

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

  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 ()

  13. REMEMBER: EVERY MAPPED VARIABLE HAS A SCALE

  14. ggplot IMPLEMENTS A GRAMMAR OF GRAPHICS

  15. 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 .

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

  17. Grouped Data and the group aesthetic

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

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

  20. 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 Facets use R’s geom. It’s a way ‘formula’ syntax. Read of arranging geoms . the ~ as “on” or “by”.

  21. 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.

Recommend


More recommend