Skip to content

Commit a1fb32e

Browse files
committed
edits to tutorials; use exercise blocks
1 parent 905088e commit a1fb32e

2 files changed

Lines changed: 219 additions & 95 deletions

File tree

‎vignettes/articles/plotting-with-formulas.qmd‎

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,26 @@ format: live-html
44
engine: knitr
55
webr:
66
packages: [ggformula, mosaic, tibble, NHANES, mosaicData]
7+
cell-options:
8+
fig-width: 8
9+
fig-height: 3
710
---
811

912
{{< include ../_extensions/r-wasm/live/_knitr.qmd >}}
1013

14+
<style>
15+
16+
.exercise {
17+
border-radius: 10px;
18+
border-color: steelblue;
19+
border-width: 3px;
20+
border-style: solid;
21+
padding: 15px;
22+
box-sizing: border-box;
23+
margin-bottom: 15px;
24+
}
25+
</style>
26+
1127
## How do we make this plot?
1228

1329
```{r setup, include=FALSE}
@@ -74,7 +90,7 @@ th, td { padding: 5px; }
7490
### US Births in 1978
7591
Here is an interesting plot showing the number of live births in the United
7692
States each day of 1978. We are going to use it to learn how to create
77-
plots using the `ggformula` package.
93+
and modify this plot using the `ggformula` package.
7894

7995
```{r, first-look, echo = FALSE}
8096
gf_point(births ~ date, data = Births1978)
@@ -221,7 +237,7 @@ goal( formula , data = mydata )
221237
</tr>
222238
</table>
223239

224-
#### Exercise
240+
:::{#exr-assemble-first-plot}
225241

226242
Put each piece in its place in the template below and then run the code
227243
to create the plot.
@@ -235,16 +251,22 @@ goal(y ~ x, data = mydata)
235251
If you get an "object not found" or "could not find function" error message,
236252
that indicates that you have not correctly filled in one of the four boxes from
237253
the template.
254+
:::
238255

239-
Note: R is case sensitive, so watch your capitalization.
256+
:::{.callout-warning}
257+
#### Capitalization matters
258+
R is case sensitive, so watch your capitalization.
240259
:::
241260

242261
For the record, here are the first few rows of `Births1978`.
243262

244-
```{r, top-of-data}
263+
```{r}
264+
#| label: top-of-data
245265
Births1978 |> head(3) |> select(births, date, day_of_year)
246266
```
247267

268+
:::
269+
248270
## Using formulas to describe plots
249271

250272
The most distinctive feature of `ggformula` plots is the use of formulas to
@@ -281,7 +303,7 @@ so the $y$-variable name goes before the tilde and the $x$-variable name goes
281303
after. (Think: "y depends on x". Also note that the $y$-axis label appears
282304
farther left than the $x$-axis label.)
283305

284-
#### Exercise
306+
:::{#exr-reverse-x-and-y}
285307

286308
Reverse the roles of the variables -- changing `births ~ date` to
287309
`date ~ birth` -- to see how the plot changes.
@@ -290,8 +312,9 @@ Reverse the roles of the variables -- changing `births ~ date` to
290312
#| exercise: gf_point-xy
291313
gf_point(births ~ date, data = Births1978)
292314
```
315+
:::
293316

294-
#### Exercise
317+
:::{#exr-date-vs-day-of-year}
295318

296319
Change `date` to `day_of_year` and see how the plot changes.
297320
(If you do this on a separate line, you will see both plots at once.)
@@ -300,6 +323,20 @@ Change `date` to `day_of_year` and see how the plot changes.
300323
#| exercise: gf_point-day_of_year
301324
gf_point(births ~ date, data = Births1978)
302325
```
326+
:::
327+
328+
:::{.callout-tip}
329+
Beginning with version 2.0, `ggformula` allows each element of the formula
330+
shape to be defined individually. For example, the following is equivalent
331+
to the previous plot.
332+
333+
```{webr}
334+
#| exercise: exercise-ggformula-2.0
335+
gf_point(y = ~ births, x = ~ date, data = Births1978)
336+
```
337+
338+
:::
339+
303340

304341
## Changing things up -- different data
305342

@@ -470,16 +507,19 @@ In the lingo of `ggformula`, we need to **map** color to the variable `wday`.
470507
gf_point(births ~ date, data = Births1978, color = "navy")
471508
```
472509

473-
**Question 1.** Does it appear that the conjecture about weekends is generally correct?
510+
:::{#exr-is-conjecture-correct}
511+
Does it appear that the conjecture about weekends is generally correct?
474512

475513
::: {.callout-note collapse="true"}
476514
## Answer
477515

478516
Yes -- the two bands of points correspond to weekdays (higher) and weekends
479517
(lower); mapping color to `wday` shows this directly.
480518
:::
519+
:::
481520

482-
**Question 2.** What happens if you omit the `~` before `wday`?
521+
:::{#exr-omit-tilde}
522+
What happens if you omit the `~` before `wday`?
483523

484524
::: {.callout-note collapse="true"}
485525
## Answer
@@ -488,6 +528,7 @@ There is an error message: `'wday' not found`. Without the tilde, R looks
488528
for an object named `wday` in your environment (rather than a column in
489529
the data), and there isn't one.
490530
:::
531+
:::
491532

492533
## Facets
493534

@@ -515,7 +556,7 @@ gf_point(births ~ day_of_year, data = Births, size = 0.5) |>
515556
gf_facet_wrap( ~ year)
516557
```
517558

518-
#### Exercise
559+
:::{#exr-edit-facet-plots}
519560

520561
Edit one of the plots above to do the following:
521562

@@ -525,8 +566,9 @@ Edit one of the plots above to do the following:
525566
```{webr}
526567
#| exercise: births-facets-ex1
527568
```
569+
:::
528570

529-
#### Exercise
571+
:::{#exr-more-facet-edits}
530572

531573
Now edit one the plots above to
532574

@@ -538,6 +580,7 @@ What advantage do we get from using facets in this case?
538580
```{webr}
539581
#| exercise: births-facets-ex2
540582
```
583+
:::
541584

542585
## Facet Grids and Facet Wraps
543586

@@ -551,7 +594,7 @@ A facet grid uses rows, or columns, or both in a fixed way.
551594
gf_point(births ~ day_of_year | year ~ wday, data = Births, size = 0.5)
552595
```
553596

554-
#### Exercise
597+
:::{#exr-facet-grid}
555598

556599
Recreate the plot above using `gf_facet_grid()`. This works much like `gf_facet_wrap()`
557600
and accepts a formula with one of three shapes
@@ -566,6 +609,7 @@ and accepts a formula with one of three shapes
566609
```{webr}
567610
#| exercise: facet-grid2
568611
```
612+
:::
569613

570614

571615
## Plots with one positional variable
@@ -586,7 +630,7 @@ Notice that when there is only one variable it is always on the **right** side.
586630
Other examples of functions that take just one variable include
587631
`gf_dens()`, `gf_freqpoly()`, `gf_dotplot()`, `gf_bar()`, and `gf_qq()`.
588632

589-
#### Exercise
633+
:::{#exr-one-var-plots}
590634

591635
Create some "one-variable" plots using the functions listed above
592636
and the template below. Which variable should you use?
@@ -595,7 +639,7 @@ and the template below. Which variable should you use?
595639
#| exercise: gf_histogram-etc
596640
gf____( ~ _____, data = Births1978)
597641
```
598-
642+
:::
599643

600644
## Your Turn
601645

@@ -619,7 +663,7 @@ To get a list of functions available in `ggformula`, run this code chunk.
619663
apropos("gf_")
620664
```
621665

622-
#### Exercise.
666+
:::{#exr-make-more-plots}
623667

624668
Make some plots to explore one or more of these data sets.
625669

@@ -639,6 +683,7 @@ head(KidsFeet)
639683
```{webr}
640684
#| exercise: explore-more
641685
```
686+
:::
642687

643688
## Creating data summaries with df_stats()
644689

@@ -655,7 +700,7 @@ gf_boxplot(births ~ wday, data = Births1978)
655700
We can see clear trends, but what if we want to know the mean and standard deviation,
656701
or the median and IQR for the number of births on each of the seven weekdays?
657702

658-
#### Exercise
703+
:::{#exr-df_stats}
659704

660705
Change `gf_boxplot()` to `df_stats()`.
661706

@@ -664,6 +709,8 @@ Change `gf_boxplot()` to `df_stats()`.
664709
gf_boxplot(births ~ wday, data = Births1978)
665710
```
666711

712+
:::
713+
667714
The default use of `df_stats()` computes some of the most common summary statistics.
668715
But we can use `df_stats()` to create other numerical summaries as well.
669716

@@ -673,25 +720,44 @@ But we can use `df_stats()` to create other numerical summaries as well.
673720
df_stats(births ~ wday, data = Births1978, median, iqr)
674721
```
675722

676-
#### Exercise
723+
:::{#exr-modify-df_stats}
677724

678725
Modify the code above to compute some other summaries of your choosing.
679726

727+
```{webr}
728+
#| exercise: df_stats03
729+
df_stats(births ~ wday, data = Births1978, median, iqr)
730+
```
731+
:::
680732

681733

682734
### Chaining with df_stats()
683735

736+
`df_stats()` is designed to work in command chains with `|>`.
737+
684738
```{webr}
685-
#| exercise: df_stats03
739+
#| exercise: df_stats-chaining01
686740
Births1978 |>
687741
df_stats(births ~ wday, median, iqr)
688742
```
689743

690-
### Combining with plots
744+
The result can be passed directly to a `gf_*()` function for plotting.
745+
746+
```{webr}
747+
#| exercise: df_stats-chaining02
748+
Births1978 |>
749+
df_stats(births ~ wday, median, iqr) |>
750+
gf_point(median ~ wday)
751+
752+
```
753+
754+
### Annotating plots
691755

692756
In addition to the usual plot elements (lines, points, etc.), sometimes it is
693757
useful to add text with `gf_text()` or `gf_label()`.
694758

759+
760+
:::{#exr-annotated-plot}
695761
See if you can figure out what this does before your execute it.
696762

697763
```{webr}
@@ -704,6 +770,7 @@ gf_boxplot(births ~ wday, data = Births1978) |>
704770
gf_text( 6700 ~ wday, label = ~ round(mean), data = Births_summary,
705771
color = "red")
706772
```
773+
:::
707774

708775
## Where do we go from here?
709776

0 commit comments

Comments
 (0)