Skip to content

Commit

Permalink
new exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Jul 27, 2024
1 parent 11c4d19 commit 5421dd0
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 15 deletions.
28 changes: 28 additions & 0 deletions docs/materials/positconf2024-themes-solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,34 @@ <h2 class="anchored" data-anchor-id="solutions-section-2">Solutions, Section 2</
<p><img src="positconf2024-themes-solutions_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Adjust the minor grid lines in this plot to match the log ticks.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># made-up data that follows exponential growth</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>exp_data <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">40</span>) <span class="sc">|&gt;</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">exp</span>(<span class="fl">0.08</span> <span class="sc">*</span> x <span class="sc">+</span> <span class="fl">0.1</span> <span class="sc">*</span> <span class="fu">rnorm</span>(<span class="fu">length</span>(x)))</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(exp_data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># sometimes the simplest solution</span></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># is a quick manual fix</span></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="at">minor_breaks =</span> <span class="fu">c</span>(</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> .<span class="dv">8</span>, .<span class="dv">9</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>, <span class="dv">20</span></span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="at">guide =</span> <span class="fu">guide_axis_logticks</span>(</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="co"># make major and minor ticks all the same length</span></span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="at">long =</span> <span class="dv">1</span>,</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> <span class="at">mid =</span> <span class="dv">1</span>,</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> <span class="at">short =</span> <span class="dv">1</span></span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="positconf2024-themes-solutions_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>

</main>
Expand Down
27 changes: 27 additions & 0 deletions docs/materials/positconf2024-themes-solutions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,30 @@ ggplot(diamonds, aes(price)) +
) +
panel_border("gray40")
```

Adjust the minor grid lines in this plot to match the log ticks.

```{r}
# made-up data that follows exponential growth
exp_data <- tibble(x = 0:40) |>
mutate(
y = exp(0.08 * x + 0.1 * rnorm(length(x)))
)
ggplot(exp_data, aes(x, y)) +
geom_point() +
theme_bw() +
scale_y_log10(
# sometimes the simplest solution
# is a quick manual fix
minor_breaks = c(
.8, .9, 2, 4, 5, 6, 7, 8, 9, 20
),
guide = guide_axis_logticks(
# make major and minor ticks all the same length
long = 1,
mid = 1,
short = 1
)
)
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 20 additions & 3 deletions docs/materials/positconf2024-themes.html
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ <h2 class="anchored" data-anchor-id="new-axis-features-in-ggplot2-3.5.0">2. New
<p>For logarithmic axes, there is a new guide <code>guide_axis_logticks()</code> that places minor ticks with log spacing.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># made-up data that follows exponential growth</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">40</span>) <span class="sc">|&gt;</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a>exp_data <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">40</span>) <span class="sc">|&gt;</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">exp</span>(<span class="fl">0.08</span> <span class="sc">*</span> x <span class="sc">+</span> <span class="fl">0.1</span> <span class="sc">*</span> <span class="fu">rnorm</span>(<span class="fu">length</span>(x)))</span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(exp_data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<span id="cb26-8"><a href="#cb26-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb26-9"><a href="#cb26-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb26-10"><a href="#cb26-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(</span>
Expand All @@ -357,7 +357,7 @@ <h2 class="anchored" data-anchor-id="new-axis-features-in-ggplot2-3.5.0">2. New
</div>
<p>However, as you can see, the axis ticks don’t line up with the minor grid lines. Therefore, it is best to just turn off the minor grid lines.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(exp_data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(</span>
Expand Down Expand Up @@ -397,6 +397,23 @@ <h2 class="anchored" data-anchor-id="exercises-1">Exercises</h2>
<p><img src="positconf2024-themes_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Adjust the minor grid lines in this plot to match the log ticks.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(exp_data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(</span>
<span id="cb30-5"><a href="#cb30-5" aria-hidden="true" tabindex="-1"></a> <span class="at">guide =</span> <span class="fu">guide_axis_logticks</span>(</span>
<span id="cb30-6"><a href="#cb30-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># make major and minor ticks all the same length</span></span>
<span id="cb30-7"><a href="#cb30-7" aria-hidden="true" tabindex="-1"></a> <span class="at">long =</span> <span class="dv">1</span>,</span>
<span id="cb30-8"><a href="#cb30-8" aria-hidden="true" tabindex="-1"></a> <span class="at">mid =</span> <span class="dv">1</span>,</span>
<span id="cb30-9"><a href="#cb30-9" aria-hidden="true" tabindex="-1"></a> <span class="at">short =</span> <span class="dv">1</span></span>
<span id="cb30-10"><a href="#cb30-10" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb30-11"><a href="#cb30-11" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="positconf2024-themes_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>

</main>
Expand Down
22 changes: 19 additions & 3 deletions docs/materials/positconf2024-themes.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ For logarithmic axes, there is a new guide `guide_axis_logticks()` that places m

```{r}
# made-up data that follows exponential growth
data <- tibble(x = 0:40) |>
exp_data <- tibble(x = 0:40) |>
mutate(
y = exp(0.08 * x + 0.1 * rnorm(length(x)))
)
ggplot(data, aes(x, y)) +
ggplot(exp_data, aes(x, y)) +
geom_point() +
theme_bw() +
scale_y_log10(
Expand All @@ -238,7 +238,7 @@ ggplot(data, aes(x, y)) +
However, as you can see, the axis ticks don't line up with the minor grid lines. Therefore, it is best to just turn off the minor grid lines.

```{r}
ggplot(data, aes(x, y)) +
ggplot(exp_data, aes(x, y)) +
geom_point() +
theme_bw() +
scale_y_log10(
Expand Down Expand Up @@ -272,3 +272,19 @@ ggplot(diamonds, aes(price)) +
geom_density(fill = "#0072B280") +
facet_wrap(~cut)
```

Adjust the minor grid lines in this plot to match the log ticks.

```{r}
ggplot(exp_data, aes(x, y)) +
geom_point() +
theme_bw() +
scale_y_log10(
guide = guide_axis_logticks(
# make major and minor ticks all the same length
long = 1,
mid = 1,
short = 1
)
)
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
"description": "A bootcamp-style course on data visualization",
"author": [],
"contents": "\nThis is the home page for a dataviz shortcourse by Claus O. Wilke. The course is an abbreviated version of a semester-long course taught at The University of Texas at Austin. Many of the materials presented are taken from the book Fundamentals of Data Visualization.\n\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT. Note that figures in slides may be pulled in from external sources and may be licensed under different terms. For such images, image credits are available in the slide notes, accessible via pressing the letter ‘p’.\n\n\n\n",
"last_modified": "2024-07-27T18:02:06+02:00"
"last_modified": "2024-07-27T21:00:49+02:00"
},
{
"path": "lessons.html",
"title": "Lessons",
"description": "",
"author": [],
"contents": "\n\nContents\nEffective Visual Communication\nFrom Data to Visualisation 1\nFrom Data to Visualisation 2\nPrinciples of Figure Design\nReuse\n\nEffective Visual Communication\nThis lecture covers three core concepts of visual communication. First, I will discuss how to engage your audience by telling a story. Second, I will provide high-level guidance for designing visualizations that make a clear point. Third, I will describe key design principles that ensure your figures will be readable by people with vision impairments, and specifically impairments of color perception.\n\nMaterials:\n\nSlides\nFrom Data to Visualisation 1\nVisualizing amounts; visualizing distributions; visualizing associations and trends\n\nMaterials:\n\nSlides\nFrom Data to Visualisation 2\nVisualizing uncertainty; visualizing geospatial data\n\nMaterials:\n\nSlides\nPrinciples of Figure Design\nColors; compound figures; infographics\n\nMaterials:\n\nSlides\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT. Note that figures in slides may be pulled in from external sources and may be licensed under different terms. For such images, image credits are available in the slide notes, accessible via pressing the letter ‘p’.\n\n\n\n",
"last_modified": "2024-07-27T18:02:06+02:00"
"last_modified": "2024-07-27T21:00:50+02:00"
},
{
"path": "workshops.html",
"title": "Workshops",
"description": "",
"author": [],
"contents": "\n\nContents\nReuse\n\nMaterials for past and present workshops. Many workshop materials are based on content from my semester-long data visualization course at UT Austin.\n2024-08-12: Effective Data Visualization with ggplot2\nAll workshop materials will be posted here.\n2024-06-24: Visualizing Uncertainty and Trends\nKeynote, June 24 2024: slides\nWorkshop, June 27 2024:\nQuarto notebook\nHTML\nHTML with solutions\n\n2024-06-20: Effective Visual Communication with R\nWorkshop in support of Ukraine. To work through all the examples provided, you will need the following R packages: tidyverse, cowplot, ggiraph, sf, patchwork\nSlides Part 1\nSlides Part 2\nSlides Part 2, without interactivity (Some browsers crash when there are too many interactive examples in one slide set.)\n2024-05-28: Effective Visual Communication\nSlides Part 1 (telling a story, making a point, making figures accessible)\nSlides Part 2 (figure composition and context, text annotations, overplotting)\nReuse\nText and figures are licensed under Creative Commons Attribution CC BY 4.0. Any computer code (R, HTML, CSS, etc.) in slides and worksheets, including in slide and worksheet sources, is also licensed under MIT. Note that figures in slides may be pulled in from external sources and may be licensed under different terms.\n\n\n\n",
"last_modified": "2024-07-27T18:02:07+02:00"
"last_modified": "2024-07-27T21:00:51+02:00"
}
],
"collections": []
Expand Down
28 changes: 28 additions & 0 deletions materials/positconf2024-themes-solutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,34 @@ <h2 class="anchored" data-anchor-id="solutions-section-2">Solutions, Section 2</
<p><img src="positconf2024-themes-solutions_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Adjust the minor grid lines in this plot to match the log ticks.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># made-up data that follows exponential growth</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>exp_data <span class="ot">&lt;-</span> <span class="fu">tibble</span>(<span class="at">x =</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">40</span>) <span class="sc">|&gt;</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">exp</span>(<span class="fl">0.08</span> <span class="sc">*</span> x <span class="sc">+</span> <span class="fl">0.1</span> <span class="sc">*</span> <span class="fu">rnorm</span>(<span class="fu">length</span>(x)))</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(exp_data, <span class="fu">aes</span>(x, y)) <span class="sc">+</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_y_log10</span>(</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="co"># sometimes the simplest solution</span></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># is a quick manual fix</span></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="at">minor_breaks =</span> <span class="fu">c</span>(</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> .<span class="dv">8</span>, .<span class="dv">9</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">6</span>, <span class="dv">7</span>, <span class="dv">8</span>, <span class="dv">9</span>, <span class="dv">20</span></span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="at">guide =</span> <span class="fu">guide_axis_logticks</span>(</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="co"># make major and minor ticks all the same length</span></span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="at">long =</span> <span class="dv">1</span>,</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> <span class="at">mid =</span> <span class="dv">1</span>,</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> <span class="at">short =</span> <span class="dv">1</span></span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="positconf2024-themes-solutions_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>

</main>
Expand Down
Loading

0 comments on commit 5421dd0

Please sign in to comment.