csss 569 visualizing data and models
play

CSSS 569 Visualizing Data and Models Lab 3: Intro to ggplot2 Kai - PowerPoint PPT Presentation

CSSS 569 Visualizing Data and Models Lab 3: Intro to ggplot2 Kai Ping (Brian) Leung Department of Political Science, UW January 30, 2020 Introduction Lets start with some examples Introduction Belgium 80 Majoritarian Denmark


  1. CSSS 569 Visualizing Data and Models Lab 3: Intro to ggplot2 Kai Ping (Brian) Leung Department of Political Science, UW January 30, 2020

  2. Introduction ◮ Let’s start with some examples

  3. Introduction Belgium 80 Majoritarian Denmark Proportional Norway Netherlands ● Unanimity Finland % lifted from poverty by taxes & transfers Sweden 60 France Germany United Kingdom Italy Australia 40 Canada 20 Switzerland United States ● 0 2 3 4 5 6 7 Effective number of parties

  4. Introduction Predicted probability of voting Clinton Perot Bush 1.0 0.8 0.6 0.4 0.2 White Non−white 0.0 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 Ideological self−placement (from very liberal to very conservative)

  5. Introduction First difference in predicted prob. to win award −75% −50% −25% 0% 25% 50% ● winpct ● ● innings ● ● strikeout ● ● walks ● ● ● Model1 era ● ● Model2 −75% −50% −25% 0% 25% 50% First difference in predicted prob. to win award

  6. Grammar of graphics ◮ A statistical graphic is a mapping of data variables to aes thetic attributes of geom etric objects. (Wilkinson 2005)

  7. Grammar of graphics in ggplot2 ◮ What data do you want to visualize?

  8. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...)

  9. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes?

  10. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes? ◮ aes(... = ...)

  11. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes? ◮ aes(... = ...) ◮ positions ( x , y ), shape , colour , size , fill , alpha , linetype , label . . .

  12. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes? ◮ aes(... = ...) ◮ positions ( x , y ), shape , colour , size , fill , alpha , linetype , label . . . ◮ If the value of an attribute do not vary w.r.t. some variable, don’t wrap it within aes(...)

  13. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes? ◮ aes(... = ...) ◮ positions ( x , y ), shape , colour , size , fill , alpha , linetype , label . . . ◮ If the value of an attribute do not vary w.r.t. some variable, don’t wrap it within aes(...) ◮ Which geometric shapes do you use to represent the data?

  14. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes? ◮ aes(... = ...) ◮ positions ( x , y ), shape , colour , size , fill , alpha , linetype , label . . . ◮ If the value of an attribute do not vary w.r.t. some variable, don’t wrap it within aes(...) ◮ Which geometric shapes do you use to represent the data? ◮ geom_{} :

  15. Grammar of graphics in ggplot2 ◮ What data do you want to visualize? ◮ ggplot(data = ...) ◮ How are variables mapped to specific aesthetic attributes? ◮ aes(... = ...) ◮ positions ( x , y ), shape , colour , size , fill , alpha , linetype , label . . . ◮ If the value of an attribute do not vary w.r.t. some variable, don’t wrap it within aes(...) ◮ Which geometric shapes do you use to represent the data? ◮ geom_{} : ◮ geom_point , geom_line , geom_ribbon , geom_polygon , geom_label . . .

  16. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009)

  17. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation

  18. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation ◮ Use + to stack up layers

  19. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation ◮ Use + to stack up layers ◮ Within each geom_{} layer, two things are inherited from previous layers

  20. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation ◮ Use + to stack up layers ◮ Within each geom_{} layer, two things are inherited from previous layers ◮ Data: inherited from the master data

  21. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation ◮ Use + to stack up layers ◮ Within each geom_{} layer, two things are inherited from previous layers ◮ Data: inherited from the master data ◮ Aesthetics: inherited ( inherit.aes = TRUE ) from the master aesthetics

  22. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation ◮ Use + to stack up layers ◮ Within each geom_{} layer, two things are inherited from previous layers ◮ Data: inherited from the master data ◮ Aesthetics: inherited ( inherit.aes = TRUE ) from the master aesthetics ◮ They are convenient but create unintended consequences

  23. ggplot2 : A layered grammar ◮ ggplot2 : A layered grammer of graphics (Wickham 2009) ◮ Build a graphic from multiple layers; each consists of some geometric objects or transformation ◮ Use + to stack up layers ◮ Within each geom_{} layer, two things are inherited from previous layers ◮ Data: inherited from the master data ◮ Aesthetics: inherited ( inherit.aes = TRUE ) from the master aesthetics ◮ They are convenient but create unintended consequences ◮ We’ll revisit them very soon and learn how to overwrite them

  24. Tidy data ◮ ggplot2 works well only with tidy data ◮ Tidy data : ◮ Each variable must have its own column ◮ Each observation must have its own row ◮ Each value must have its own cell ◮ Example: iverRevised.csv for Homework1 ## # A tibble: 6 x 4 ## country povertyReduction effectiveParties partySystem ## <chr> <dbl> <dbl> <chr> ## 1 Australia 42.2 2.38 Majoritarian ## 2 Belgium 78.8 7.01 Proportional ## 3 Canada 29.9 1.69 Majoritarian ## 4 Denmark 71.5 5.04 Proportional ## 5 Finland 69.1 5.14 Proportional ## 6 France 57.9 2.68 Majoritarian

  25. Building a plot from scratch Belgium 80 Majoritarian Denmark Proportional Norway Netherlands ● Unanimity Finland % lifted from poverty by taxes & transfers Sweden 60 France Germany United Kingdom Italy Australia 40 Canada 20 Switzerland United States ● 0 2 3 4 5 6 7 Effective number of parties

  26. Building a plot from scratch # Load packages library (tidyverse) library (RColorBrewer) library (ggrepel) #install.packages("MASS") # Load data iver <- read_csv ("data/iverRevised.csv") # Shorten the variable names iver <- iver %>% rename (povRed = povertyReduction, effPar = effectiveParties, parSys = partySystem)

  27. Building a plot from scratch 80 ggplot ( data = iver, 60 mapping = aes (y = povRed, x = effPar) povRed ) 40 20 2 3 4 5 6 7 effPar

  28. Building a plot from scratch data =... and mapping =... can be 80 omitted for simplicity 60 ggplot ( iver, povRed aes (y = povRed, x = effPar) 40 ) 20 2 3 4 5 6 7 effPar

  29. Building a plot from scratch No data will be drawn until you supply 80 geom_{} 60 ggplot ( iver, povRed aes (y = povRed, x = effPar) 40 ) + geom_point () 20 2 3 4 5 6 7 effPar

  30. Building a plot from scratch Map variable partySystem to aesthetics 80 ggplot ( 60 iver, aes (y = povRed, x = effPar, parSys povRed Majoritarian colour = parSys, Proportional Unanimity 40 shape = parSys) ) + geom_point () 20 2 3 4 5 6 7 effPar

  31. Building a plot from scratch Why does it produce multiples smooth 80 curves? 60 ggplot ( iver, parSys povRed Majoritarian aes (y = povRed, x = effPar, Proportional 40 Unanimity colour = parSys, shape = parSys) ) + 20 geom_point () + geom_smooth (method = MASS :: rlm) 2 3 4 5 6 7 effPar

  32. Building a plot from scratch There is a hidden inherit.aes = TRUE 80 default argument in every geom_{} 60 ggplot ( iver, parSys povRed Majoritarian aes (y = povRed, x = effPar, Proportional Unanimity 40 colour = parSys, shape = parSys) ) + 20 geom_point ( inherit.aes = TRUE, 2 3 4 5 6 7 effPar aes (y = povRed, x = effPar, colour = parSys, shape = parSys) ) + geom_smooth ( inherit.aes = TRUE, aes (y = povRed, x = effPar, colour = parSys, shape = parSys), method = MASS :: rlm )

  33. Building a plot from scratch One solution: localize different aesthetic 100 settings to specific layers 75 ggplot ( iver, parSys povRed Majoritarian aes (y = povRed, x = effPar) Proportional 50 Unanimity ) + geom_point ( aes (colour = parSys, 25 shape = parSys), size = 4 2 3 4 5 6 7 effPar ) + geom_smooth (method = MASS :: rlm)

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