Skip to content

Commit bb930f6

Browse files
committed
add solutions using details
1 parent 0967eef commit bb930f6

11 files changed

+38
-17
lines changed

figure/unnamed-chunk-15-1.png

-9.08 KB
Loading

figure/unnamed-chunk-16-1.png

-34.4 KB
Loading

figure/unnamed-chunk-17-1.png

15.6 KB
Loading

figure/unnamed-chunk-18-1.png

50.6 KB
Loading

figure/unnamed-chunk-20-1.png

-2.21 KB
Loading

figure/unnamed-chunk-21-1.png

2.48 KB
Loading

figure/unnamed-chunk-22-1.png

47.7 KB
Loading

ggplot.Rmd

+11-3
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ hidden.
173173
>
174174
> - Create boxplot for `weight`.
175175
176+
```{r, eval=FALSE}
177+
surveys_bw + geom_violin()
178+
179+
surveys_bw + geom_boxplot() + scale_y_log10()
180+
181+
ggplot(data = surveys_complete,
182+
aes(x = species_id, y = weight)) +
183+
geom_boxplot()
184+
```
176185
### Plotting time series data
177186

178187
Let's calculate number of counts per year for each species. To do that
@@ -259,7 +268,6 @@ ggplot(data = yearly_sex_counts,
259268
> Modify the plotting code above to colour the time series by sex in
260269
> the different facets.
261270
262-
263271
To make the plot easier to read, we can color by sex instead of
264272
species (species are already in separate plots, so we don’t need to
265273
distinguish them further).
@@ -287,7 +295,7 @@ starting point to create a new hand-crafted theme.
287295
> Use what you just learned to create a plot that depicts how the
288296
> average weight of each species changes through the years.
289297
290-
298+
<details>
291299
```{r average-weight-timeseries, purl=FALSE}
292300
yearly_weight <- surveys_complete %>%
293301
group_by(year, species_id) %>%
@@ -298,7 +306,7 @@ ggplot(data = yearly_weight,
298306
facet_wrap(~ species_id) +
299307
theme_bw()
300308
```
301-
309+
</details>
302310

303311

304312
## References

ggplot.md

+19-10
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ hidden.
207207
>
208208
> - Create boxplot for `weight`.
209209
210+
211+
```r
212+
surveys_bw + geom_violin()
213+
214+
surveys_bw + geom_boxplot() + scale_y_log10()
215+
216+
ggplot(data = surveys_complete,
217+
aes(x = species_id, y = weight)) +
218+
geom_boxplot()
219+
```
210220
### Plotting time series data
211221

212222
Let's calculate number of counts per year for each species. To do that
@@ -228,7 +238,7 @@ ggplot(data = yearly_counts, aes(x = year, y = n)) +
228238
geom_line()
229239
```
230240

231-
![plot of chunk unnamed-chunk-14](figure/unnamed-chunk-14-1.png)
241+
![plot of chunk unnamed-chunk-15](figure/unnamed-chunk-15-1.png)
232242

233243
Unfortunately this does not work, because we plot data for all the species
234244
together. We need to tell ggplot to draw a line for each species by modifying
@@ -241,7 +251,7 @@ ggplot(data = yearly_counts,
241251
geom_line()
242252
```
243253

244-
![plot of chunk unnamed-chunk-15](figure/unnamed-chunk-15-1.png)
254+
![plot of chunk unnamed-chunk-16](figure/unnamed-chunk-16-1.png)
245255

246256
We will be able to distinguish species in the plot if we add colors.
247257

@@ -252,7 +262,7 @@ ggplot(data = yearly_counts,
252262
geom_line()
253263
```
254264

255-
![plot of chunk unnamed-chunk-16](figure/unnamed-chunk-16-1.png)
265+
![plot of chunk unnamed-chunk-17](figure/unnamed-chunk-17-1.png)
256266

257267
## Faceting
258268

@@ -273,7 +283,7 @@ ggplot(data = yearly_counts,
273283
## adjust the group aesthetic?
274284
```
275285

276-
![plot of chunk unnamed-chunk-17](figure/unnamed-chunk-17-1.png)
286+
![plot of chunk unnamed-chunk-18](figure/unnamed-chunk-18-1.png)
277287

278288
Now we would like to split line in each plot by sex of each individual
279289
measured. To do that we need to make counts in data frame grouped by year,
@@ -301,7 +311,7 @@ ggplot(data = yearly_sex_counts,
301311
## adjust the group aesthetic?
302312
```
303313

304-
![plot of chunk unnamed-chunk-19](figure/unnamed-chunk-19-1.png)
314+
![plot of chunk unnamed-chunk-20](figure/unnamed-chunk-20-1.png)
305315

306316
Usually plots with white background look more readable when printed.
307317
We can set the background to white using the function
@@ -321,14 +331,13 @@ ggplot(data = yearly_sex_counts,
321331
## adjust the group aesthetic?
322332
```
323333

324-
![plot of chunk unnamed-chunk-20](figure/unnamed-chunk-20-1.png)
334+
![plot of chunk unnamed-chunk-21](figure/unnamed-chunk-21-1.png)
325335

326336
> ### Challenges
327337
>
328338
> Modify the plotting code above to colour the time series by sex in
329339
> the different facets.
330340
331-
332341
To make the plot easier to read, we can color by sex instead of
333342
species (species are already in separate plots, so we don’t need to
334343
distinguish them further).
@@ -347,7 +356,7 @@ ggplot(data = yearly_sex_counts,
347356
## adjust the group aesthetic?
348357
```
349358

350-
![plot of chunk unnamed-chunk-21](figure/unnamed-chunk-21-1.png)
359+
![plot of chunk unnamed-chunk-22](figure/unnamed-chunk-22-1.png)
351360

352361
#### The ggplot2 themes
353362

@@ -364,7 +373,7 @@ starting point to create a new hand-crafted theme.
364373
> Use what you just learned to create a plot that depicts how the
365374
> average weight of each species changes through the years.
366375
367-
376+
<details>
368377

369378
```r
370379
yearly_weight <- surveys_complete %>%
@@ -383,7 +392,7 @@ ggplot(data = yearly_weight,
383392
```
384393

385394
![plot of chunk average-weight-timeseries](figure/average-weight-timeseries-1.png)
386-
395+
</details>
387396

388397

389398
## References

tidy.Rmd

+4-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ surveys %>%
200200
> In this `hindfoot_half` column, there are no `NA`s and all values are less
201201
> than 30.
202202
203-
203+
<details>
204204
```{r}
205205
## Answer
206206
surveys_hindfoot_half <- surveys %>%
@@ -209,6 +209,7 @@ surveys_hindfoot_half <- surveys %>%
209209
filter(hindfoot_half < 30) %>%
210210
select(species_id, hindfoot_half)
211211
```
212+
</details>
212213

213214
### Split-apply-combine data analysis and the summarize() function
214215

@@ -282,7 +283,7 @@ counts the total number of records for each category.
282283
> combination of `group_by()` and `tally()`. How could you get the same result
283284
> using `group_by()` and `summarize()`? Hint: see `?n`.
284285
285-
286+
<details>
286287
```{r}
287288
## Answer 1
288289
surveys %>%
@@ -312,6 +313,7 @@ surveys %>%
312313
group_by(sex) %>%
313314
summarize(n = n())
314315
```
316+
</details>
315317

316318

317319
See also [`dplyr` cheat sheet](http://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf)

tidy.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ surveys %>%
457457
> In this `hindfoot_half` column, there are no `NA`s and all values are less
458458
> than 30.
459459
460-
460+
<details>
461461

462462
```r
463463
## Answer
@@ -467,6 +467,7 @@ surveys_hindfoot_half <- surveys %>%
467467
filter(hindfoot_half < 30) %>%
468468
select(species_id, hindfoot_half)
469469
```
470+
</details>
470471

471472
### Split-apply-combine data analysis and the summarize() function
472473

@@ -600,7 +601,7 @@ counts the total number of records for each category.
600601
> combination of `group_by()` and `tally()`. How could you get the same result
601602
> using `group_by()` and `summarize()`? Hint: see `?n`.
602603
603-
604+
<details>
604605

605606
```r
606607
## Answer 1
@@ -693,6 +694,7 @@ surveys %>%
693694
## 2 M 17348
694695
## 3 <NA> 1748
695696
```
697+
</details>
696698

697699

698700
See also [`dplyr` cheat sheet](http://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf)

0 commit comments

Comments
 (0)