\[ %% % Add your macros here; they'll be included in pdf and html output. %% \newcommand{\R}{\mathbb{R}} % reals \newcommand{\E}{\mathbb{E}} % expectation \renewcommand{\P}{\mathbb{P}} % probability \DeclareMathOperator{\logit}{logit} \DeclareMathOperator{\logistic}{logistic} \DeclareMathOperator{\SE}{SE} \DeclareMathOperator{\sd}{sd} \DeclareMathOperator{\var}{var} \DeclareMathOperator{\cov}{cov} \DeclareMathOperator{\cor}{cor} \DeclareMathOperator{\Normal}{Normal} \DeclareMathOperator{\MVN}{MVN} \DeclareMathOperator{\LogNormal}{logNormal} \DeclareMathOperator{\Poisson}{Poisson} \DeclareMathOperator{\Beta}{Beta} \DeclareMathOperator{\Binom}{Binomial} \DeclareMathOperator{\Gam}{Gamma} \DeclareMathOperator{\Exp}{Exponential} \DeclareMathOperator{\Cauchy}{Cauchy} \DeclareMathOperator{\Unif}{Unif} \DeclareMathOperator{\Dirichlet}{Dirichlet} \DeclareMathOperator{\Wishart}{Wishart} \DeclareMathOperator{\StudentsT}{StudentsT} \DeclareMathOperator{\Weibull}{Weibull} \newcommand{\given}{\;\vert\;} \]

Visualization of data

Peter Ralph

Advanced Biological Statistics

Visualization

Goals

  • pattern discovery

  • efficient summary of information

  • visual/spatial analogy for quantitative patterns

aim to maximize information and minimize ink

paraphrased from Edward Tufte

Considerations

  • Is the visual analogy appropriate for the type of data?

counts? quantities? multivariate? relationships?

  • Are important comparisons clear?

between groups? differences? time trend?

  • Are units easily interpretable?

meters? dollars? percent? relative change? is it isometric?

plot of chunk r plots

Principles of effective display

  • Show the data

  • Encourage the eye to compare differences

  • Represent magnitudes honestly and accurately

  • Draw graphical elements clearly, minimizing clutter

  • Make displays easy to interpret

Above all else show the data.

Tufte 1983

honeybees

Think about what you want to communicate

cumulative COVID test numbers

Broman’s bad graphs 1

from Roeder K (1994), Statistical Science 9:222-278, Figure 4 via Karl Broman

Case study:

Distributions of litter sizes by Order, and Family, in the PanTHERIA dataset:

source("../Datasets/PanTHERIA/read_pantheria.R")
pantheria <- read_pantheria("../Datasets/PanTHERIA")
# look at most common orders
order_nums <- sort(table(pantheria$Order))
## 
##   Microbiotheria    Tubulidentata       Dermoptera Notoryctemorphia      Proboscidea       Hyracoidea      Monotremata          Sirenia Paucituberculata        Pholidota           Pilosa    Macroscelidea 
##                1                1                2                2                3                4                5                5                6                8               10               15 
##   Perissodactyla       Scandentia        Cingulata  Peramelemorphia   Erinaceomorpha     Afrosoricida   Dasyuromorphia          Cetacea  Didelphimorphia       Lagomorpha    Diprotodontia     Artiodactyla 
##               17               20               21               21               24               51               71               84               87               92              143              240 
##        Carnivora         Primates     Soricomorpha       Chiroptera         Rodentia 
##              286              376              428             1116             2277

note the pipe

px <- (pantheria %>% filter(Order %in% big_orders)
       %>% filter(!is.na(LitterSize))
       %>% select(Order, Family, Genus, Species, LitterSize))
for (xn in c("Order", "Family", "Genus")) px[[xn]] <- factor(px[[xn]])
summary(px)
##           Order                  Family              Genus        Species            LitterSize    
##  Artiodactyla:178   Muridae         : 242   Microtus    :  38   Length:2060        Min.   : 0.960  
##  Carnivora   :209   Cricetidae      : 239   Myotis      :  38   Class :character   1st Qu.: 1.000  
##  Chiroptera  :465   Sciuridae       : 158   Crocidura   :  36   Mode  :character   Median : 1.970  
##  Primates    :209   Vespertilionidae: 135   Peromyscus  :  32                      Mean   : 2.489  
##  Rodentia    :883   Bovidae         : 110   Sorex       :  32                      3rd Qu.: 3.490  
##  Soricomorpha:116   Phyllostomidae  : 106   Spermophilus:  31                      Max.   :11.300  
##                     (Other)         :1070   (Other)     :1853

Raw numbers

px$LitterSize
##    [1]  0.98  4.50  3.74  5.72  4.98  1.22  1.00  1.22  1.01  1.02  1.02  1.02  1.02  1.39  1.39  3.89  2.00  1.09  1.73  1.45  1.11  1.02  1.01  1.01  1.00  1.01  1.02  1.02  1.01  1.01  1.05  1.84  2.00  2.00
##   [35]  2.00  2.31  2.00  2.00  1.93  1.04  1.94  3.00  2.19  1.94  2.36  2.62  3.00  1.29  4.91  5.30  4.29  6.27  4.00  4.99  1.50  1.50  2.00  3.40  1.01  1.02  1.01  2.00  1.22  1.00  1.73  1.00  0.99  1.00
##   [69]  7.29  6.12  1.00  1.00  1.00  1.00  1.00  1.01  1.01  1.01  1.02  1.01  1.02  1.01  1.02  1.01  1.02  1.02  1.02  1.00  1.00  1.00  1.02  1.00  1.02  1.01  1.02  1.02  1.01  1.02  3.09  1.09  1.00  1.00
##  [103]  0.99  1.00  0.99  2.91  3.68  4.00  3.45  5.28  5.34  5.53  3.46  3.40  3.50  6.06  5.71  5.20  7.14  3.99  0.99  2.99  2.99  3.00  3.59  4.29  1.99  3.37  1.99  1.73  3.74  3.00  2.99  3.89  3.00  2.44
##  [137]  4.04  3.99  3.49  1.22  1.50  4.86  1.00  1.00  1.00  1.01  1.50  2.50  2.29  2.12  0.96  0.98  1.00  3.40  5.39  3.95  2.62  2.52  1.21  0.98  1.00  0.98  1.02  1.00  1.00  1.00  1.00  1.34  1.41  1.38
##  [171]  3.49  3.97  3.94  2.95  3.45  4.62  1.70  2.75  3.63  2.60  2.62  1.00  1.00  3.78  3.49  3.85  2.79  2.50  1.97  0.99  4.99  1.18  4.34  3.88  6.24  4.99  4.99  3.46  4.66  3.67  1.00  0.99  1.00 11.30
##  [205]  2.83  2.75  3.06  1.93  1.01  4.00  1.00  1.00  1.53  1.10  1.04  1.11  1.01  0.96  1.29  2.60  2.19  3.54  2.25  2.50  1.00  1.00  1.00  1.50  1.22  1.22  1.00  1.00  1.39  4.14  3.00  1.73  1.00  3.00
##  [239]  2.00  3.49  2.89  1.71  0.96  1.00  3.49  4.00  1.00  3.88  1.50  1.00  1.00  1.00  1.22  1.01  4.37  0.97  2.50  1.87  5.83  3.42  1.94  1.00  0.99  0.98  0.99  0.98  1.65  1.01  1.00  2.68  2.15  1.07
##  [273]  1.01  1.02  1.02  1.02  1.00  1.50  1.00  1.79  2.00  1.00  1.22  2.00  2.30  1.00  0.98  0.99  1.00  0.98  1.94  0.98  3.60  2.95  2.47  1.50  2.14  1.34  3.45  1.90  1.01  1.05  1.01  1.00  1.01  0.98
##  [307]  0.99  1.00  3.14  3.49  1.66  3.29  3.36  2.99  1.99  3.94  3.00  3.99  2.49  2.99  2.00  0.98  2.26  2.04  0.98  0.98  0.99  0.99  2.00  2.31  0.99  0.98  1.00  0.98  1.00  1.01  1.00  1.00  0.99  1.02
##  [341]  1.97  5.39  3.49  3.60  3.49  2.00  1.00  2.00  1.40  1.50  1.25  0.99  1.00  0.99  1.00  4.50  3.00  0.97  1.94  0.98  1.00  0.99  1.00  2.34  1.94  1.94  1.94  5.23  0.97  3.11  2.00  1.99  3.49  5.99
##  [375]  1.91  4.00  4.29  1.07  3.00  1.84  2.48  2.60  3.00  2.98  2.99  2.99  4.55  2.00  2.00  2.00  3.00  4.34  2.50  1.01  1.00  4.30  2.15  2.30  0.98  0.99  1.00  4.48  5.40  4.43  4.18  4.89  0.99  1.50
##  [409]  1.00  1.00  1.00  0.98  1.41  2.67  1.01  3.19  4.68  2.60  2.60  1.94  3.36  2.91  3.11  4.77  2.39  2.37  1.94  2.14  2.95  3.78  2.59  2.67  2.68  1.73  3.49  0.99  0.99  0.99  0.99  0.98  0.98  1.00
##  [443]  1.75  1.94  4.00  1.00  3.68  4.18  3.24  3.00  1.94  0.99  1.22  1.08  2.14  1.00  1.22  0.96  3.74  7.50  1.00  1.00  1.80  4.77  3.42  2.00  1.00  0.99  0.98  0.99  0.99  0.98  1.00  1.46  1.86  1.85
##  [477]  1.50  2.76  1.73  1.04  4.71  1.89  2.53  3.14  3.09  1.68  4.00  5.94  3.40  3.92  4.06  2.74  4.00  4.50  4.44  5.26  3.94  3.64  4.40  3.87  3.89  8.00  1.00  1.00  0.98  0.99  1.04  2.46  1.94  2.94
##  [511]  4.83  4.12  1.78  4.99  5.17  4.99  2.84  1.94  1.90  0.96  1.01  1.50  1.00  0.98  1.00  1.10  2.46  3.00  2.21  2.50  3.00  3.59  1.00  1.00  2.43  2.62  2.95  2.00  2.64  1.32  2.44  0.98  1.80  0.97
##  [545]  1.26  1.00  1.29  1.29  0.99  1.60  1.50  4.00  4.86  3.16  4.65  3.30  1.29  3.00  3.13  1.12  4.00  5.17  1.00  0.98  0.98  0.98  1.00  1.00  0.98  4.83  1.05  1.97  6.97  4.99  3.16  4.41  4.99  3.00
##  [579]  3.11  2.90  3.11  2.43  3.88  3.24  2.41  4.00  1.00  0.98  2.91  2.00  2.91  3.49  1.73  1.41  1.00  1.01  3.49  2.00  1.00  1.00  1.00  1.00  1.50  0.99  0.99  0.99  0.98  0.99  1.00  1.00  1.00  0.99
##  [613]  1.00  0.99  0.98  0.98  1.00  0.99  1.00  0.98  1.00  0.98  1.00  1.39  3.88  3.12  1.00  2.10  2.30  2.44  2.72  2.30  2.00  3.49  4.99  3.51  3.00  0.96  1.22  1.02  1.01  1.01  2.73  2.95  3.41  0.98
##  [647]  1.94  1.77  3.12  1.96  2.14  2.97  2.68  0.99  5.11  1.00  1.02  1.00  4.00  4.18  3.00  4.61  3.11  0.99  2.23  2.00  1.07  1.00  2.50  2.50  1.26  1.40  2.00  1.51  1.41  1.73  1.73  2.35  1.94  2.00
##  [681]  2.30  0.99  0.97  1.00  1.01  2.64  3.85  0.97  1.00  1.00  0.98  0.98  1.00  0.98  1.52  1.00  1.00  1.00  1.00  1.00  1.00  5.64  5.83  5.99  5.84  4.04  5.80  5.01  7.48  1.87  2.00  1.00  0.98  1.03
##  [715]  1.00  1.00  1.00  1.45  3.11  2.35  1.01  1.00  1.00  1.71  3.25  3.26  3.78  0.98  0.99  0.99  0.99  0.99  0.99  1.00  0.99  1.00  1.22  1.00  1.00  1.00  0.98  0.99  1.00  0.99  1.50  1.20  1.20  1.74
##  [749]  0.98  2.68  0.99  1.00  5.99  6.20  4.67  4.99  3.65  4.66  4.00  2.73  4.10  4.00  4.50  5.10  6.39  6.86  4.61  3.68  4.64  1.18  2.91  2.04  1.94  1.66  1.50  1.94  3.33  1.01  1.01  1.00  1.02  1.00
##  [783]  2.95  2.35  1.88  1.00  0.99  1.00  0.99  0.98  3.20  4.22  3.67  3.69  0.98  0.99  1.00  0.96  0.98  0.98  0.98  0.98  2.67  2.54  3.69  1.58  1.01  0.98  1.00  0.99  2.30  1.00  1.55  1.94  3.00  1.97
##  [817]  2.75  1.44  4.16  2.91  2.01  3.24  4.88  3.09  3.00  8.10  2.73  2.30  2.50  2.76  1.01  1.00  1.00  1.02  1.01  1.01  1.01  1.01  1.00  1.01  1.01  1.01  1.02  1.01  1.00  4.10  4.54  1.75  1.00  8.79
##  [851]  9.01  8.99  0.97  0.99  0.99  1.00  1.00  2.50  1.00  0.98  0.98  0.98  1.00  1.00  2.50  1.00  2.46  2.91  4.04  1.02  1.07  1.02  2.68  1.69  1.01  1.02  3.59  2.60  3.53  3.78  3.49  3.02  3.13  2.04
##  [885]  1.41  7.96 11.23  9.23  2.91  3.29  3.54  3.00  1.22  1.50  1.00  1.22  1.00  1.41  2.60  2.00  2.00  2.50  2.50  2.20  2.15  2.50  2.17  0.98  1.54  4.68  5.70  4.21  2.09  3.60  6.80  2.33  5.49  3.89
##  [919]  5.54  4.30  3.00  1.20  3.87  5.62  3.32  5.16  2.47  1.40  5.93  7.89  5.51  2.46  2.91  4.32  5.06  2.10  8.10  2.30  5.72  0.98  0.98  0.99  0.99  1.00  0.99  1.00  1.00  1.00  0.99  1.22  1.00  0.98
##  [953]  1.00  5.18  4.50  1.98  3.49  2.48  3.16  3.16  2.00  2.00  2.52  3.94  3.89  4.92  0.99  0.98  0.99  0.99  3.00  4.65  4.99  4.50  4.41  4.62  3.58  0.98  0.98  0.99  0.99  0.99  0.99  0.99  0.99  0.99
##  [987]  1.44  1.42  2.00  0.99  3.00  1.50  1.00  1.84  2.51  5.34  8.48  6.14  1.97  4.73  1.37  5.96  4.40  4.76  2.95  4.18  3.49  5.54  6.93  3.92  6.69  5.15  1.94  4.40  4.30  5.44  6.74  7.82  6.50  4.50
## [1021]  3.36  5.07  4.00  8.48  6.48  4.04  5.34  3.10  4.83  5.37  4.31  5.01  5.60  4.10  1.00  1.97  1.77  5.25  1.97  2.99  2.99  4.00  2.90  4.50  3.11  1.00  0.98  1.00  1.88  1.00  0.98  0.99  0.99  0.99
## [1055]  0.98  1.00  1.21  0.98  0.99  0.98  0.98  1.00  0.99  0.99  0.98  0.99  0.99  1.00  1.00  1.00  1.50  0.99  0.98  0.98  1.00  1.00  1.00  0.98  4.50  1.02  4.00  3.69  0.99  3.94  4.50  4.86  3.52  2.25
## [1089]  2.25  2.33  7.10  6.39  1.35  0.98  1.96  1.98  1.86  2.00  2.14  0.97  3.45  3.11  2.79  1.29  2.70  2.00  2.59  2.00  1.00  4.00  2.50  2.95  2.81  0.98  3.30  4.95  3.64  4.77  3.59  0.96  6.55  3.72
## [1123]  3.88  4.08  2.50  2.00  1.06  3.20  1.00  1.00  1.00  4.18  2.99  1.94  2.37  3.70  4.86  4.41  0.99  1.01  3.69  4.01  2.95  3.07  3.28  0.99  1.00  1.91  1.82  1.73  1.33  6.36  0.98  1.00  0.99  1.00
## [1157]  0.99  1.00  1.00  1.12  1.75  2.54  1.94  0.98  0.98  0.98  1.00  0.99  1.00  0.99  0.99  0.99  2.00  2.00  1.54  2.62  5.31  2.04  1.22  1.61  1.57  3.15  3.84  3.30  1.40  1.00  0.98  1.97  1.00  1.00
## [1191]  1.00  3.40  3.59  3.23  3.63  4.37  1.68  1.00  4.00  1.14  1.02  2.10  0.99  1.00  2.86  1.59  1.65  3.70  1.29  1.70  1.38  0.99  2.20  0.98  1.01  1.22  1.19  1.16  1.22  1.00  2.51  1.00  3.49  1.00
## [1225]  2.16  1.01  1.05  2.75  1.96  2.14  2.60  8.47  6.28  5.48  1.00  0.98  1.00  1.01  1.01  1.01  1.01  1.01  0.97  2.00  3.40  3.29  3.00  2.50  1.52  1.07  1.69  2.15  1.94  2.00  0.98  4.49  0.97  1.06
## [1259]  2.02  1.22  2.20  1.60  1.50  2.00  2.04  2.91  1.56  1.17  0.98  1.00  1.09  5.48  4.37  3.94  4.00  4.45  4.18  4.86  3.53  3.49  3.18  1.94  3.08  3.88  2.52  1.94  3.70  2.43  1.94  3.09  2.39  5.99
## [1293]  4.27  0.99  0.98  1.21  2.12  1.00  1.00  0.98  2.91  4.76  2.00  2.23  2.91  3.69  2.75  3.40  2.56  4.86  3.88  2.79  3.59  2.38  3.49  3.00  1.94  1.00  0.99  1.00  0.97  1.00  2.00  1.07  1.50  2.00
## [1327]  1.50  2.46  3.20  1.94  1.01  4.66  4.80  1.00  0.98  1.00  1.00  1.00  1.00  0.99  0.98  0.98  0.99  0.99  0.99  2.79  2.16  2.84  2.25  1.00  3.40  0.99  1.00  1.01  1.02  1.00  1.00  2.50  1.28  0.99
## [1361]  0.98  1.00  1.00  0.99  1.98  1.40  4.59  2.00  0.99  1.07  0.99  6.04  5.74  6.49  4.99  4.19  4.02  4.25  1.00  0.98  1.00  1.01  1.02  1.01  1.01  2.10  1.98  1.40  1.86  2.29  1.21  1.96  1.73  1.37
## [1395]  1.96  1.96  0.98  1.96  1.92  2.00  1.02  1.01  1.14  0.98  1.00  0.98  0.99  0.99  0.98  0.99  0.98  1.22  2.60  2.23  2.15  2.15  2.68  2.81  2.64  2.23  1.07  2.73  3.00  1.11  5.44  3.88  3.75  3.49
## [1429]  3.67  3.28  3.36  1.01  1.01  1.02  1.01  2.50  2.23  2.00  2.30  2.00  1.41  1.00  1.02  2.62  3.06  2.64  2.68  1.47  1.00  3.49  0.97  1.22  3.89  4.90  3.00  3.19  3.00  3.49  2.91  2.00  2.62  3.00
## [1463]  4.43  3.00  3.00  1.00  1.00  1.00  1.00  1.00  0.98  1.00  0.98  0.99  1.00  1.00  1.00  1.00  1.00  0.99  1.00  1.00  1.22  2.76  2.50  1.94  1.00  1.00  1.00  4.00  3.80  1.00  1.01  1.00  5.17  3.35
## [1497]  5.49  0.98  3.00  3.00  0.98  1.00  1.01  1.22  0.98  1.00  1.00  1.01  1.00  1.00  1.00  1.00  0.99  1.00  6.75  5.75  2.00  1.83  2.00  0.98  1.00 10.00  9.85  3.70  4.58  3.45  4.45  6.40  2.47  2.15
## [1531]  3.09  8.99  5.37  4.37  4.23  5.88  2.15  5.99  3.65  3.87  5.34  2.15  2.15  1.50  3.49  0.98  1.00  3.40  3.40  3.40  2.21  3.88  3.76  3.11  3.88  2.99  4.18  2.91  3.94  3.88  3.49  5.28  0.97  0.97
## [1565]  1.00  0.99  0.98  0.99  1.00  1.96  1.00  0.98  0.99  0.99  1.00  1.00  0.99  1.00  0.99  0.98  0.98  1.00  1.00  0.99  0.99  0.98  0.98  1.00  0.98  0.98  1.00  1.00  1.00  0.99  1.00  1.39  1.97  1.84
## [1599]  2.00  2.02  1.93  1.82  1.90  1.50  1.02  3.24  2.91  3.80  4.50  3.88  5.40  6.71  5.71  2.69  7.52  3.91  8.70  5.24  5.84  7.50  6.48  3.54  5.62  5.75  1.01  1.00  1.02  1.02  3.00  2.59  3.89  3.49
## [1633]  3.24  3.63  1.94  3.20  3.00  4.00  3.09  1.94  2.98  2.66  2.91  1.90  2.65  2.43  2.59  4.99  4.50  3.00  1.00  2.46  3.08  1.96  0.98  0.99  2.00  2.20  1.92  1.50  1.38  1.50  2.00  1.41  1.08  1.85
## [1667]  2.90  6.90  1.02  5.29  3.49  4.37  5.18  7.50  5.44  3.00  1.02  1.50  1.64  5.95  6.56  6.69  7.24  5.49  6.49  4.94  4.99  5.99  4.18  5.49  5.33  4.58  4.89  7.00  5.84  7.09  3.99  5.99  8.99  5.46
## [1701]  4.89  3.14  0.98  1.07  0.99  7.77  8.69  5.99  5.18  4.34  5.91  6.50  7.97  6.50  6.07  7.08  4.89  7.59  6.94  5.49  6.32  8.49  8.08  7.77  4.93  7.88  4.99  0.97  1.00  3.09  2.05  3.00  3.56  1.62
## [1735]  1.70  5.07  4.00  4.62  3.96  4.41  0.99  2.38  0.99  0.99  0.98  0.99  0.99  0.99  0.98  0.98  0.98  0.98  3.49  4.00  2.99  1.89  3.10  3.49  1.00  1.00  1.00  1.00  0.98  0.96  1.44  1.00  1.01  1.02
## [1769]  1.01  0.98  1.00  2.43  2.50  2.71  2.29  3.23  2.46  3.11  1.41  1.49  3.86  6.59  4.58  3.24  6.24  3.66  4.52  5.47  1.00  0.98  0.98  1.78  2.69  1.99  1.02  4.27  3.09  1.08  1.00  1.40  1.41  0.99
## [1803]  1.00  1.11  0.98  4.37  4.59  3.49  3.89  4.50  4.99  2.43  3.88  4.89  4.99  2.38  4.50  4.95  3.49  3.93  4.04  5.93  3.92  4.85  4.37  5.01  4.00  4.14  3.75  6.80  5.49  3.93  2.59  2.38  3.40  1.00
## [1837]  1.00  0.99  1.00  0.99  1.00  0.99  1.02  1.01  1.00  4.86  2.19  3.70  5.34  0.98  1.00  0.98  3.28  4.59  4.86  3.89  3.77  1.00  0.98  1.25  1.00  2.52  4.47  4.99  3.49  3.93  3.44  5.15  1.01  1.00
## [1871]  5.19  4.99  1.01  1.02  1.01  1.42  4.57  5.44  7.77  6.90  0.98  1.00  6.32  9.36  8.00  8.90  1.22  0.99  2.91  0.98  1.00  2.50  0.97  0.99  1.22  0.99  2.82  1.00  1.93  1.22  1.78  2.62  1.47  1.00
## [1905]  1.01  1.02  1.06  1.02  2.46  5.64  4.94  3.88  4.41  3.56  4.47  5.16  5.72  1.00  2.73  2.91  2.00  2.50  1.01  1.00  1.00  1.00  1.00  1.00  1.00  1.00  1.40  2.79  3.00  0.99  0.98  0.99  0.99  0.98
## [1939]  0.98  1.00  1.00  0.98  0.99  0.99  1.00  4.86  4.76  3.21  0.99  1.00  4.99  3.76  4.00  4.47  1.00  2.76  2.00  1.65  3.20  1.33  1.01  4.61  4.99  6.50  3.79  3.49  4.86  6.15  2.20  0.98  3.20  2.91
## [1973]  3.88  0.98  0.98  0.98  1.01  1.50  1.02  1.02  1.01  1.00  1.01  1.00  1.00  1.00  1.00  2.00  3.00  1.37  0.97  1.00  1.64  4.24  2.47  2.91  1.92  2.00  2.12  4.07  3.71  2.17  0.99  0.98  4.30  1.46
## [2007]  3.00  3.49  2.39  2.24  1.66  1.50  0.98  0.98  0.99  0.98  0.98  1.00  4.00  3.24  2.16  1.50  1.21  1.00  1.00  1.50  2.00  1.00  1.92  2.89  3.74  5.07  3.49  2.00  2.50  5.62  3.49  7.65  4.50  4.00
## [2041]  2.12  4.25  4.59  2.36  3.80  1.60  2.00  3.45  2.06  2.00  1.94  1.41  5.36  5.04  5.47  5.75  4.99  4.23  2.76  2.23

five(-ish) number summary

summary(px$LitterSize)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.960   1.000   1.970   2.489   3.490  11.300

Points

plot(px$LitterSize, xlab='', ylab='Litter size', pch=20, cex=0.5)

plot of chunk r points

Points, sorted

plot(sort(px$LitterSize), xlab='rank', ylab='Litter size', pch=20, cex=0.5)

plot of chunk r points2

Points, sorted and colored

plot(sort(px$LitterSize), col=px$Order, xlab='rank', ylab='Litter size', pch=20, cex=0.5)
legend("topleft", pch=20, col=1:nlevels(px$Order), legend=levels(px$Order))

plot of chunk r points3

Histogram

layout(t(1:2))
with(px, hist(LitterSize))
with(px, hist(LitterSize, breaks=40))

plot of chunk r hist

Histograms

layout(matrix(1:6, ncol=3, byrow=TRUE), heights=c(1, 1.2))
opar <- par(mar=c(1, 4, 1, 1)+.1)
xh <- hist(px$LitterSize, plot=FALSE, breaks=30)
for (k in 1:nlevels(px$Order)) {
    ord <- levels(px$Order)[k]
    if (k == 4) par(opar)
    with(subset(px, Order == ord), 
        hist(LitterSize, xlim=c(0, max(px$LitterSize)),
             breaks=xh$breaks, main=ord,
             xaxt=if (k > 3) 's' else 'n',
             xlab=if (k > 3) 'litter size' else '') )
}

Histograms

plot of chunk r many_hist

Overlaid histograms

overlay_hist <- function (x, f, breaks=30, ...) {
    xh <- hist(x, breaks=breaks, plot=FALSE)
    ymax <- do.call(max, with(px, lapply(tapply(LitterSize, Order, hist, plot=FALSE), "[[", "counts")))
    for (k in 1:nlevels(f)) {
        hist(x[f==levels(f)[k]], breaks=xh$breaks, ...,
             add=(k>1), col=adjustcolor(k, 0.4), ylim=c(0, ymax))
    }
    legend("topright", fill=adjustcolor(1:nlevels(f), 0.4),
           legend=levels(f))
}

Overlaid histograms

with(px, overlay_hist(LitterSize, Order, xlab='litter size', main=''))

plot of chunk r do_stacked_hists

boxplots

with(px, boxplot(LitterSize ~ Order))

plot of chunk r boxplot

introduced by Mary Eleanor Spear

Many boxes

par(mar=c(9, 3, 1, 1) + 0.1)
with(px, boxplot(LitterSize ~ Family, las=2, xlab=''))

plot of chunk r boxplot3

par(mar=c(9, 4, 1, 1) + 0.1)
famsize <- aggregate(LitterSize ~ Order + Family, data=px, mean)
famorder <- rank(with(famsize, LitterSize + 100 * as.numeric(Order)))
with(px, boxplot(LitterSize ~ Family, las=2, xlab='',
                 col=as.numeric(famsize$Order),
                 at=famorder))
text(x=tapply(famorder, famsize$Order, mean),
     y=10, label=levels(famsize$Order))

plot of chunk r boxplot4

Your turn

Challenge: visualize LitterSize by TeatNumber, using a boxplot.

The Grammar of Graphics

or, “gg

Ingredients of a visualization

  • data

  • coordinate axes

  • a geometric representation of numbers

  • a mapping from (summaries of) variables to properties of the geoms

  • maybe more plots

basic template

ggplot(data = <DATA>) + 
  <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>))

more options

ggplot(data = <DATA>) + 
  <GEOM_FUNCTION>(
     mapping = aes(<MAPPINGS>),
     stat = <STAT>, 
     position = <POSITION>
  ) +
  <COORDINATE_FUNCTION> +
  <FACET_FUNCTION>

Reference: the ggplot2 book.

Points

ggplot(px, mapping=aes(x=Order, y=LitterSize)) + geom_point()

plot of chunk r ggpoints

(ggplot(px,
        mapping=aes(x=1:nrow(px),
                    y=LitterSize,
                    col=Order))
 + xlab("")
 + geom_point())

plot of chunk r ggplot

(ggplot(px,
        mapping=aes(x=rank(LitterSize,
                           ties.method='first'),
                    y=LitterSize,
                    col=Order))
 + xlab("rank")
 + geom_point())

plot of chunk r ggplot2

Histogram

ggplot(px, mapping=aes(LitterSize)) + geom_histogram(bins=40) + facet_wrap(~ Order)

plot of chunk r gghist

Histogram, stacked

ggplot(px, mapping=aes(LitterSize, fill=Order)) + geom_histogram(bins=40)

plot of chunk r gghist2

Boxplot

ggplot(px, mapping=aes(y=LitterSize, x=Order)) + geom_boxplot()

plot of chunk r boxplot2

Your turn, again

Challenge: make this plot.

plot of chunk r fancyplot

The cheatsheet might be helpful.

// reveal.js plugins