introduction to ggplot2
play

Introduction to ggplot2 R Pruim July, 2014 Goals What I will try - PowerPoint PPT Presentation

Introduction to ggplot2 R Pruim July, 2014 Goals What I will try to do give a tour of ggplot2 explain how to think about plots the ggplot2 way prepare/encourage you to learn more later What I cant do in one session show every


  1. Introduction to ggplot2 R Pruim July, 2014

  2. Goals What I will try to do ◮ give a tour of ggplot2 ◮ explain how to think about plots the ggplot2 way ◮ prepare/encourage you to learn more later What I can’t do in one session ◮ show every bell and whistle ◮ make you an expert at using ggplot2

  3. The Births78 data set – revised edition require (dplyr) require (mosaic) require (lubridate) Births2 <- Births78 %>% mutate ( date = mdy (date) - years (100), # y2k fix wd = wday (date), # as a number wday = wday (date, label=TRUE, abbr=TRUE) # as text (abbrev) ) head (Births2, 2) ## date births dayofyear wd wday ## 1 1978-01-01 7701 1 1 Sun ## 2 1978-01-02 7527 2 2 Mon

  4. The grammar of graphics geom : the geometric “shape” used to display data (glyph) ◮ bar, point, line, ribbon, text, etc. aesthetic : an attribute controlling how geom is displayed ◮ x position, y position, color, fill, shape, size, etc. stat : a transformation applied to data before geom gets it ◮ example: histograms work on binned data scale : conversion of raw data to visual display ◮ particular assignment of colors, shapes, sizes, etc. guide : helps user convert visual data back into raw data (legends, axes)

  5. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date

  6. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What does R need to know?

  7. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What does R need to know? ◮ data source ◮ aesthetics ◮ geom – dots

  8. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What does R need to know?

  9. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What does R need to know? ◮ data frame containing the data: ggplot(data=) ggplot (data=Births2)

  10. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What does R need to know? ◮ data frame containing the data: ggplot(data=) ggplot (data=Births2) * how we want to map our aesthetics: aes() ggplot (data=Births2, aes (x=date, y=births))

  11. How do we make this plot? 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What does R need to know? ◮ data frame containing the data: ggplot(data=) ggplot (data=Births2) * how we want to map our aesthetics: aes() ggplot (data=Births2, aes (x=date, y=births)) ◮ what geom we want to use: + geom_point() ggplot (data=Births2, aes (x=date, y=births)) + geom_point ()

  12. How do we make this plot? wday 10000 Sun Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What information has changed?

  13. How do we make this plot? wday 10000 Sun Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date What information has changed? ◮ new aesthetic: mapping color to day of week ggplot (data=Births2, aes (x=date, y=births, color=wday)) + geom_point ()

  14. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date

  15. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date This time we use lines instead of dots ggplot (data=Births2, aes (x=date, y=births, color=wday)) + geom_line ()

  16. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date

  17. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date This time we have two layers , one with points and one with lines ggplot (data=Births2, aes (x=date, y=births, color=wday)) + geom_point () + geom_line ()

  18. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date This time we have two layers , one with points and one with lines ggplot (data=Births2, aes (x=date, y=births, color=wday)) + geom_point () + geom_line () ◮ The layers are placed one on top of the other: the points are below and the lines are above . Sometimes the order of the layers can be important because of overplotting.

  19. Alternative Syntax Births2 %>% ggplot ( aes (x=date, y=births, color=wday)) + geom_point () + geom_line () wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date

  20. What does this do? Births2 %>% ggplot ( aes (x=date, y=births, color="navy")) + geom_point ()

  21. What does this do? Births2 %>% ggplot ( aes (x=date, y=births, color="navy")) + geom_point () 10000 births "navy" 9000 navy 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date This is mapping the color aesthetic to a new variable with only one value (“navy”). So all the dots get set to the same color, but it’s not navy.

  22. Setting vs. Mapping If we want to set the color to be navy for all of the dots, we do it this way: Births2 %>% ggplot ( aes (x=date, y=births)) + # map these geom_point (color = "navy") # set this 10000 births 9000 8000 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date ◮ Note that color = "navy" is now outside of the aesthetics list. That’s how ggplot2 distinguishes between mapping and setting.

  23. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date

  24. How do we make this plot? wday Sun 10000 Mon births Tues 9000 Wed Thurs 8000 Fri Sat 7000 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date Births2 %>% ggplot ( aes (x=date, y=births)) + geom_line ( aes (color=wday)) + # map color here geom_point (color="navy") # set color here ◮ ggplot() establishes the default data and aesthetics for the geoms, but each geom may change these defaults. ◮ good practice: put into ggplot() the things that affect all (or most) of the layers; rest in geom_blah()

  25. Other geoms apropos ("^geom_") [1] "geom_abline" "geom_area" "geom_bar" [4] "geom_bin2d" "geom_blank" "geom_boxplot" [7] "geom_contour" "geom_crossbar" "geom_density" [10] "geom_density2d" "geom_dotplot" "geom_errorbar" [13] "geom_errorbarh" "geom_freqpoly" "geom_hex" [16] "geom_histogram" "geom_hline" "geom_jitter" [19] "geom_line" "geom_linerange" "geom_map" [22] "geom_path" "geom_point" "geom_pointrange" [25] "geom_polygon" "geom_quantile" "geom_rangeframe" [28] "geom_raster" "geom_rect" "geom_ribbon" [31] "geom_rug" "geom_segment" "geom_smooth" [34] "geom_step" "geom_text" "geom_tile" [37] "geom_tufteboxplot" "geom_violin" "geom_vline" help pages will tell you their aesthetics and default stats ?geom_area # for example

  26. Let’s try geom_area Births2 %>% ggplot ( aes (x=date, y=births, fill=wday)) + geom_area () wday 9000 Sun Mon Tues births 6000 Wed Thurs 3000 Fri Sat 0 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date This is not a good plot

  27. Let’s try geom_area Births2 %>% ggplot ( aes (x=date, y=births, fill=wday)) + geom_area () wday 9000 Sun Mon Tues births 6000 Wed Thurs 3000 Fri Sat 0 Jan 1978 Apr 1978 Jul 1978 Oct 1978 Jan 1979 date This is not a good plot ◮ overplotting is hiding much of the data ◮ extending y-axis to 0 may or may not be desirable.

  28. Side note: what makes a plot good? Most (all?) graphics are intended to help us make comparisons ◮ How does something change over time? ◮ Do my treatments matter? How much? ◮ Do men and women respond the same way? Key plot metric: Does my plot make the comparisions I am interested in ◮ easily, and ◮ accurately?

  29. Time for some different data HELPrct: Health Evaluation and Linkage to Primary care randomized clinical trial ?HELPrct

  30. Why are these people in the study? HELPrct %>% ggplot ( aes (x=substance)) + geom_bar () 150 count 100 50 0 alcohol cocaine heroin substance

  31. Why are these people in the study? HELPrct %>% ggplot ( aes (x=substance)) + geom_bar () 150 count 100 50 0 alcohol cocaine heroin substance ◮ Hmm. What’s up with y ?

  32. Why are these people in the study? HELPrct %>% ggplot ( aes (x=substance)) + geom_bar () 150 count 100 50 0 alcohol cocaine heroin substance ◮ Hmm. What’s up with y ? ◮ stat_bin() is being applied to the data before the geom_bar() gets to do its thing. Binning creates the y values.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend