class: inverse, center, middle # 36-315: Statistical Graphics and Visualization ## Lab 5 Meghan Hall <br> Department of Statistics & Data Science <br> Carnegie Mellon University <br> June 8, 2021 --- layout: true <div class="my-footer"><span>cmu-36315.netlify.app</span></div> --- # Today <br> .large[Heat maps, dot plots, pie charts, choropleth maps] <br> .medium[joining data] <br> .medium[data aggregations] <br> <br> <br> <br> .large[Instructions] <br> .medium[add plot title and relevant axis titles (include units!)] <br> .medium[problem 5] --- # The power of `group_by` **very familiar with the `group_by`/`summarize` combination**<br> ```r penguins %>% group_by(species) %>% summarize(max_weight = max(body_mass_g, na.rm = TRUE)) ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> species </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> max_weight </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:right;"> 4775 </td> </tr> <tr> <td style="text-align:left;"> Chinstrap </td> <td style="text-align:right;"> 4800 </td> </tr> <tr> <td style="text-align:left;"> Gentoo </td> <td style="text-align:right;"> 6300 </td> </tr> </tbody> </table> --- # The power of `group_by` **also useful for filtering**<br> ```r penguins %>% group_by(species) %>% filter(body_mass_g == max(body_mass_g, na.rm = TRUE)) ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> species </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> island </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> bill_length_mm </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> bill_depth_mm </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> flipper_length_mm </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> body_mass_g </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> sex </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> year </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Adelie </td> <td style="text-align:left;"> Biscoe </td> <td style="text-align:right;"> 43.2 </td> <td style="text-align:right;"> 19.0 </td> <td style="text-align:right;"> 197 </td> <td style="text-align:right;"> 4775 </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 2009 </td> </tr> <tr> <td style="text-align:left;"> Gentoo </td> <td style="text-align:left;"> Biscoe </td> <td style="text-align:right;"> 49.2 </td> <td style="text-align:right;"> 15.2 </td> <td style="text-align:right;"> 221 </td> <td style="text-align:right;"> 6300 </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 2007 </td> </tr> <tr> <td style="text-align:left;"> Chinstrap </td> <td style="text-align:left;"> Dream </td> <td style="text-align:right;"> 52.0 </td> <td style="text-align:right;"> 20.7 </td> <td style="text-align:right;"> 210 </td> <td style="text-align:right;"> 4800 </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 2008 </td> </tr> </tbody> </table> --- # `n_distinct` **another useful function within `summarize`**<br> ```r storms %>% count(status) ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> status </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> hurricane </td> <td style="text-align:right;"> 3091 </td> </tr> <tr> <td style="text-align:left;"> tropical depression </td> <td style="text-align:right;"> 2545 </td> </tr> <tr> <td style="text-align:left;"> tropical storm </td> <td style="text-align:right;"> 4374 </td> </tr> </tbody> </table> <br> -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> name </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> year </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> month </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> day </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> hour </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> lat </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> long </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> status </th> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> category </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> wind </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> pressure </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> ts_diameter </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> hu_diameter </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Amy </td> <td style="text-align:right;"> 1975 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 27.5 </td> <td style="text-align:right;"> -79 </td> <td style="text-align:left;"> tropical depression </td> <td style="text-align:left;"> -1 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:right;"> 1013 </td> <td style="text-align:right;"> </td> <td style="text-align:right;"> </td> </tr> <tr> <td style="text-align:left;"> Amy </td> <td style="text-align:right;"> 1975 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 28.5 </td> <td style="text-align:right;"> -79 </td> <td style="text-align:left;"> tropical depression </td> <td style="text-align:left;"> -1 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:right;"> 1013 </td> <td style="text-align:right;"> </td> <td style="text-align:right;"> </td> </tr> <tr> <td style="text-align:left;"> Amy </td> <td style="text-align:right;"> 1975 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 27 </td> <td style="text-align:right;"> 12 </td> <td style="text-align:right;"> 29.5 </td> <td style="text-align:right;"> -79 </td> <td style="text-align:left;"> tropical depression </td> <td style="text-align:left;"> -1 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:right;"> 1013 </td> <td style="text-align:right;"> </td> <td style="text-align:right;"> </td> </tr> </tbody> </table> --- # `n_distinct` **another useful function within `summarize`**<br> ```r storms %>% group_by(status) %>% summarize(n = n_distinct(name, year)) ``` -- <table class="table" style="font-size: 16px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> status </th> <th style="text-align:right;font-weight: bold;color: white !important;background-color: #bb0000 !important;"> n </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> hurricane </td> <td style="text-align:right;"> 208 </td> </tr> <tr> <td style="text-align:left;"> tropical depression </td> <td style="text-align:right;"> 396 </td> </tr> <tr> <td style="text-align:left;"> tropical storm </td> <td style="text-align:right;"> 379 </td> </tr> </tbody> </table> --- # Upcoming <br> .large[Lab assignment due 11:30am EDT Wednesday!] <br> .medium[Ask questions on Piazza if they don't get answered here] <br> .large[Lecture 8 on Wednesday June 9] <br> .medium[Elevating plots to the next level] <br> .large[Graphic critique]