Skip to content

Commit 5dd2989

Browse files
committed
Merge remote branch
Merge branch 'main' of github.com:fmicompbio/intermediate_ggplot2 # Conflicts: # 3_many_points_scatterplot.html
2 parents 5cdca6a + be69226 commit 5dd2989

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

3_many_points_scatterplot.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,12 +3181,12 @@ <h3 class="anchored" data-anchor-id="plot">Plot</h3>
31813181
<p>Let’s first create a simple scatter plot to illustrate the problems. The <code>diamonds</code> dataset has 53940 observations. If stored into a vectorized graphics device such as <code>pdf()</code> or <code>svg()</code>, the file will be large (each observation is individually represented as graphic elements) and slow to open or arrange. Furthermore, the high number of data points leads to saturation and we do not see the full underlying density of data points.</p>
31823182
<div class="cell">
31833183
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># create base plot</span></span>
3184-
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>p0 <span class="ot">&lt;-</span> <span class="fu">ggplot</span>(<span class="at">data =</span> diamonds, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> carat, <span class="at">y =</span> price)) <span class="sc">+</span></span>
3184+
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>gg <span class="ot">&lt;-</span> <span class="fu">ggplot</span>(<span class="at">data =</span> diamonds, <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> carat, <span class="at">y =</span> price)) <span class="sc">+</span></span>
31853185
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">&quot;Weight of the diamond (carat)&quot;</span>, <span class="at">y =</span> <span class="st">&quot;Price (US dollars)&quot;</span>) <span class="sc">+</span></span>
31863186
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>(<span class="dv">20</span>) <span class="sc">+</span></span>
31873187
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">panel.grid =</span> <span class="fu">element_blank</span>(),</span>
31883188
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a> <span class="at">legend.position =</span> <span class="st">&quot;bottom&quot;</span>)</span>
3189-
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>p0 <span class="sc">+</span> <span class="fu">geom_point</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
3189+
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a>gg <span class="sc">+</span> <span class="fu">geom_point</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
31903190
<div class="cell-output-display">
31913191
<div>
31923192
<figure class="figure">
@@ -3200,7 +3200,7 @@ <h3 class="anchored" data-anchor-id="plot">Plot</h3>
32003200
<details class="code-fold">
32013201
<summary>Code</summary>
32023202
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ... with transparency</span></span>
3203-
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>p0 <span class="sc">+</span> <span class="fu">geom_point</span>(<span class="at">color =</span> <span class="fu">alpha</span>(<span class="st">&quot;black&quot;</span>, <span class="fl">0.02</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
3203+
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a>gg <span class="sc">+</span> <span class="fu">geom_point</span>(<span class="at">color =</span> <span class="fu">alpha</span>(<span class="st">&quot;black&quot;</span>, <span class="fl">0.02</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
32043204
</details>
32053205
<div class="cell-output-display">
32063206
<div>
@@ -3215,7 +3215,7 @@ <h3 class="anchored" data-anchor-id="plot">Plot</h3>
32153215
<details class="code-fold">
32163216
<summary>Code</summary>
32173217
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ... with marginal density plots by number of cylinders</span></span>
3218-
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>p0 <span class="sc">+</span> <span class="fu">geom_density_2d_filled</span>(<span class="at">bins =</span> <span class="dv">48</span>) <span class="sc">+</span></span>
3218+
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>gg <span class="sc">+</span> <span class="fu">geom_density_2d_filled</span>(<span class="at">bins =</span> <span class="dv">48</span>) <span class="sc">+</span></span>
32193219
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_cartesian</span>(<span class="at">expand =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
32203220
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">&quot;none&quot;</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
32213221
</details>
@@ -3227,12 +3227,12 @@ <h3 class="anchored" data-anchor-id="plot">Plot</h3>
32273227
</div>
32283228
</div>
32293229
</div>
3230-
<p>The linear contour levels or color intervals (controlled by <code>bins</code> or <code>breaks</code>) may not work well in a case like ours, where the density is very high in some regions that will occupy almost the complete color scale and we lose resoulution in low density regions. You can use <code>breaks</code> to create non-linear intervals (here combined with <code>ndensity</code> so that we know the range of densities: [0, 1]) and with <code>theme(panel.background)</code> to make the zero-density area dark blue.</p>
3230+
<p>The linear contour levels or color intervals (controlled by <code>bins</code> or <code>breaks</code>) may not work well in a case like ours, where the density is very high in some regions that will occupy almost the complete color scale and we lose resolution in low density regions. You can use <code>breaks</code> to create non-linear intervals (here combined with <code>ndensity</code> so that we know the range of densities: [0, 1]) and with <code>theme(panel.background)</code> to make the zero-density area dark blue.</p>
32313231
<div class="cell">
32323232
<details class="code-fold">
32333233
<summary>Code</summary>
32343234
<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"># ... with marginal density plots by number of cylinders</span></span>
3235-
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>p0 <span class="sc">+</span> <span class="fu">geom_density_2d_filled</span>(<span class="at">contour_var =</span> <span class="st">&quot;ndensity&quot;</span>,</span>
3235+
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>gg <span class="sc">+</span> <span class="fu">geom_density_2d_filled</span>(<span class="at">contour_var =</span> <span class="st">&quot;ndensity&quot;</span>,</span>
32363236
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="at">breaks =</span> <span class="fu">exp</span>(<span class="fu">seq</span>(<span class="fu">log</span>(<span class="fl">1e-4</span>), <span class="fu">log</span>(<span class="dv">1</span>), <span class="at">length.out =</span> <span class="dv">64</span>))) <span class="sc">+</span></span>
32373237
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_cartesian</span>(<span class="at">expand =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
32383238
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">&quot;none&quot;</span>,</span>
@@ -3251,7 +3251,7 @@ <h3 class="anchored" data-anchor-id="plot">Plot</h3>
32513251
<details class="code-fold">
32523252
<summary>Code</summary>
32533253
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># ... with marginal violin and labelled data points</span></span>
3254-
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>p0 <span class="sc">+</span> <span class="fu">geom_scattermore</span>(<span class="at">pointsize =</span> <span class="dv">2</span>, <span class="at">alpha =</span> <span class="fl">0.02</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
3254+
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>gg <span class="sc">+</span> <span class="fu">geom_scattermore</span>(<span class="at">pointsize =</span> <span class="dv">2</span>, <span class="at">alpha =</span> <span class="fl">0.02</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
32553255
</details>
32563256
<div class="cell-output-display">
32573257
<div>

3_many_points_scatterplot.qmd

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ leads to saturation and we do not see the full underlying density of data points
6565
#| code-fold: false
6666
6767
# create base plot
68-
p0 <- ggplot(data = diamonds, mapping = aes(x = carat, y = price)) +
68+
gg <- ggplot(data = diamonds, mapping = aes(x = carat, y = price)) +
6969
labs(x = "Weight of the diamond (carat)", y = "Price (US dollars)") +
7070
theme_bw(20) +
7171
theme(panel.grid = element_blank(),
7272
legend.position = "bottom")
73-
p0 + geom_point()
73+
gg + geom_point()
7474
```
7575

7676
A simple way to improve the saturation issue is to use transparency, so that
@@ -83,7 +83,7 @@ solve the "many points" problem yet.
8383
#| fig-height: 7
8484
8585
# ... with transparency
86-
p0 + geom_point(color = alpha("black", 0.02))
86+
gg + geom_point(color = alpha("black", 0.02))
8787
```
8888

8989
A simple way to solve also the "many points" problem is to avoid showing
@@ -96,14 +96,14 @@ using a color scale.
9696
#| fig-height: 7
9797
9898
# ... with marginal density plots by number of cylinders
99-
p0 + geom_density_2d_filled(bins = 48) +
99+
gg + geom_density_2d_filled(bins = 48) +
100100
coord_cartesian(expand = FALSE) +
101101
theme(legend.position = "none")
102102
```
103103

104104
The linear contour levels or color intervals (controlled by `bins` or `breaks`)
105105
may not work well in a case like ours, where the density is very high in some
106-
regions that will occupy almost the complete color scale and we lose resoulution
106+
regions that will occupy almost the complete color scale and we lose resolution
107107
in low density regions. You can use `breaks` to create non-linear intervals
108108
(here combined with `ndensity` so that we know the range of densities: [0, 1])
109109
and with `theme(panel.background)` to make the zero-density area dark blue.
@@ -114,7 +114,7 @@ and with `theme(panel.background)` to make the zero-density area dark blue.
114114
#| fig-height: 7
115115
116116
# ... with marginal density plots by number of cylinders
117-
p0 + geom_density_2d_filled(contour_var = "ndensity",
117+
gg + geom_density_2d_filled(contour_var = "ndensity",
118118
breaks = exp(seq(log(1e-4), log(1), length.out = 64))) +
119119
coord_cartesian(expand = FALSE) +
120120
theme(legend.position = "none",
@@ -131,7 +131,7 @@ hard to use, `scattermore` provides a solution for that too.
131131
#| fig-height: 7
132132
133133
# ... with marginal violin and labelled data points
134-
p0 + geom_scattermore(pointsize = 2, alpha = 0.02)
134+
gg + geom_scattermore(pointsize = 2, alpha = 0.02)
135135
```
136136

137137

0 commit comments

Comments
 (0)