Skip to content

Commit 062a84a

Browse files
authored
Merge pull request #806 from vickimzhang/answers-to-challenges-visualization-lesson
add answers to boxplot challenges
2 parents 9c8aa5b + 964b5d4 commit 062a84a

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

04-visualization-ggplot2.Rmd

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ hidden?
291291
>
292292
> - Replace the box plot with a violin plot; see `geom_violin()`.
293293
>
294+
> ```{r, answer=TRUE, purl=FALSE}
295+
> ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
296+
> geom_jitter(alpha = 0.3, color = "tomato") +
297+
> geom_violin()
298+
> ```
299+
>
294300
> In many types of data, it is important to consider the *scale* of the
295301
> observations. For example, it may be worth changing the scale of the axis to
296302
> better distribute the observations in the space of the plot. Changing the scale
@@ -299,24 +305,39 @@ hidden?
299305
>
300306
> - Represent weight on the log~10~ scale; see `scale_y_log10()`.
301307
>
308+
> ```{r, answer=TRUE, purl=FALSE}
309+
> ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
310+
> scale_y_log10() +
311+
> geom_jitter(alpha = 0.3, color = "tomato") +
312+
> geom_boxplot(outlier.shape = NA)
313+
> ```
314+
>
302315
> So far, we've looked at the distribution of weight within species. Try making
303316
> a new plot to explore the distribution of another variable within each species.
304317
>
305318
> - Create boxplot for `hindfoot_length`. Overlay the boxplot layer on a jitter
306319
> layer to show actual measurements.
307320
>
321+
> ```{r, answer=TRUE, purl=FALSE}
322+
> ggplot(data = surveys_complete, mapping = aes(x = species_id, y = hindfoot_length)) +
323+
> geom_jitter(alpha = 0.3, color = "tomato") +
324+
> geom_boxplot(outlier.shape = NA)
325+
> ```
326+
>
308327
> - Add color to the data points on your boxplot according to the plot from which
309328
> the sample was taken (`plot_id`).
310-
329+
>
311330
> Hint: Check the class for `plot_id`. Consider changing the class of `plot_id`
312331
> from integer to factor. Why does this change how R makes the graph?
313332
314333
```{r boxplot-challenge, eval = FALSE, purl = TRUE, echo = FALSE}
315334
## Challenge with boxplots:
316335
## Start with the boxplot we created:
317336
ggplot(data = surveys_complete, mapping = aes(x = species_id, y = weight)) +
318-
geom_boxplot(alpha = 0) +
319-
geom_jitter(alpha = 0.3, color = "tomato")
337+
geom_jitter(alpha = 0.3, color = "tomato") +
338+
geom_boxplot(outlier.shape = NA)
339+
## By ordering the geom layers like this, we can make sure that the boxplot is
340+
## layered over the jittered points.
320341
321342
## 1. Replace the box plot with a violin plot; see `geom_violin()`.
322343

0 commit comments

Comments
 (0)