class: inverse, center, middle # 36-315: Statistical Graphics and Visualization ## Lab 2 Meghan Hall <br> Department of Statistics & Data Science <br> Carnegie Mellon University <br> May 27, 2021 --- layout: true <div class="my-footer"><span>cmu-36315.netlify.app</span></div> --- # Today <br> .large[Reviewing bar charts] <br> .medium[] --- # Standard bar chart ```r got %>% ggplot(aes(x = region)) + geom_bar() ``` <br> .medium[If you don't have a variable on the `y` aesthetic, you don't need a `stat` argument] <br> .medium[If you *do* have a variable on the `y` aesthetic (whether a count, aggregate, or other data), you need the `stat = "identity"` argument within `geom_bar()`] --- # Grouped bar chart ```r got %>% ggplot(aes(x = attacker_king, fill = battle_type)) + geom_bar(position = "dodge") ``` <br> .medium[The `position = "dodge"` argument within `geom_bar()` will created a grouped bar chart, as long as you have a `fill` aesthetic] --- # Stacked bar chart ```r got %>% ggplot(aes(x = attacker_king, fill = region)) + geom_bar(position = "stack") ``` <br> .medium[The `position = "stack"` argument within `geom_bar()` will created a stacked bar chart, as long as you have a `fill` aesthetic] <br> .medium[Be careful using this chart type with a `fill` variable that has too many levels, as it's hard to compare] --- # Percent stacked bar chart ```r got %>% add_count(attacker_king) %>% ggplot(aes(x = attacker_king, y = n, fill = attacker_outcome)) + geom_bar(stat = "identity", position = "fill") ``` <br> .medium[The `position = "fill"` argument within `geom_bar()` will created a percent stacked bar chart, as long as you have a `fill` aesthetic and a summary variable on the `y` aesthetic] --- # Upcoming <br> .large[Lab assignment due 11:30am EDT Friday!] <br> .medium[Ask questions on Piazza if they don't get answered here] <br> .large[Lecture 4 on Friday May 28] <br> .medium[Histograms and box plots] <br> .large[Homework 1 due Tuesday June 1] <br> .medium[Will be posted soon]