class: center, middle, inverse, title-slide .title[ # Plotting a categorical x and numeric y ] --- <style type="text/css"> .left-column, .right-column { display: inline-block; vertical-align: top; width: 46%; padding: 0 1%; } .left-column pre { font-size: 85%; max-height: 400px; overflow-y: auto; } </style> **Plotting a numerical y and a categorical x is hard** This slideshow works you through challenges and how to overcome them! These solutions include: - Using jitter to avoid overplotting. - Using color to differentiate values. - Suppressing a redundant legend. - Showing means on a plot. --- **Step 1: Basic ggplot setup** Setting up aesthetic mapping <br> <br> .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid)) ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-3-1.png" width="576" /> ] --- **Step 1: Basic ggplot setup** Adding points <br> <br> .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid)) + geom_point() ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-5-1.png" width="576" /> ] --- **Step 1: Basic ggplot setup** OH NO those points are too small. Let's try bigger, somewhat transparent points. .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid)) + geom_point(size = 6, alpha = .3) ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-7-1.png" width="576" /> ] --- class: center middle section-slide # Try jitterplots --- **Step 2: Try jitterplots** It's still tough to differentiate overlapping points. Let's switch to jitterplots to spread out our x. .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid)) + geom_jitter(size = 6, alpha = .3, height = 0) # I set height equal to zero # So the plot doesn't lie. ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-9-1.png" width="576" /> ] --- **Step 2: Try jitterplots** That helps - but now categories blend :( Lets redundantly map visited on to color. .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid, color = visited)) + geom_jitter(size = 6, alpha = .3, height = 0) ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-11-1.png" width="576" /> ] --- **Step 2: Try jitterplots** Now the unnecessary legend takes up too much space. Let's remove it! .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid, color = visited)) + geom_jitter(size = 6, alpha = .3, height = 0)+ theme(legend.position = "none") ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-13-1.png" width="576" /> ] --- class: center middle section-slide # Add summaries --- **Step 3: Add summaries** Now let's add each group's mean <br><br> .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid, color = visited)) + geom_jitter(size = 6, alpha = .3, height = 0)+ theme(legend.position = "none")+ stat_summary(fun = "mean") ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-15-1.png" width="576" /> ] --- **Step 3: Add summaries** Those points are tiny. Let's make them big and black .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid, color = visited)) + geom_jitter(size = 6, alpha = .3, height = 0)+ theme(legend.position = "none")+ stat_summary(fun = "mean", color = "black", size = 4) ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-17-1.png" width="576" /> ] --- **Step 3: Add summaries** Those points are tiny. Let's make them big and black .left-column[ ``` r gc_rils|> ggplot(aes(x = visited, y = prop_hybrid, color = visited)) + geom_jitter(size = 6, alpha = .3, height = 0)+ theme(legend.position = "none")+ stat_summary(fun = "mean", color = "black", size = 1.5)+ stat_summary(aes(group = 1), color = "black", geom = "line", lty = 2, size = 2) ``` ] .right-column[ <img src="cat_cont_flipbook_files/figure-html/unnamed-chunk-19-1.png" width="576" /> ] --- class: center middle section-slide # *'Fin'*