<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>OSU Code Club</title>
<link>https://osu-codeclub.github.io/</link>
<atom:link href="https://osu-codeclub.github.io/index.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.10.18</generator>
<lastBuildDate>Thu, 17 Sep 2026 04:00:00 GMT</lastBuildDate>
<item>
  <title>AI-assisted plotting with R and Positron</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <dc:creator>Jelmer Poelstra</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S12E02_plotting_01/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<section id="what-well-cover" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="what-well-cover"><span class="header-section-number">1.1</span> What we’ll cover</h2>
<p>Developments in generative artificial intelligence (AI) using large language models (LLMs) is revolutizing the way the people code. We have covered in <a href="https://osu-codeclub.github.io/#category=ggplot2">many past Code Clubs</a> how to use <code>ggplot2</code> in R to make the plots of your dreams, and today we are going to talk about aiding that process using AI.</p>
<p>By the end of the session, you should be more comfortable with using AI to aid in your plotting. Specifically, we’ll cover:</p>
<ul>
<li>The basics of <code>ggplot2</code> syntax</li>
<li>Using AI to make a simple plot</li>
<li>Using prompts to refine that plot more</li>
<li>Prompting to make a much more complicated figure</li>
</ul>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-discussion" class="level3 unnumbered exercise">
<h3 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-discussion"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Discussion</h3>
<ul>
<li>Do you currently use AI to help with your plotting?</li>
<li>If yes, how do you integrate AI into your workflow?</li>
<li>Do you consider yourself familiar with the basics <code>ggplot2</code>?</li>
</ul>
</section>
</section>
<section id="before-we-start" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="before-we-start"><span class="header-section-number">1.2</span> Before we start</h2>
<p>We are going to be working in Positron for the rest of the semester. If you haven’t done so already, please:</p>
<ol type="1">
<li><p><strong>Install Positron</strong>: download the appropriate installer from the <a href="https://positron.posit.co/download.html" target="_blank">download page</a>, and run it. There are installers for Windows, macOS, and Linux.</p></li>
<li><p><strong>Make sure you have a recent R</strong> (4.4.0 or higher) installed; Positron does not come with its own copy of R.</p></li>
</ol>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Using an OSU-managed computer and have no admin rights?
</div>
</div>
<div class="callout-body-container callout-body">
<p>You should still be able to install Positron, just do so at the user level rather than system-wide.</p>
<p>On a Mac, this involves dragging the Positron app into your <strong>User</strong> Applications folder, <code>/Users/&lt;username&gt;/Applications</code> (i.e.&nbsp;this Applications folder is in the same parent folder as your Documents, Downloads, and Desktop folder).</p>
<p>On Windows, the Positron installer has a <strong>“User install”</strong> option. Look for a checkbox or dropdown labeled <strong>“Install for me only”</strong> (vs.&nbsp;“for all users”) during setup — that keeps the install in your user profile without needing admin rights.</p>
</div>
</div>
</section>
</section>
<section id="getting-started" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Getting started</h1>
<p>Go ahead and start Positron! It should look something like this:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/positron-whole.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:98.0%" alt="Screenshot of the Positron IDE, with the Editor, Console, and various panes"></p>
</figure>
</div>
<section id="opening-a-folder-replaces-rstudio-projects" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="opening-a-folder-replaces-rstudio-projects"><span class="header-section-number">2.1</span> Opening a folder (replaces RStudio Projects)</h2>
<div class="columns">
<div class="column" style="width:45%;">
<p>On the left side of the Positron window, you should see something similar to what’s shown in the screenshot on the right, prompting you to <strong>open a folder</strong>.</p>
<p>“Opening a folder” is Positron’s <strong>equivalent of opening an RStudio Project</strong>. The idea is the same: a folder is a self-contained workspace, and opening it sets your working directory and gives you a fresh R session.</p>
<p>But instead of requiring the special <code>.Rproj</code> file that is created when you make an RStudio Project, Positron simply <strong>treats any folder you open as a project</strong>.</p>
</div><div class="column" style="width:55%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/open-folder.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Positron welcome screen, with a button to open a folder"></p>
</figure>
</div>
</div>
</div>
<p><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Let’s open a new folder for today’s session:</p>
<ol type="1">
<li><p>You can use the folder you created last month when you <a href="https://osu-codeclub.github.io/posts/S12E01_positron_01/">learned about Positron from Jelmer</a>, or you can create a new folder to use today. You can create it in any way, such as with your computer’s File Explorer / Finder.</p>
<p>I would suggest putting this folder within a folder for Code Club, for example: <code>Documents</code> &gt; <code>codeclub</code> &gt; <code>26-au</code> (or put another way: <code>~/Documents/codeclub/26-au</code>). Again, it would be fine to use the same folder from our August session.</p></li>
<li><p>Open this folder in Positron by clicking the <strong>Open Folder</strong> button shown above<sup>1</sup>.</p></li>
<li><p>Positron will reload and ask whether you want to trust the folder. Click <strong>Yes, I trust the authors</strong> and optionally check the box above it.</p></li>
</ol>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/trust.png" class="img-fluid figure-img" style="width:60.0%" alt="Positron dialog asking whether to trust the folder you just opened"></p>
<figcaption>The dialog for trusting a folder.</figcaption>
</figure>
</div>
<p><strong>Why are Projects or open folders needed?</strong> These encourage good practices:</p>
<ul>
<li>Keep each research project in its own self-contained folder</li>
<li>Use paths <em>relative</em> to the folder root, not absolute paths</li>
<li>Avoid using <code>setwd()</code></li>
</ul>
<p>They also make a lot of Positron’s features work better, such as the Explorer, Search, and version control support.</p>
</section>
<section id="setting-up-positron-to-use-github-copilot-with-posit-assistant" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="setting-up-positron-to-use-github-copilot-with-posit-assistant"><span class="header-section-number">2.2</span> Setting up Positron to use GitHub Copilot with Posit Assistant</h2>
<p>You can set up Posit Assistant to work with a variety of different LLMs. Today we will set it up for using GitHub Copilot.</p>
<p>The two main AI tools we have are:</p>
<ol type="1">
<li>Code completions where you get AI suggested completions as you type</li>
<li>Agentic chat using Posit Assistant where you can both get code suggestions and have the agent run the code for you.</li>
</ol>
<section id="get-a-github-account-if-you-dont-have-one" class="level3" data-number="2.2.1">
<h3 data-number="2.2.1" class="anchored" data-anchor-id="get-a-github-account-if-you-dont-have-one"><span class="header-section-number">2.2.1</span> Get a GitHub account if you don’t have one</h3>
<p>In order to use GitHub Copilot with Posit Assistant, you must have a GitHub account.</p>
<p>Create a GitHub account by going to <a href="www.github.com/join">github.com/join</a>.</p>
<ul>
<li>You will have to link to an email address. You can use your OSU email or a personal one, it doesn’t matter.</li>
<li>You will have to pick a username. Some <a href="https://happygitwithr.com/github-acct.html">advice for picking a username</a>:
<ul>
<li>Incorporate your actual name - it’s useful for seeing who you are</li>
<li>Pick a username you’d be comfortable with an employer seeing</li>
<li>Shorter is better</li>
<li>Recommend to use all lower case letters</li>
<li>Your username can be changed but its annoying so try and get it right the first time</li>
</ul></li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can sign up for GitHub education with your OSU email by confirming your status as a student <a href="https://github.com/education">GitHub Education page</a>.</p>
</div>
</div>
<p>Anyone with a GitHub account will be automatically enrolled in the free GitHub Copilot plan, which has a limit of 2,000 code completions per month and ~50 chat requests. That’s not enough for most people to use GitHub Copilot in a sustained way — but as students and teachers/faculty, we can get additional free access after applying for it. In summary (see also this <a href="https://docs.github.com/en/copilot/concepts/billing/individual-plans">GitHub page</a>):</p>
<table class="caption-top table">
<colgroup>
<col style="width: 9%">
<col style="width: 15%">
<col style="width: 28%">
<col style="width: 23%">
<col style="width: 23%">
</colgroup>
<thead>
<tr class="header">
<th>Plan</th>
<th>Free for whom</th>
<th>How to get it</th>
<th>Code completions</th>
<th>Chat requests</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Free</strong></td>
<td>Everyone</td>
<td>Automatically</td>
<td>2,000 per month</td>
<td>~50</td>
</tr>
<tr class="even">
<td><strong>Student</strong></td>
<td>Students</td>
<td>Apply for education benefits</td>
<td>Unlimited</td>
<td>200</td>
</tr>
<tr class="odd">
<td><strong>Pro</strong></td>
<td>Teachers/Faculty</td>
<td>Apply for education benefits</td>
<td>Unlimited</td>
<td>1,500</td>
</tr>
</tbody>
</table>
<p>To apply for educational benefits, the following link will work for teachers/faculty and students alike — just select the appropriate option in the form:</p>
<ol type="1">
<li><p>Go to <a href="https://github.com/settings/education/benefits" class="uri">https://github.com/settings/education/benefits</a>.</p></li>
<li><p>Click “<strong>Start an application</strong>”:</p></li>
<li><p>Fill out the form as needed. <em>(You will likely need a document that proves your current status at the university.)</em></p></li>
</ol>
</section>
<section id="set-your-llm-provider-to-github-copilot" class="level3" data-number="2.2.2">
<h3 data-number="2.2.2" class="anchored" data-anchor-id="set-your-llm-provider-to-github-copilot"><span class="header-section-number">2.2.2</span> Set your LLM provider to GitHub Copilot</h3>
<p>In order to set up the Posit Assistant for AI-assisted plotting, you will need to configure the Posit Assistant extension. We will configure it to use GitHub Copilot.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Want to set up with a different LLM?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Jelmer has created <a href="https://cfaes-bioinfo.github.io/agentic-ai-workshop/o3-positron-install.html#authentication-via-the-osu-litellm-api">some intructions</a> for how to set up Claude Code using your OSU LiteLLM API</p>
</div>
</div>
<p>To do that, call up your Command Palette (<kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>), and we are going to configure which LLMs our Posit Assistant can use. Type in “<em>Authentication: Configure Language Model Providers</em>” and select it.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/authentication-configure-llm.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Authentication: Configure Language Model Providers"></p>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Don’t see Authentication: Configure Language Model Providers as an option?<em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>This may be because:</p>
<ul>
<li>You have an old version of Positron installed. Go to the <a href="">Positron site</a> and download/install the newest version.</li>
<li>You don’t have the Posit Assistant extension installed.
<ol type="1">
<li>Open the Extensions view in Positron by clicking the Extension icon in the Activity Bar (the narrow leftmost sidebar).</li>
<li>In the search box, start typing “Posit Assistant”</li>
<li>Click Install on the “Posit Assistant” extension by Posit Software, PBC.</li>
</ol></li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/positron-extension-posit-assistant.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Posit Assistant extension in the Extensions view of Positron"></p>
</figure>
</div>
</div>
</div>
</div>
<p>This will prompt opening a new window where you can select which LLM provider you want to use. There are more options than what you can see in the screenshot below.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/configure-llm-providers.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Configure Language Model Providers"></p>
</figure>
</div>
<p>Today we are going to select GitHub Copilot, which is free to use for students and academics. You will need to have a GitHub account and sign in to GitHub Copilot. Go ahead and click the orange <code>Connect</code> Positron to GitHub Copilot. A new pop up with open that says “The extension ‘Authentication’ wants to sign in using GitHub. Click the blue <code>Allow</code>. This will open a new browser window where you can sign in to GitHub and authorize Posit Assistant to use GitHub Copilot.</p>
<div class="columns">
<div class="column" style="width:70%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-01-copilot-connect.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Connect to GitHub Copilot"></p>
</figure>
</div>
</div><div class="column" style="width:30%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-02-authenticate-to-github.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="The extension 'Authentication' wants to sign in using GitHub"></p>
</figure>
</div>
</div>
</div>
<p>Next you will get popup that includes a code for authenticating in your browser. You can click <code>Copy &amp; Continue to Browser</code> and the 8 digit code will be added to your clipboard and a browser will open.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-03-code-to-browser.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="A pop up including your code for authenticating with GitHub."></p>
</figure>
</div>
<p>You will need to be logged into a GitHub account to do this - you can see that mine shows up below. You might also have the option here to log in.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-04-device-activation.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Selecting a log in for activating your device with GitHub"></p>
</figure>
</div>
<p>Now you can authorize your device to use GitHub Copilot with Positron. Your code should be ready in your clipboard, you can paste it here using (<kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>V</kbd>)</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-05-device-authorization.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Authorize your device by entering the code you previously copied to your clipboard. It also says what GitHub user you are signed in as"></p>
</figure>
</div>
<p>Now you can authorize VS code to access items within your GitHub account. Yours might look different than mine depending on what groups you are involved with. You can press the green button to <code>Authorize Visual-Studio-Code</code>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-06-authorize-vs-code.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Authorize yVS Code to access your GitHub account with the username you've previously indicated. This allows existing access to reading user profile data, controlling private repos, access user email address and update github action workflows. along with some community access."></p>
</figure>
</div>
<p>Now you should be connected.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-07-congratulations-github-connected.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Congratulations! You have successfully connected to GitHub Copilot!"></p>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Getting a popup that you have not finished authorizing GitHub? There are some instructions below to authorize by creating a Personal Access Token (PAT) <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>While preparing the material for today’s session, I set up GitHub Copilot in Positron about 5 times. Sometimes when I would set up, I would get the pop up on the bottom right of my Positron window:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-08-authorization-pat.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="You have not yet finished authorizing this extension to use GitHub. Would you like to try a different way? (personal access token)."></p>
</figure>
</div>
<p>If you get this prompt, you can click the orange <code>Yes</code>. This will initiate a new popup that asks if you want to Continue to GitHub to create a Personal Access Token (PAT). You can click the blue <code>Continue to GitHub</code>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-09-create-pat.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="A pop up asking if you want to Continue to GitHub to create a Personal Access Token (PAT)."></p>
</figure>
</div>
<p>You should next get a popup asking “Do you want Positron to open the external website?” and you can click the blue <code>Open</code>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-10-positron-open-website.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Positron is asking if you want Positron to open the external website."></p>
</figure>
</div>
<p>This will bring you to your GitHub account online where you can create a Personal Access Token (PAT). You can do this with all the defaults, and you will need to set an expiration date. GitHub recommends you set at 30 days but you can do longer. If you scroll to the bottom of hte page, you will find a green <code>Generate token</code> button - go ahead and click that.</p>
<p>Your PAT will then be created and you can see it. Be sure to copy this, and I like to save this token also in my password manager. Then you can navigate back to Postrion where there should be a prompt asking you for this PAT.</p>
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/github-11-enter-pat-positron.png" class="img-fluid"></p>
<p>Paste your PAT and press <code>Enter</code>. You should now be connected to GitHub Copilot through Positron.</p>
</div>
</div>
</div>
</section>
</section>
<section id="starting-an-r-session" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="starting-an-r-session"><span class="header-section-number">2.3</span> Starting an R session</h2>
<p>Near the <strong>top-right corner</strong> of the Positron window is the so-called <strong>interpreter picker</strong>, which lets you choose which installation of R (or Python) to use for your session.</p>
<p>If Positron found your R installation, it may have started a session already — then it would <em>look similar to what’s shown in the screenshot on the right</em> (with the R version number corresponding to what <em>you</em> have installed).</p>
</section>
</section>
<section id="re-familiarizing-ourself-with-ggplot2-syntax" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Re-familiarizing ourself with <code>ggplot2</code> syntax</h1>
<p>Perhaps it is possible now to use AI to make your plots without understanding any <code>ggplot2</code> or <code>R</code> syntax, but I do think it is much easier, and you’ll have a better time if you understand the basics.</p>
<section id="installing-and-loading-the-tidyverse" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="installing-and-loading-the-tidyverse"><span class="header-section-number">3.1</span> Installing and loading the <code>tidyverse</code></h2>
<p>Before we start going through the syntactical basics, we need to have <code>ggplot2</code> installed in R. If you were here last time, you’ll already have the <code>tidyverse</code> (and <code>ggplot2</code> is one of the packages contain in this meta-package), but if you weren’t then you will need to first download it using <code>install.packages()</code>. Then you can load the <code>tidyverse</code> by calling it with the function <code>library()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Prefer that your output be displayed in the source editor?
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you liked that output would be shown in the source editor, directly below code, like you might be used to in RStudio, you can enable inline output by turning on <a href="positron://settings/quarto.inlineOutput.enabled">quarto.inlineOutput.enabled</a>.</p>
</div>
</div>
<p>We are going to use the <code>penguins</code> dataset that comes preloaded with base R. If you want to see all of these datasets, you can run <code>library(help = "datasets")</code></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(datasets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species     &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ad…
$ island      &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Tor…
$ bill_len    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, 42.0, …
$ bill_dep    &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, 20.2, …
$ flipper_len &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186, 180,…
$ body_mass   &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, 4250, …
$ sex         &lt;fct&gt; male, female, female, NA, female, male, female, male, NA, …
$ year        &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
<p>I am clipping below a bit of the []<code>ggplot2</code> cheatsheet](https://github.com/rstudio/cheatsheets/blob/main/data-visualization.pdf) to see how the developesr explain the basis of plotting.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/ggplot-basics.png" class="img-fluid figure-img"></p>
<figcaption>The basics of <code>ggplot2</code> from the RStudio ggplot2 cheatsheet</figcaption>
</figure>
</div>
<p>The general syntax is like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># start with dataframe and pipe to ggplot()</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> _x<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>variable_, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># inside aes set variables mapped to visual aspects</span></span>
<span id="cb4-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> _y<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>variable_, </span>
<span id="cb4-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> _color<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>variable)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb4-5">    geom_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add a geometry - the type of plot</span></span>
<span id="cb4-6">    scale_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set how data maps to visual aspects </span></span>
<span id="cb4-7">    theme_<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># adjust the overall look of the plot</span></span>
<span id="cb4-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add labels - title, axes, legend</span></span></code></pre></div></div>
</div>
<p>You can find the full <code>ggplot2</code> <a href="https://github.com/rstudio/cheatsheets/blob/main/data-visualization.pdf">cheatsheet</a> also for reference.</p>
</section>
</section>
<section id="simple-plots" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Simple plots</h1>
<p>Let’s start with with asking the Posit Assistant for help with a plot. Let’s ask for:</p>
<blockquote class="blockquote">
<p>“Create a plot using the penguins dataset that plots flipper length versus bill length.”</p>
</blockquote>
<p>When I did this for the first time, I got a pop up asking “Can Posit Assistant use executeCode?” This would mean that the Posit Assistant would like to actually run this code, versus just provide the code for me to run.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/posit-AI_01-executeCode-allow.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:50.0%" alt="Posit Assistant asking if it can execute code."></p>
</figure>
</div>
<p>I am going to click <code>Allow</code> and see what happens. Alternatively you could click <code>Deny</code> and the Posit Assistant will provide you with code that you can copy and paste into your script or <code>.qmd</code> file to run.</p>
<p>When I do this, I get the following code, which I can see in my console:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>There are a few things to note here, Posit AI has:</p>
<ol type="1">
<li>loaded the packages it needs to run the code its specified.</li>
<li>decided that the data we want is in the package <code>palmerpenguins</code> even though I was expecting it to pull from the base R <code>penguins</code> dataset. This is fine, but it is something to be aware of as these two datasets are not identical. For example, the variable names in <code>palmerpenguins::penguins</code> are different than in <code>datasets::penguins</code>.</li>
<li>not used the pipe <code>|&gt;</code> operator which is a more modern way to write code that is considered best practice. This approach is fine but it is something to be aware of.</li>
</ol>
<p>While this is a very good start, let’s see if different prompting can get us a different result. I am now going to ask for:</p>
<blockquote class="blockquote">
<p>“Create a plot using the penguins dataset embedded in base R that plots flipper length versus bill length. Use the pipe operator and add code annotations so I can follow what each line is doing.”</p>
</blockquote>
<p>When I do this, I get the following code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the penguins dataset from datasets package</span></span>
<span id="cb10-2">datasets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the columns we need for plotting</span></span>
<span id="cb10-4">  (\(d) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb10-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bill_length =</span> d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bill_length_mm,</span>
<span id="cb10-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">flipper_length =</span> d<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>flipper_length_mm</span>
<span id="cb10-7">  ))() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove rows with missing values</span></span>
<span id="cb10-9">  (\(d) d[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">complete.cases</span>(d), ])() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb10-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the plot with bill_length on x-axis and flipper_length on y-axis</span></span>
<span id="cb10-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">with</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(</span>
<span id="cb10-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length,</span>
<span id="cb10-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length,</span>
<span id="cb10-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Measurements"</span>,</span>
<span id="cb10-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill Length (mm)"</span>,</span>
<span id="cb10-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length (mm)"</span>,</span>
<span id="cb10-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pch =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">19</span>,</span>
<span id="cb10-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rgb</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>)</span>
<span id="cb10-19">  ))</span></code></pre></div></div>
</div>
<p>This code has a number of problems, is actually worse than my first attempt, and does not work for me at all. I notice a few problems:</p>
<ol type="1">
<li>I asked for the <code>penguins</code> dataset which is a part of base R, and Posit AI has given me a fully base R plot, which is not what I want. I can change my prompt to fix this problem.</li>
<li>Because the assistant thinks I want a base R plot, this leads to addition code which is fully not necessary. There are some steps that aim to extract the columns from the data, which is needed for base R plotting, but this is also done incorrectly. The code we’ve received is (trying) to extract <code>bill_length_mm</code> and <code>flipper_length_mm</code> from <code>penguins</code> but those are not the correct variable names.</li>
</ol>
<p>Let’s try a new prompt:</p>
<blockquote class="blockquote">
<p>“Create a plot using the data datasets::penguins using ggplot2 that plots flipper length versus bill length. Use the pipe operator and add code annotations so I can follow what each line is doing.”</p>
</blockquote>
<p>Now I get this code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the penguins dataset from the datasets package</span></span>
<span id="cb11-4">datasets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb11-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize ggplot with bill_length_mm on x-axis and flipper_length_mm on y-axis</span></span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a scatter plot layer with semi-transparent points</span></span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add labels for the axes and title</span></span>
<span id="cb11-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb11-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Measurements"</span>,</span>
<span id="cb11-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill Length (mm)"</span>,</span>
<span id="cb11-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length (mm)"</span></span>
<span id="cb11-14">  )</span></code></pre></div></div>
</div>
<p>Better but still I have the wrong variable names. What if I try and give the prompt information about my dataset? I am going to paste the output of <code>glimpse(datasets::penguins)</code> into the end of my prompt such that the AI can see exactly what my data looks like.</p>
<blockquote class="blockquote">
<p>Create a plot using the data datasets::penguins using ggplot2 that plots flipper length versus bill length. Use the pipe operator and add code annotations so I can follow what each line is doing. I am pasting below the output of glimpse(datasets::penguins) so you can see what the data looks like.</p>
</blockquote>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(datasets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species     &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Ad…
$ island      &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Tor…
$ bill_len    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, 42.0, …
$ bill_dep    &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, 20.2, …
$ flipper_len &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186, 180,…
$ body_mass   &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, 4250, …
$ sex         &lt;fct&gt; male, female, female, NA, female, male, female, male, NA, …
$ year        &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
<p>Ok now I am getting a functional plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb14-2"></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the penguins dataset from the datasets package</span></span>
<span id="cb14-4">datasets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Initialize ggplot with bill_len on x-axis and flipper_len on y-axis</span></span>
<span id="cb14-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_len, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_len)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a scatter plot layer with semi-transparent points</span></span>
<span id="cb14-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add labels for the axes and title</span></span>
<span id="cb14-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb14-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Measurements"</span>,</span>
<span id="cb14-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill Length (mm)"</span>,</span>
<span id="cb14-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length (mm)"</span></span>
<span id="cb14-14">  )</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>I have gotten a little bit more than I asked for - Posit AI decided that the points should be better (<code>size = 3</code>) and be a bit transparent (<code>alpha = 0.6</code>) which I think is ok. I also have gotten x- and y-axis labels along with a title even though I didn’t ask for that.</p>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-try-it-yourself" class="level2 exercise" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-try-it-yourself"><span class="header-section-number">4.1</span> <i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Try it yourself</h2>
<ol type="1">
<li><p>Try on your own to prompt Posit AI to make you a new plot with this same data. Play around and then we will all share our experiences.</p></li>
<li><p>What have you learned about what makes a successful prompt?</p></li>
</ol>
</section>
</section>
<section id="more-complicated-plotsinvolved-requests" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> More complicated plots/involved requests</h1>
<p>We’ve done some simple plots but what if we want to make something much more complicated? We could think about this in two ways:</p>
<ol type="1">
<li>We could ask for a much more customized plot</li>
<li>We could see how well we think Posit AI can select a visualization tool without providing that information explicitly.</li>
</ol>
<p>Let’s pick a more involved dataset, one we’ve used in Code Club before to have more variables to interrogate. The data we are going to download can be found <a href="https://www.cia.gov/stories/story/spotlighting-the-world-factbook-as-we-bid-a-fond-farewell/">here</a>, though I have saved the file, added it to our Code Club Github, and included some code below for you to download it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(</span>
<span id="cb16-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/osu-codeclub/osu-codeclub.github.io/raw/refs/heads/main/posts/S08E01_wrangling_01/data/factbook.csv"</span>,</span>
<span id="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factbook_download.csv"</span></span>
<span id="cb16-4">)</span></code></pre></div></div>
</div>
<p>Let’s see if we can get Posit AI to read the data in for us. I am going to start with a minimal prompt so we can see how that goes:</p>
<blockquote class="blockquote">
<p>“Read in factbook_download.csv”.</p>
</blockquote>
<p>Posit AI gave me the following message:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/read-in-factbook.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Posit AI message that it is locating the CSV in the project so it can load it into the current R session correctly.."></p>
</figure>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">factbook <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"posts/S12E02_plotting_01/factbook_download.csv"</span>)</span>
<span id="cb17-2">factbook</span></code></pre></div></div>
</div>
<p>Let’s look at this data, using <code>glimpse()</code> (to get a summary).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(factbook)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 11,072
Columns: 20
$ Series.Name    &lt;chr&gt; "Population, total", "Population, total", "Population, …
$ Series.Code    &lt;chr&gt; "SP.POP.TOTL", "SP.POP.TOTL", "SP.POP.TOTL", "SP.POP.TO…
$ Country.Name   &lt;chr&gt; "Afghanistan", "Albania", "Algeria", "American Samoa", …
$ Country.Code   &lt;chr&gt; "AFG", "ALB", "DZA", "ASM", "AND", "AGO", "ATG", "ARG",…
$ X2000..YR2000. &lt;chr&gt; "19542982", "3089027", "30774621", "58230", "66097", "1…
$ X2001..YR2001. &lt;chr&gt; "19688632", "3060173", "31200985", "58324", "67820", "1…
$ X2002..YR2002. &lt;chr&gt; "21000256", "3051010", "31624696", "58177", "70849", "1…
$ X2003..YR2003. &lt;chr&gt; "22645130", "3039616", "32055883", "57941", "73907", "1…
$ X2004..YR2004. &lt;chr&gt; "23553551", "3026939", "32510186", "57626", "76933", "1…
$ X2005..YR2005. &lt;chr&gt; "24411191", "3011487", "32956690", "57254", "79826", "1…
$ X2006..YR2006. &lt;chr&gt; "25442944", "2992547", "33435080", "56837", "80221", "2…
$ X2007..YR2007. &lt;chr&gt; "25903301", "2970017", "33983827", "56383", "78168", "2…
$ X2008..YR2008. &lt;chr&gt; "26427199", "2947314", "34569592", "55891", "76055", "2…
$ X2009..YR2009. &lt;chr&gt; "27385307", "2927519", "35196037", "55366", "73852", "2…
$ X2010..YR2010. &lt;chr&gt; "28189672", "2913021", "35856344", "54849", "71519", "2…
$ X2011..YR2011. &lt;chr&gt; "29249157", "2905195", "36543541", "54310", "70567", "2…
$ X2012..YR2012. &lt;chr&gt; "30466479", "2900401", "37260563", "53691", "71013", "2…
$ X2013..YR2013. &lt;chr&gt; "31541209", "2895092", "38000626", "52995", "71367", "2…
$ X2014..YR2014. &lt;chr&gt; "32716210", "2889104", "38760168", "52217", "71621", "2…
$ X2015..YR2015. &lt;chr&gt; "33753499", "2880703", "39543154", "51368", "71746", "2…</code></pre>
</div>
</div>
<p>We have some problems here:</p>
<ol type="1">
<li>All data is read in as characters, even those that aren’t of the type <code>character</code></li>
<li>Since R by default doesn’t allow column names to start with a number, an <code>X</code> has been added to the beginning of all columns that begin with a number.</li>
</ol>
<p>Let’s see if we can get Posit AI to help us fix this. I am going to prompt:</p>
<blockquote class="blockquote">
<p>“Read in factbook_download.csv. The first four columns should be character type and the rest of the columns are numeric. I want the year columns to start just with the number of the year, with no X preceeding.”</p>
</blockquote>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/read-in-fail1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Posit AI message that it is trying to read in my data"></p>
</figure>
</div>
<p>This is the output I get - and this code doesn’t work for me.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">factbook <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(</span>
<span id="cb20-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factbook_download.csv"</span>,</span>
<span id="cb20-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb20-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb20-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.strings =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".."</span>),</span>
<span id="cb20-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb20-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb20-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span>
<span id="cb20-9">  )</span>
<span id="cb20-10">)</span></code></pre></div></div>
</div>
<p>In my Quarto document, I have a “Fix” option - and I’m going to try that and see how it works.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/fix1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%" alt="Posit AI message that it is trying to read in my data"></p>
</figure>
</div>
<p>We get the following code, which runs. Let’s look at the message and then we can look at the data to see how it all went.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">factbook <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read.csv</span>(</span>
<span id="cb21-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factbook_download.csv"</span>,</span>
<span id="cb21-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">check.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb21-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb21-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.strings =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".."</span>),</span>
<span id="cb21-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colClasses =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb21-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb21-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span>
<span id="cb21-9">  )</span>
<span id="cb21-10">)</span></code></pre></div></div>
</div>
<p>The message we get is:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/fix2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:80.0%"></p>
</figure>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(factbook)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 11,072
Columns: 20
$ `Series Name`   &lt;chr&gt; "Population, total", "Population, total", "Population,…
$ `Series Code`   &lt;chr&gt; "SP.POP.TOTL", "SP.POP.TOTL", "SP.POP.TOTL", "SP.POP.T…
$ `Country Name`  &lt;chr&gt; "Afghanistan", "Albania", "Algeria", "American Samoa",…
$ `Country Code`  &lt;chr&gt; "AFG", "ALB", "DZA", "ASM", "AND", "AGO", "ATG", "ARG"…
$ `2000 [YR2000]` &lt;dbl&gt; 19542982, 3089027, 30774621, 58230, 66097, 16394062, 7…
$ `2001 [YR2001]` &lt;dbl&gt; 19688632, 3060173, 31200985, 58324, 67820, 16941587, 7…
$ `2002 [YR2002]` &lt;dbl&gt; 21000256, 3051010, 31624696, 58177, 70849, 17516139, 7…
$ `2003 [YR2003]` &lt;dbl&gt; 22645130, 3039616, 32055883, 57941, 73907, 18124342, 7…
$ `2004 [YR2004]` &lt;dbl&gt; 23553551, 3026939, 32510186, 57626, 76933, 18771125, 7…
$ `2005 [YR2005]` &lt;dbl&gt; 24411191, 3011487, 32956690, 57254, 79826, 19450959, 7…
$ `2006 [YR2006]` &lt;dbl&gt; 25442944, 2992547, 33435080, 56837, 80221, 20162340, 8…
$ `2007 [YR2007]` &lt;dbl&gt; 25903301, 2970017, 33983827, 56383, 78168, 20909684, 8…
$ `2008 [YR2008]` &lt;dbl&gt; 26427199, 2947314, 34569592, 55891, 76055, 21691522, 8…
$ `2009 [YR2009]` &lt;dbl&gt; 27385307, 2927519, 35196037, 55366, 73852, 22507674, 8…
$ `2010 [YR2010]` &lt;dbl&gt; 28189672, 2913021, 35856344, 54849, 71519, 23364185, 8…
$ `2011 [YR2011]` &lt;dbl&gt; 29249157, 2905195, 36543541, 54310, 70567, 24259111, 8…
$ `2012 [YR2012]` &lt;dbl&gt; 30466479, 2900401, 37260563, 53691, 71013, 25188292, 8…
$ `2013 [YR2013]` &lt;dbl&gt; 31541209, 2895092, 38000626, 52995, 71367, 26147002, 8…
$ `2014 [YR2014]` &lt;dbl&gt; 32716210, 2889104, 38760168, 52217, 71621, 27128337, 8…
$ `2015 [YR2015]` &lt;dbl&gt; 33753499, 2880703, 39543154, 51368, 71746, 28127721, 8…</code></pre>
</div>
</div>
<p>I am an experienced R user, if I was going to read in this data, I would just have used the code below – all of our issues are stemming from the fact that missing values are denoted with a non-standard notation of “..”. I can just pass to <code>read_csv()</code> that the missing values are denoted with “..” and it will read in the data correctly.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">factbook_jess <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factbook_download.csv"</span>,</span>
<span id="cb24-2">                          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">".."</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Rows: 11072 Columns: 20
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (4): Series Name, Series Code, Country Name, Country Code
dbl (16): 2000 [YR2000], 2001 [YR2001], 2002 [YR2002], 2003 [YR2003], 2004 [...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(factbook_jess)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 11,072
Columns: 20
$ `Series Name`   &lt;chr&gt; "Population, total", "Population, total", "Population,…
$ `Series Code`   &lt;chr&gt; "SP.POP.TOTL", "SP.POP.TOTL", "SP.POP.TOTL", "SP.POP.T…
$ `Country Name`  &lt;chr&gt; "Afghanistan", "Albania", "Algeria", "American Samoa",…
$ `Country Code`  &lt;chr&gt; "AFG", "ALB", "DZA", "ASM", "AND", "AGO", "ATG", "ARG"…
$ `2000 [YR2000]` &lt;dbl&gt; 19542982, 3089027, 30774621, 58230, 66097, 16394062, 7…
$ `2001 [YR2001]` &lt;dbl&gt; 19688632, 3060173, 31200985, 58324, 67820, 16941587, 7…
$ `2002 [YR2002]` &lt;dbl&gt; 21000256, 3051010, 31624696, 58177, 70849, 17516139, 7…
$ `2003 [YR2003]` &lt;dbl&gt; 22645130, 3039616, 32055883, 57941, 73907, 18124342, 7…
$ `2004 [YR2004]` &lt;dbl&gt; 23553551, 3026939, 32510186, 57626, 76933, 18771125, 7…
$ `2005 [YR2005]` &lt;dbl&gt; 24411191, 3011487, 32956690, 57254, 79826, 19450959, 7…
$ `2006 [YR2006]` &lt;dbl&gt; 25442944, 2992547, 33435080, 56837, 80221, 20162340, 8…
$ `2007 [YR2007]` &lt;dbl&gt; 25903301, 2970017, 33983827, 56383, 78168, 20909684, 8…
$ `2008 [YR2008]` &lt;dbl&gt; 26427199, 2947314, 34569592, 55891, 76055, 21691522, 8…
$ `2009 [YR2009]` &lt;dbl&gt; 27385307, 2927519, 35196037, 55366, 73852, 22507674, 8…
$ `2010 [YR2010]` &lt;dbl&gt; 28189672, 2913021, 35856344, 54849, 71519, 23364185, 8…
$ `2011 [YR2011]` &lt;dbl&gt; 29249157, 2905195, 36543541, 54310, 70567, 24259111, 8…
$ `2012 [YR2012]` &lt;dbl&gt; 30466479, 2900401, 37260563, 53691, 71013, 25188292, 8…
$ `2013 [YR2013]` &lt;dbl&gt; 31541209, 2895092, 38000626, 52995, 71367, 26147002, 8…
$ `2014 [YR2014]` &lt;dbl&gt; 32716210, 2889104, 38760168, 52217, 71621, 27128337, 8…
$ `2015 [YR2015]` &lt;dbl&gt; 33753499, 2880703, 39543154, 51368, 71746, 28127721, 8…</code></pre>
</div>
</div>
<p>Ok, now that we have data read in - let’s play around with prompting for an interesting and progressingly more complicated plot. Let’s do this together.</p>


</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>Or if that’s somehow not showing, by using the menu <code>File</code> &gt; <code>Open Folder...</code>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>ggplot2</category>
  <category>GitHub Copilot</category>
  <category>LLM</category>
  <category>AI</category>
  <category>Positron</category>
  <category>plotting</category>
  <guid>https://osu-codeclub.github.io/posts/S12E02_plotting_01/</guid>
  <pubDate>Thu, 17 Sep 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S12E02_plotting_01/img/posit-assistant-logo.png" medium="image" type="image/png" height="104" width="144"/>
</item>
<item>
  <title>Getting Started with Positron</title>
  <dc:creator>Jelmer Poelstra</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S12E01_positron_01/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<section id="what-well-cover" class="level3" data-number="1.1">
<h3 data-number="1.1" class="anchored" data-anchor-id="what-well-cover"><span class="header-section-number">1.1</span> What we’ll cover</h3>
<p>Every Code Club session so far has used <strong>RStudio</strong>. Today we’ll spend the session in a different program: <strong><a href="https://positron.posit.co/">Positron</a></strong>, a newer data science IDE from Posit, the same company that makes RStudio.</p>
<p>By the end of the session, you should be able to open your own work in Positron, find your way around, and decide for yourself whether you want to keep using it. Specifically, we’ll cover:</p>
<ul>
<li><strong>Installing Positron and opening a folder</strong></li>
<li><strong>Touring the interface</strong></li>
<li><strong>Exploring data</strong> — the Data Explorer, Variables pane, and Plots pane</li>
<li><strong>Customizing Positron</strong> — keyboard shortcuts, settings, and extensions</li>
</ul>
</section>
<section id="why-is-there-a-new-ide" class="level3" data-number="1.2">
<h3 data-number="1.2" class="anchored" data-anchor-id="why-is-there-a-new-ide"><span class="header-section-number">1.2</span> Why is there a new IDE?</h3>
<p>Basically, Posit wanted better support for other programming languages, particularly Python, alongside R. This turned out to be harder with RStudio than starting fresh, so they created a new IDE: Positron.</p>
<p>Positron is built on <a href="https://github.com/microsoft/vscode">Code OSS</a>, the open-source version of the popular general-purpose code IDE called Visual Studio Code (<strong>VS Code</strong>). It adds data science features that RStudio has but VS Code does not, such as a Variables pane, a Plots pane, and a Data Explorer.</p>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-discussion" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-discussion"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Discussion</h4>
<ul>
<li>Which languages do you use besides R — Python, bash, SQL, none?</li>
<li>Has anyone tried Positron already? Did you stick with it?</li>
<li>Do you consider yourself familiar with the basics of RStudio?</li>
</ul>
</section>
</section>
<section id="why-should-you-consider-switching" class="level3" data-number="1.3">
<h3 data-number="1.3" class="anchored" data-anchor-id="why-should-you-consider-switching"><span class="header-section-number">1.3</span> Why should you consider switching?</h3>
<p>If you use Python alongside R, or are already familiar with VS Code, Positron is a natural choice and I’d strongly recommend switching.</p>
<p>If you use R only, you may not need to switch, but should still consider it, because:</p>
<ul>
<li><p><strong>New features</strong>: While Posit has committed to maintaining and updating RStudio, major new features are now going to Positron first. And some features may never make it to RStudio, because Positron is more adaptable.</p></li>
<li><p><strong>AI support</strong>: This is much more flexible in Positron, where you can connect to different model providers and subscriptions, and even use Claude Code. In RStudio, you can only use AI via a Posit AI subscription. We’ll demonstrate using AI assistance in Positron in the next session!</p></li>
</ul>
<p>And two more minor points:</p>
<ul>
<li><p><strong>Data Explorer</strong>: Positron’s Data Explorer can open data files (CSV, Excel, etc.) directly, without having to load them in R at all.</p></li>
<li><p><strong>Remote connections</strong>: A niche feature, but it was important to me: Positron can be connected to the Ohio Supercomputer Center and any other remote server. This isn’t possible with RStudio and while you can use RStudio directly at OSC through OSC OnDemand, this has its downsides. (Happy to expand on this for any OSC users!)</p></li>
</ul>
</section>
<section id="before-we-start" class="level3" data-number="1.4">
<h3 data-number="1.4" class="anchored" data-anchor-id="before-we-start"><span class="header-section-number">1.4</span> Before we start</h3>
<p>If you haven’t done so already, please:</p>
<ol type="1">
<li><p><strong>Install Positron</strong>: download the appropriate installer from the <a href="https://positron.posit.co/download.html" target="_blank">download page</a>, and run it. There are installers for Windows, macOS, and Linux.</p></li>
<li><p><strong>Make sure you have a recent R</strong> (4.4.0 or higher) installed; Positron does not come with its own copy of R.</p></li>
</ol>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>Using an OSU-managed computer and have no admin rights?
</div>
</div>
<div class="callout-body-container callout-body">
<p>You should still be able to install Positron, just do so at the user level rather than system-wide.</p>
<p>On a Mac, this involves dragging the Positron app into your <strong>User</strong> Applications folder, <code>/Users/&lt;username&gt;/Applications</code> (i.e.&nbsp;this Applications folder is in the same parent folder as your Documents, Downloads, and Desktop folder).</p>
<p>On Windows, the Positron installer has a <strong>“User install”</strong> option. Look for a checkbox or dropdown labeled <strong>“Install for me only”</strong> (vs.&nbsp;“for all users”) during setup — that keeps the install in your user profile without needing admin rights.</p>
</div>
</div>
</section>
</section>
<section id="getting-started" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="getting-started"><span class="header-section-number">2</span> Getting started</h2>
<p>Go ahead and start Positron! It should look something like this:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/positron-whole.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:98.0%" alt="Screenshot of the Positron IDE, with the Editor, Console, and various panes"></p>
</figure>
</div>
<section id="opening-a-folder-replaces-rstudio-projects" class="level3" data-number="2.1">
<h3 data-number="2.1" class="anchored" data-anchor-id="opening-a-folder-replaces-rstudio-projects"><span class="header-section-number">2.1</span> Opening a folder (replaces RStudio Projects!)</h3>
<div class="columns">
<div class="column" style="width:45%;">
<p>When you first start Positron, you should see something like what’s shown in the screenshot on the right, prompting you to <strong>open a folder</strong>.</p>
<p>“Opening a folder” is Positron’s <strong>equivalent of opening an RStudio Project</strong>. The idea is the same: a folder is a self-contained workspace, and opening it sets your working directory and gives you a fresh R session.</p>
<p>But instead of requiring the special <code>.Rproj</code> file that is created when you make an RStudio Project, Positron simply <strong>treats any folder you open as a project</strong>.</p>
<p><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Let’s create and open a new folder for today’s session:</p>
</div><div class="column" style="width:55%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/open-folder.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:85.0%" alt="Positron welcome screen, with a button to open a folder"></p>
</figure>
</div>
</div>
</div>
<ol type="1">
<li><p>Create a new folder on your computer. You can create it in any way, such as with your File Explorer / Finder.</p>
<p>I would suggest putting this folder within a folder for Code Club, for example: <code>Documents</code> &gt; <code>codeclub</code> &gt; <code>positron</code> (or put another way: <code>~/Documents/codeclub/positron</code>).</p></li>
<li><p>Open this folder in Positron by clicking the <strong>Open Folder</strong> button shown above<sup>1</sup>.</p></li>
<li><p>Positron will reload and ask whether you want to trust the folder. Click <strong>Yes, I trust the authors</strong> and optionally check the box above it.</p></li>
</ol>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/trust.png" class="img-fluid figure-img" style="width:70.0%" alt="Positron dialog asking whether to trust the folder you just opened"></p>
<figcaption>The dialog for trusting a folder.</figcaption>
</figure>
</div>
<p><strong>Why are Projects or open folders needed?</strong> These encourage good practices:</p>
<ul>
<li>Keep each research project in its own self-contained folder</li>
<li>Use paths <em>relative</em> to the folder root, not absolute paths</li>
<li>Avoid using <code>setwd()</code></li>
</ul>
<p>They also make a lot of Positron’s features work better, such as the Explorer, Search, and version control support.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>More on RStudio Projects vs.&nbsp;Positron folders
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you open a folder that already contains an <code>.Rproj</code> file, Positron simply ignores the file. So a folder can work as both an RStudio Project and a Positron workspace, which is perhaps handy while you’re trying Positron out.</p>
<p>The good thing about Positron’s mechanism is that you’re practically forced to use it, whereas in RStudio using Projects is optional and easy to skip or not know about.</p>
</div>
</div>
</section>
<section id="starting-an-r-session" class="level3" data-number="2.2">
<h3 data-number="2.2" class="anchored" data-anchor-id="starting-an-r-session"><span class="header-section-number">2.2</span> Starting an R session</h3>
<div class="columns">
<div class="column" style="width:60%;">
<p>Near the <strong>top-right corner</strong> of the Positron window is the so-called <strong>interpreter picker</strong>, which lets you choose which installation of R (or Python) to use for your session.</p>
<p>If Positron found your R installation, it may have started a session already — then it would look like something on the right (with the R version number corresponding to what you have installed).</p>
</div><div class="column" style="width:40%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/interpreter.png" class="img-fluid figure-img" style="width:90.0%" alt="Screenshot of the Positron interpreter picker"></p>
<figcaption>The interpreter picker.</figcaption>
</figure>
</div>
</div>
</div>
<p><strong>If not, click the picker</strong>, and choose your version of R (you may have to click <strong>New Console Session</strong> before you see the options).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/interpreter-pick.png" class="img-fluid figure-img" style="width:80.0%" alt="Screenshot of the Positron interpreter picker with options"></p>
<figcaption>The dialog for selecting an interpreter after you click the picker.</figcaption>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>More on R and Python sessions in Positron
</div>
</div>
<div class="callout-body-container callout-body">
<p>The picker lists <em>every</em> interpreter Positron can find, which could include several versions of R and Python.</p>
<p>Two nice features of Positron:</p>
<ul>
<li>Switching between R versions is a single click here, which is considerably easier than in RStudio.</li>
<li>You can also have several sessions running at once. Say, an R session and a Python session, or two R sessions on different R versions.</li>
</ul>
<p>On the other hand, if you’re an R user only and have no need to switch between different R versions, you might prefer the simplicity of RStudio’s approach.</p>
</div>
</div>
<p>Once a session is running, the R <strong>Console</strong> works just like in RStudio. Try it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(iris)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa</code></pre>
</div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/console.png" class="img-fluid figure-img" style="width:90.0%" alt="Screenshot of the Positron console, showing a simple calculation and the head of the iris dataset"></p>
<figcaption>The Positron console.</figcaption>
</figure>
</div>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise</h4>
<hr style="height:1pt; visibility:hidden;">
<p><strong>A) Confirm your working directory</strong></p>
<p>Confirm that opening a folder really did set your working directory: run <code>getwd()</code> and check that the path matches the folder you opened.</p>
<p><strong>B) Try running two sessions</strong></p>
<p>Start a <em>second</em> R session from the interpreter picker (click the picker, then <strong>New Console Session</strong>). You likely only have a single R version installed, and can pick the same version for the second session.</p>
<p>You should now have two Console sessions, and you can see this as shown in the screenshot:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/two-sessions.png" class="img-fluid figure-img" style="width:80.0%" alt="Screenshot of Positron with two console sessions"></p>
<figcaption>Two console sessions in Positron: R 4.6.0 is shown twice, and each of these is a session.</figcaption>
</figure>
</div>
<p>Click on the second (bottom) session, and run:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span></code></pre></div></div>
</div>
<p>Then switch back to the first session, and run:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">x</span></code></pre></div></div>
<div class="cell-output cell-output-error">
<pre><code>Error:
! object 'x' not found</code></pre>
</div>
</div>
<p>If you get the above error, that means the two sessions are independent: objects created in one session do not exist in the other.</p>
</section>
</section>
<section id="packages-and-a-script" class="level3" data-number="2.3">
<h3 data-number="2.3" class="anchored" data-anchor-id="packages-and-a-script"><span class="header-section-number">2.3</span> Packages and a script</h3>
<p>We’ll use the <code>flights</code> dataset from the <strong>nycflights13</strong> package: every flight that departed New York City in 2013. I picked a relatively large dataset, so we can see some of the nice features of the Data Explorer later.</p>
<p>First, let’s install the packages we need:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyverse"</span>)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nycflights13"</span>)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Already have these packages installed?
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you already have them or think you do, you can skip the installation step and try running the <code>library()</code> calls below directly. Alternatively, it won’t hurt to run the <code>install.packages()</code> calls again — R will simply check that you have the latest version and skip the installation if you do.</p>
</div>
</div>
<p>Now, let’s <strong>create a new R script</strong>:</p>
<ul>
<li><p>This can be done in several ways, but here is one: Click <code>File</code> (or the “New” button) &gt; <code>New File...</code>, then select “R File”.</p></li>
<li><p>Save it right away (<code>File</code> &gt; <code>Save</code> or <code>Save As</code>, or <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>S</kbd>) in your current folder as <code>explore.R</code>.</p></li>
</ul>
<p>I recommend that you <strong>add all the code below to this script</strong>, and run it from the script (instead of typing it into the console).</p>
<p>Load the two packages we’ll use:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(nycflights13)</span></code></pre></div></div>
</div>
<p>Run the code with <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>Enter</kbd> — the same shortcut as in RStudio. Also as in RStudio, this moves the cursor down after running the current line (or your selection of a part of a line or multiple lines).</p>
</section>
<section id="the-command-palette" class="level3" data-number="2.4">
<h3 data-number="2.4" class="anchored" data-anchor-id="the-command-palette"><span class="header-section-number">2.4</span> The Command Palette</h3>
<p>The Command Palette is Positron’s <strong>main way to access the program’s functionality</strong>. It may take some getting used to, but is very handy and powerful.</p>
<p>To open the Command Palette, press <kbd>Cmd</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Mac) or <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> (Windows).</p>
<p>A search box will appear at the top of the window: start typing what you want to do, and Positron will fuzzy-match it against every piece of functionality (command) it knows. Here are some things you might think of looking for in the Command Palette:</p>
<ul>
<li>Type <code>theme</code> → <em>Preferences: Color Theme</em></li>
<li>Type <code>restart</code> → <em>Interpreter: Restart Active Interpreter Session</em></li>
<li>Type <code>settings</code> → <em>Preferences: Open Settings (UI)</em></li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Keyboard shortcuts
</div>
</div>
<div class="callout-body-container callout-body">
<p>Pay attention to the keyboard shortcuts it shows next to each command that has one. For commonly used commands, it’s worth learning the shortcut so you can skip the palette next time.</p>
<p>Side note: RStudio has a Command Palette as well, but it’s more essential in Positron, because Positron has fewer menus and buttons, but more options.</p>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-find-your-way-around" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-find-your-way-around"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Find your way around</h4>
<p>Use the Command Palette to do the following:</p>
<ol type="1">
<li><p>Change the <strong>color theme</strong> to something you like better than the default.</p></li>
<li><p><strong>Restart your R session</strong>, then run <code>ls()</code> in the console to confirm that your workspace is now empty.</p></li>
<li><p>Run your whole script <em>(there’s a command for this — find it!)</em>.</p></li>
</ol>
<details>
<summary>
<em>Click here to see hints</em>
</summary>
<p>For each of these, open the palette with <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> and type:</p>
<ol type="1">
<li><code>theme</code> — look for <em>Preferences: Color Theme</em>, then use the arrow keys to preview themes live before hitting Enter</li>
<li><code>restart</code> — look for <em>Interpreter: Restart Active Interpreter Session</em></li>
<li><code>run</code> — look for <em>R: Run R File in Console</em> or similar</li>
</ol>
<p>Notice how each of these showed you a keyboard shortcut on the right-hand side.</p>
</details>
</section>
</section>
</section>
<section id="a-tour-of-the-interface" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="a-tour-of-the-interface"><span class="header-section-number">3</span> A tour of the interface</h2>
<p>If you’ve used RStudio before, Positron’s layout should feel broadly familiar. There’s an editor to write code, a console to run it, and various panes to display results. But several things have moved, and there are some aspects without an RStudio equivalent.</p>
<p>Your interface should currently look something like this:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/positron-whole2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:90.0%" alt="Screenshot of the Positron IDE, with the Editor, Console, and various panes"></p>
</figure>
</div>
<section id="the-five-regions" class="level3" data-number="3.1">
<h3 data-number="3.1" class="anchored" data-anchor-id="the-five-regions"><span class="header-section-number">3.1</span> The five regions</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/user-interface-for-rstudio-migration.jpeg" class="no-border img-fluid quarto-figure quarto-figure-center figure-img" style="width:98.0%" alt="Diagram of the Positron interface, with the five regions labeled: Editor, Panel, Activity Bar, Primary Side Bar, Secondary Side Bar. Image from <https://positron.posit.co/layout.html>"></p>
</figure>
</div>
<table class="table-striped caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 18%">
<col style="width: 62%">
</colgroup>
<thead>
<tr class="header">
<th>Region</th>
<th>Where</th>
<th>What’s in it</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Editor</strong></td>
<td>Center - top</td>
<td>Open scripts and other documents, in tabs</td>
</tr>
<tr class="even">
<td><strong>Panel</strong></td>
<td>Center - bottom</td>
<td>R console and Terminal</td>
</tr>
<tr class="odd">
<td><strong>Activity Bar</strong></td>
<td>Far left</td>
<td>Icons that <em>switch</em> what the Primary Side Bar shows</td>
</tr>
<tr class="even">
<td><strong>Primary Side Bar</strong></td>
<td>Left</td>
<td>Explorer (files), Search, Source Control, Extensions, Packages</td>
</tr>
<tr class="odd">
<td><strong>Secondary Side Bar</strong></td>
<td>Right</td>
<td>Variables, Plots, Help, Viewer, History, Connections</td>
</tr>
</tbody>
</table>
<hr style="height:1pt; visibility:hidden;">
<p>Almost every pane in Positron can be <strong>resized</strong> (like in RStudio) and <strong>dragged somewhere else</strong> (much more flexible than in RStudio). There are also layout presets.</p>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-modify-the-layout" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-modify-the-layout"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Modify the layout</h4>
<ul>
<li><p>Resize panes such as the Primary Side Bar and the Editor.</p></li>
<li><p>Drag the <strong>Help</strong> tab down to the bottom, next to the Console, and see whether you prefer it there. Drag it back if you want.</p></li>
<li><p>Try moving something else. If you make a mess of things, you can always get back to the default layout with the <em>View: Reset View Locations</em> command.</p></li>
<li><p><strong>Explore the presets</strong>: Open the Command Palette (<kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>) and type <code>layout</code>. Select “<strong>Customize Layout…</strong>” and you should see several options for layout presets. Try them.</p></li>
<li><p>The Customize Layout dialog also lets you <strong>hide or move regions</strong> — these options are below the presets. For example, try clicking on Activity Bar to hide it.</p></li>
</ul>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/layout-dialog.png" class="img-fluid figure-img" style="width:70.0%" alt="Screenshot of the Positron Customize Layout dialog"></p>
<figcaption>The Customize Layout dialog</figcaption>
</figure>
</div>
</section>
<hr style="height:1pt; visibility:hidden;">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Where did my RStudio pane go? <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<table class="table-striped table-hover caption-top table">
<colgroup>
<col style="width: 30%">
<col style="width: 69%">
</colgroup>
<thead>
<tr class="header">
<th>In RStudio</th>
<th>In Positron</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Source pane</td>
<td>Editor (center)</td>
</tr>
<tr class="even">
<td>Console</td>
<td>Console (bottom)</td>
</tr>
<tr class="odd">
<td>Environment</td>
<td><strong>Variables</strong> pane (right)</td>
</tr>
<tr class="even">
<td>Plots</td>
<td><strong>Plots</strong> pane (right)</td>
</tr>
<tr class="odd">
<td>Help</td>
<td><strong>Help</strong> pane (right), or press <code>F1</code> on a function</td>
</tr>
<tr class="even">
<td>Files</td>
<td><strong>Explorer</strong> (Activity Bar, far left)</td>
</tr>
<tr class="odd">
<td>Packages</td>
<td><strong>Packages</strong> pane (Activity Bar)</td>
</tr>
<tr class="even">
<td>Git</td>
<td><strong>Source Control</strong> (Activity Bar)</td>
</tr>
<tr class="odd">
<td>Viewer</td>
<td><strong>Viewer</strong> pane (right)</td>
</tr>
<tr class="even">
<td>Terminal</td>
<td>A tab next to the Console</td>
</tr>
<tr class="odd">
<td>History</td>
<td><strong>History</strong> pane (right)</td>
</tr>
<tr class="even">
<td>Build</td>
<td><em>No pane</em> — use the Command Palette</td>
</tr>
<tr class="odd">
<td><code>View(df)</code> data viewer</td>
<td><strong>Data Explorer</strong> (opens as an editor tab)</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="the-activity-and-primary-side-bars" class="level3" data-number="3.2">
<h3 data-number="3.2" class="anchored" data-anchor-id="the-activity-and-primary-side-bars"><span class="header-section-number">3.2</span> The Activity and Primary Side Bars</h3>
<p>The <strong>Activity Bar</strong> is the strip of icons on the far left. Clicking an icon swaps out what’s shown in the <strong>Primary Side Bar</strong> right next to it. Among the options are:</p>
<section id="explorer" class="level4" data-number="3.2.1">
<h4 data-number="3.2.1" class="anchored" data-anchor-id="explorer"><span class="header-section-number">3.2.1</span> Explorer</h4>
<p>The <strong>Explorer</strong> (the top icon, looks like stacked documents) shows the folders and files in your currently open folder. Clicking a file will open it in the Editor.</p>
<p>This is Positron’s equivalent of RStudio’s <strong>Files</strong> pane, but a bit nicer as you can see the whole project’s structure at once and expand/collapse subfolders.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>A faster way to jump to a file: Cmd/Ctrl+P
</div>
</div>
<div class="callout-body-container callout-body">
<p>Especially when your project has many subfolders and files, it can be tedious to click through the Explorer tree to find a file. The <strong>Go to File</strong> feature is a faster way to jump to a file in your project, as long as you know part of its name (this is similar to RStudio’s <strong>Go to File/Function</strong>).</p>
<p>Try it: press <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>P</kbd> and type <code>expl</code> — your <code>explore.R</code> script should be the top hit. Press <kbd>Enter</kbd> to jump to it.</p>
</div>
</div>
</section>
<section id="search" class="level4" data-number="3.2.2">
<h4 data-number="3.2.2" class="anchored" data-anchor-id="search"><span class="header-section-number">3.2.2</span> Search</h4>
<p>The <strong>Search</strong> pane lets you search, and optionally replace, text across <em>every file</em> in your open folder.</p>
<p>Try it: open Search by clicking the <strong>Search</strong> icon (magnifying glass) in the Activity Bar, then type <code>tidyverse</code> in the search box. Currently, there’s only one match, but we’ll see more later. Note that you can click on a result to jump straight to that line.</p>
<p>This is especially useful once your project grows to span multiple scripts. For example, finding every place you reference a column name before renaming it, or every script that loads a particular package.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/search.png" class="img-fluid figure-img" style="width:55.0%" alt="Screenshot of the Positron Search pane, showing a search for 'tidyverse' and one match in explore.R"></p>
<figcaption>The search pane</figcaption>
</figure>
</div>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-search-and-replace" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-search-and-replace"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Search and replace</h4>
<ol type="1">
<li><p>Create a second script in your folder, <code>plots.R</code> with these two lines in it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TODO</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: plot the distribution of arr_delay for the flights data</span></span></code></pre></div></div></li>
<li><p>Now that your project has two files, use <strong>Search</strong> to look for <code>tidyverse</code> again, paying attention to how the results are reported and organized.</p></li>
<li><p>Type <code>tidymodels</code> in the search box’s <strong>replace</strong> field: Positron previews every change it <em>would</em> make, right in the results list. Have a look — then clear the replace field, since we don’t actually want to rename anything.</p></li>
</ol>
</section>
<section id="other-sidebar-options" class="level4" data-number="3.2.3">
<h4 data-number="3.2.3" class="anchored" data-anchor-id="other-sidebar-options"><span class="header-section-number">3.2.3</span> Other sidebar options</h4>
<p>There are a few more icons in the Activity Bar:</p>
<ul>
<li><strong>Source Control</strong>: for Git (also much improved over RStudio’s Git pane!)</li>
<li><strong>Run and Debug</strong>: for debugging code</li>
<li><strong>Remote Explorer</strong>: for remote connections</li>
<li><strong>Extensions</strong>: for installing add-ons — more on that in a minute</li>
<li><strong>Testing</strong>: for running tests on your code</li>
<li><strong>Packages</strong>: lists installed R/Python packages (Positron’s equivalent of RStudio’s Packages pane)</li>
</ul>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-explore-the-packages-pane" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-explore-the-packages-pane"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Bonus exercise: Explore the Packages pane</h4>
<p>Click on the <strong>Packages</strong> icon (cube shape) in the Activity Bar, and you’ll see every package installed in your library, with:</p>
<ul>
<li>its <em>version</em>, and whether it’s attached (i.e.&nbsp;you ran <code>library()</code> on it)</li>
<li>a filter to show only <em>outdated</em> packages</li>
<li>buttons to <em>install</em>, <em>update</em>, and <em>remove</em> packages</li>
<li>links to each package’s documentation and website</li>
</ul>
<p>Explore this and try, for example, to filter to outdated packages (if any), and optionally update one or two of them.</p>
</section>
</section>
</section>
<section id="exploring-data" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="exploring-data"><span class="header-section-number">4</span> Exploring data</h2>
<p>Now, we’ll use the flights data to explore some of Positron’s data exploration features.</p>
<p>To start, <code>nycflights13</code> provides the <code>flights</code> data frame, but it won’t show up as an object in your environment yet. Add the following line to <code>explore.R</code> and run it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">flights <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> flights</span></code></pre></div></div>
</div>
<section id="the-variables-pane" class="level3" data-number="4.1">
<h3 data-number="4.1" class="anchored" data-anchor-id="the-variables-pane"><span class="header-section-number">4.1</span> The Variables pane</h3>
<p>Look at the <strong>Variables</strong> pane on the right: <code>flights</code> should now be listed there. It’s the equivalent of RStudio’s Environment pane, and like there, you can <strong>expand objects</strong> to look inside them by clicking the arrow next to an object’s name.</p>
<p>Try expanding <code>flights</code>. You should see each column, its type, and a preview of its contents.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/flights_var.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:55.0%" alt="Screenshot of the Positron Variables pane, showing the flights data frame"></p>
</figure>
</div>
</section>
<section id="the-data-explorer" class="level3" data-number="4.2">
<h3 data-number="4.2" class="anchored" data-anchor-id="the-data-explorer"><span class="header-section-number">4.2</span> The Data Explorer</h3>
<p>The Data Explorer is Positron’s equivalent of RStudio’s <code>View()</code> function, but with a lot more functionality. Click the small <strong>table icon</strong> next to <code>flights</code> in the Variables pane, and it should open <em>as an editor Tab</em> and look something like this:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/data-explorer.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:85.0%" alt="Screenshot of the Positron Variables pane, showing the flights data frame with a small table icon next to it"></p>
</figure>
</div>
<p>The Data Explorer has three parts:</p>
<ul>
<li>The <strong>data grid</strong> — the spreadsheet-like view of your rows and columns</li>
<li>The <strong>summary panel</strong> — per-column statistics and a small distribution plot. <em><span style="color: salmon;">May be hidden by default: if you don’t see it, look for an arrow on the left-hand side of the spreadsheet view.</span></em></li>
<li>The <strong>filter bar</strong> — for temporarily filtering rows</li>
</ul>
<section id="sorting-and-filtering" class="level4" data-number="4.2.1">
<h4 data-number="4.2.1" class="anchored" data-anchor-id="sorting-and-filtering"><span class="header-section-number">4.2.1</span> Sorting and filtering</h4>
<ul>
<li><p><strong>Sort</strong> by clicking the three dots in the column header and picking <code>Sort Ascending</code> or <code>Sort Descending</code>.</p></li>
<li><p><strong>Filter</strong> by clicking <code>+ Add Filter</code> in the filter bar, picking a column, and choosing a condition. You can add several filters, and they will combine.</p></li>
</ul>
<p>Sorting and filtering in the Data Explorer <strong>does not</strong> change the dataframe in your R session, but there is a <strong>“Convert to Code” button (!)</strong> that generates the R code to reproduce your filter and sort.</p>
<div class="columns">
<div class="column">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/convert2code1.png" class="img-fluid figure-img" style="width:90.0%" alt="Screenshot of the Positron Data Explorer, showing the Convert to Code button"></p>
<figcaption>Above: When you’ve applied one or more filters, the Convert to Code button is activated (no longer grayed out).<br><br>Right: After clicking Convert to Code, you should see a popup with the generated R code.</figcaption>
</figure>
</div>
</div><div class="column">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/convert2code2.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:90.0%" alt="Screenshot of the Positron Data Explorer, showing the generated R code"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-explore-the-flights-data" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-explore-the-flights-data"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Explore the flights data</h4>
<p>Use the <strong>Data Explorer</strong> to answer these:</p>
<ol type="1">
<li><p>Which column has the <strong>most missing values</strong>? <em>(Hint: the summary panel shows a missing-value percentage per column.)</em></p></li>
<li><p>How many <strong>distinct airline carriers</strong> (<code>carrier</code>) are in the data?</p></li>
<li><p>Filter to flights that departed from <code>JFK</code> <strong>and</strong> had a departure delay (<code>dep_delay</code>) of more than 120 minutes. Roughly how many rows are left? <em>(Look for the row count displayed in the Data Explorer —</em> <em>it reflects your active filters.)</em></p></li>
<li><p>Sort those filtered flights by <code>dep_delay</code>, descending. What was the single worst departure delay out of JFK, in hours?</p></li>
<li><p>Use the Convert to Code button to get code corresponding to what you did in steps 3 and 4, and copy it into your <code>explore.R</code> script.</p></li>
</ol>
</section>
<section id="opening-data-files-directly-in-the-data-explorer" class="level4" data-number="4.2.2">
<h4 data-number="4.2.2" class="anchored" data-anchor-id="opening-data-files-directly-in-the-data-explorer"><span class="header-section-number">4.2.2</span> Opening data files directly in the Data Explorer</h4>
<p>You can <strong>open a data file directly in the Data Explorer</strong>, without any R code (this is a feature RStudio does not have at all). Let’s start by creating a CSV file from the <code>flights</code> dataset, so we have a data file to work with:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make a copy of the data as a CSV file, so we have a data file to work with</span></span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>)</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_csv</span>(flights, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/flights.csv"</span>)</span></code></pre></div></div>
</div>
<p>Next, open <code>data/flights.csv</code> via the Explorer or with <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>P</kbd>. The file will open directly in the Data Explorer! And this will also work for <code>.tsv</code> and <strong>even <code>.xlsx</code> (Excel) files</strong>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Want to see the raw text of a data file instead?
</div>
</div>
<div class="callout-body-container callout-body">
<p>For plain-text files, if you want to see the raw text instead of the Data Explorer, click the “Open as Plain Text File” button:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/data-explorer-show-plaintext.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:70.0%" alt="Screenshot of the Positron Data Explorer, showing the Show Plain Text button"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="plots" class="level3" data-number="4.3">
<h3 data-number="4.3" class="anchored" data-anchor-id="plots"><span class="header-section-number">4.3</span> Plots</h3>
<p>Let’s create a plot, so we can see where it appears. Copy the following code into the <code>plots.R</code> script and run it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(flights, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> arr_delay)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tomato1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_x_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(</span>
<span id="cb13-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expansion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)),</span>
<span id="cb13-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comma_format</span>()</span>
<span id="cb13-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arrival delay (minutes)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of flights"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>())</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 10041 rows containing non-finite outside the scale range
(`stat_bin()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_bar()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The <strong>Plots</strong> pane behaves much like RStudio’s, including the back/forward arrows through your plot history. Two things to try:</p>
<ul>
<li>The <strong>expand</strong> button, which pops the plot out into a large editor tab.</li>
<li>Click the <strong>circle icon</strong> (to the right of “Auto”), and you should see options “Dark Filter” and “Follow Theme” (and the default “No Filter”): Dark Filter will show the plot on a dark background, and Follow Theme will match your color theme. Handy for those who prefer a dark theme for their IDE!</li>
</ul>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>The Help pane
</div>
</div>
<div class="callout-body-container callout-body">
<p>Like in RStudio, you can get help on a function by typing <code>?function_name</code> in the console, or by putting your cursor inside a function name in your script and pressing <kbd>F1</kbd>. Give one or both methods a go with <code>geom_histogram</code>.</p>
</div>
</div>
</section>
</section>
<section id="customize-positron" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="customize-positron"><span class="header-section-number">5</span> Customize Positron</h2>
<p>Let’s finish by looking at some ways to customize Positron to your liking.</p>
<section id="extensions" class="level3" data-number="5.1">
<h3 data-number="5.1" class="anchored" data-anchor-id="extensions"><span class="header-section-number">5.1</span> Extensions</h3>
<p>Positron has <em>many</em> extensions (add-ins) that you can install, in large part because most <strong>VS Code extensions</strong> are compatible with Positron. To find and install extensions, click the <strong>Extensions</strong> icon in the Activity Bar (four blocks, with one set apart from the other three). Some useful ones:</p>
<table class="table-striped caption-top table">
<colgroup>
<col style="width: 37%">
<col style="width: 62%">
</colgroup>
<thead>
<tr class="header">
<th>Extension</th>
<th>What it does</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Code Spell Checker</strong></td>
<td>Underlines misspelled words in code and prose</td>
</tr>
<tr class="even">
<td><strong>Better Comments</strong></td>
<td>Color-codes certain comments by type (TODOs, questions, etc.)</td>
</tr>
<tr class="odd">
<td><strong>Pastum</strong></td>
<td>Converts a table copied to your clipboard (!) <sup>2</sup></td>
</tr>
<tr class="even">
<td><strong>Indent Rainbow</strong></td>
<td>Colorizes indentation levels in scripts</td>
</tr>
<tr class="odd">
<td><strong>Rainbow CSV</strong></td>
<td>Colorizes CSV columns when viewing as plain text</td>
</tr>
<tr class="even">
<td><strong>Air</strong></td>
<td>R code formatter</td>
</tr>
<tr class="odd">
<td><strong>Quarto</strong></td>
<td>Quarto support</td>
</tr>
<tr class="even">
<td><strong>Tomorrow Night Bright (R Classic)</strong></td>
<td>The RStudio color theme</td>
</tr>
<tr class="odd">
<td><strong>SVG Preview</strong></td>
<td>Enables viewing of <code>.svg</code> files</td>
</tr>
</tbody>
</table>
<section id="automatic-code-formatting" class="level4" data-number="5.1.1">
<h4 data-number="5.1.1" class="anchored" data-anchor-id="automatic-code-formatting"><span class="header-section-number">5.1.1</span> Automatic code formatting</h4>
<p>Let’s try the <strong>Air</strong> extension: find it in the Extensions marketplace and install it. Then, type some deliberately messy code into <code>explore.R</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">x<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,   <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb16-2">y   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span></code></pre></div></div>
<p>Open the Command Palette and run <em>Format Document</em>.</p>
<p>Let’s try it with one other example — add this to <code>plots.R</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(flights, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> arr_delay)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey20"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">expand =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expansion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">comma_format</span>())</span></code></pre></div></div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Want to always format code when you save a file?
</div>
</div>
<div class="callout-body-container callout-body">
<p>Look for <code>editor.formatOnSave</code> further down on this page.</p>
</div>
</div>
</section>
</section>
<section id="settings-the-ui-and-the-json-file" class="level3" data-number="5.2">
<h3 data-number="5.2" class="anchored" data-anchor-id="settings-the-ui-and-the-json-file"><span class="header-section-number">5.2</span> Settings: the UI and the JSON file</h3>
<p>The main way to change Positron’s settings is the <strong>Settings UI</strong>: open the Command Palette and run <em>Preferences: Open Settings (UI)</em>. We’ll explore some Editor-related settings next.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-11-contents" aria-controls="callout-11" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span><code>settings.json</code> and user vs.&nbsp;workspace settings <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-11" class="callout-11-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Under the hood, every setting lives in a <code>settings.json</code> file, which you can edit directly via <em>Preferences: Open User Settings (JSON)</em>.</p>
<p>This can be useful because, for example, it lets you paste in a whole block of settings someone shared with you, and makes it easy to copy your entire configuration to a new computer. Occasionally, it’s also the only way to reach a setting that isn’t yet exposed in the UI.</p>
<hr>
<p>Settings come in two levels:</p>
<ul>
<li><strong>User</strong> settings apply everywhere, across every folder you open — these are your personal preferences, like a theme or keybindings.</li>
<li><strong>Workspace</strong> settings apply only to the current folder, and get saved in a <code>.vscode/settings.json</code> file inside it.</li>
</ul>
<p>Workspace settings override user settings when the two conflict. Unless you have a specific reason to limit a setting to one project, change <strong>user</strong> settings by default, so your preferences carry over to every new folder you open.</p>
</div>
</div>
</div>
</section>
<section id="editor-features-and-settings" class="level3" data-number="5.3">
<h3 data-number="5.3" class="anchored" data-anchor-id="editor-features-and-settings"><span class="header-section-number">5.3</span> Editor features and settings</h3>
<section id="side-by-side-editors" class="level4" data-number="5.3.1">
<h4 data-number="5.3.1" class="anchored" data-anchor-id="side-by-side-editors"><span class="header-section-number">5.3.1</span> Side-by-side editors</h4>
<p>Drag a tab to the left or right edge of the Editor area (or use <em>View: Split Editor</em> from the Command Palette) to view two files at once, side by side. This is useful for comparing two scripts, or keeping a script and a data file’s Data Explorer view open together.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/side-by-side-editor.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:95.0%" alt="Screenshot of the Positron IDE, showing two editor tabs side by side"></p>
</figure>
</div>
</section>
<section id="preview-mode" class="level4" data-number="5.3.2">
<h4 data-number="5.3.2" class="anchored" data-anchor-id="preview-mode"><span class="header-section-number">5.3.2</span> Preview mode</h4>
<p>By default, clicking a file opens it in <strong>preview mode</strong>: the tab title shows in italics, and opening a <em>different</em> file this way reuses that same tab instead of opening a new one. This can be handy to prevent your tab bar from filling up with files you only glance at, but takes some getting used to.</p>
<p>Once you start editing a file, the tab becomes permanent and is no longer in preview mode. But sometimes you want to keep a file open even if you haven’t edited it yet. You can do this in two ways:</p>
<ul>
<li><strong>Double-click</strong> the file to open it in a permanent tab to begin with, or</li>
<li>For a file in preview mode, with your cursor in the file, use the <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>K</kbd> <kbd>P</kbd> shortcut to make it permanent.</li>
</ul>
<p>If you really don’t like preview mode, you can turn it off entirely with the setting <code>workbench.editor.enablePreview: false</code>.</p>
</section>
<section id="a-few-more-settings-and-features" class="level4" data-number="5.3.3">
<h4 data-number="5.3.3" class="anchored" data-anchor-id="a-few-more-settings-and-features"><span class="header-section-number">5.3.3</span> A few more settings and features</h4>
<ul>
<li><p><code>files.autoSave</code> — saves files automatically; some people love this, others hate it, try it and see.</p></li>
<li><p><code>editor.rulers: [80]</code> — draws a thin vertical guide line at column 80, helping you keep lines from getting too long (a common style convention in R). Off by default; add more numbers to the list for multiple guides.</p></li>
<li><p><code>editor.formatOnSave</code> — automatically formats code when you save a file, if you have a formatter installed (we do — we installed Air above).</p></li>
</ul>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Watch out: copying with nothing selected
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you press <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>C</kbd> with no text selected, Positron (like VS Code) copies the <strong>entire current line</strong> instead of doing nothing. This is quite handy but can also catch you off guard. <strong>Try it</strong>: click somewhere in a line of your script <em>without selecting anything</em>, press <kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>C</kbd>, and then paste on an empty line below.</p>
<p>If you don’t want this behavior, you can turn it off with:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb18-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"editor.emptySelectionClipboard"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">false</span></span></code></pre></div></div>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Want more space for your editor and panes?
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you want to maximize the horizontal space for your editor and panes, you can move the Activity Bar from the left side to the top of the window, next to the menu bar, with the setting <code>workbench.activityBar.location: "top"</code>. Try it!</p>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-14-contents" aria-controls="callout-14" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Multiple cursors <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-14" class="callout-14-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Multiple cursors work much as in RStudio (<kbd>Alt</kbd>+click, <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+arrow keys), but Positron adds a handy way to build a multi-cursor selection from <em>matching text</em>:</p>
<ul>
<li><kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>D</kbd> adds the <strong>next occurrence</strong> of whatever you have selected — press it repeatedly to build up the selection one match at a time.</li>
<li><kbd>Cmd</kbd>/<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>L</kbd> selects <strong>all occurrences</strong> in the file at once.</li>
<li><kbd>Esc</kbd> returns you to a single cursor.</li>
</ul>
<p>This is a quick way to rename a variable or column throughout a script, without a find-and-replace.</p>
</div>
</div>
</div>
</section>
</section>
<section id="keyboard-shortcuts-keybindings" class="level3" data-number="5.4">
<h3 data-number="5.4" class="anchored" data-anchor-id="keyboard-shortcuts-keybindings"><span class="header-section-number">5.4</span> Keyboard shortcuts (keybindings)</h3>
<p>Open the Command Palette and run <em>Preferences: Open Keyboard Shortcuts</em> to see every action (command) you can perform in Positron, and their current shortcut (if it has one) in a searchable list — type a command name or describe the action you want (e.g.&nbsp;<code>run line</code>) to find it.</p>
<p>You can click any row to record a new shortcut, and right-click to reset it back to default.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-15-contents" aria-controls="callout-15" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Get your RStudio keybindings back <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-15" class="callout-15-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>Many RStudio shortcuts already work in Positron, but others don’t, because Positron inherits VS Code’s shortcuts, which sometimes conflict. But if you’re really used to the RStudio keybindings, and want to keep using those, you can essentially restore them.</p>
<p>Open the Command Palette and run <em>Preferences: Open Settings (UI)</em>, then search for <code>rstudioKeybindings</code> and check the box:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode json code-with-copy"><code class="sourceCode json"><span id="cb19-1"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"workbench.keybindings.rstudioKeybindings"</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">:</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">true</span></span></code></pre></div></div>
<p><strong>Restart Positron</strong> for it to take effect. This turns on a large set of RStudio shortcuts, including:</p>
<table class="table-striped caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 80%">
</colgroup>
<thead>
<tr class="header">
<th>Shortcut</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><kbd>Ctrl</kbd>+<kbd>1</kbd></td>
<td>Focus the Source/Editor pane</td>
</tr>
<tr class="even">
<td><kbd>Ctrl</kbd>+<kbd>2</kbd></td>
<td>Focus the Console</td>
</tr>
<tr class="odd">
<td><kbd>F2</kbd></td>
<td>Go to definition</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-customize-positron" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-customize-positron"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Customize Positron</h4>
<p>Spend a few minutes setting Positron up the way you like it. For example:</p>
<ol type="1">
<li><p>Turn on <strong>RStudio keybindings</strong>.</p></li>
<li><p>Change some <strong>settings</strong> from the list above or find your own by browsing the Settings UI.</p></li>
<li><p>Install an <strong>extension</strong> that looks useful to you.</p></li>
</ol>
</section>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-open-one-of-your-own-projects" class="level4 unnumbered exercise">
<h4 class="unnumbered anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-open-one-of-your-own-projects"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Bonus exercise: Open one of your own projects</h4>
<ol type="1">
<li><p>Use <code>File</code> &gt; <code>Open Folder...</code> to open a folder that corresponds to one of your own projects with R scripts and data files.</p></li>
<li><p>Open a script you know well, and run the first few lines of it.</p></li>
<li><p>Open one of your own data objects in the <strong>Data Explorer</strong>.</p></li>
</ol>
<p>Does anything stand out?</p>
</section>
<hr style="height:1pt; visibility:hidden;">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Further resources
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li>The <a href="https://positron.posit.co/" target="_blank">Positron documentation <i class="fa-solid fa-external-link" aria-label="external-link"></i></a></li>
<li>The <a href="https://positron.posit.co/migrate-rstudio.html" target="_blank">migration guide for RStudio users <i class="fa-solid fa-external-link" aria-label="external-link"></i></a></li>
<li>An walkthrough within VS Code: open the Command Palette, run <em>Welcome: Open Walkthrough…</em>, and pick one, such as “Migrating from RStudio to Positron”.</li>
<li><a href="https://www.youtube.com/watch?v=7Tog8OkrIWI" target="_blank">Video: Tips &amp; tricks from the maintainers of Positron <i class="fa-solid fa-external-link" aria-label="external-link"></i></a></li>
<li><a href="https://www.youtube.com/watch?v=QIYyeuZ_ISY" target="_blank">Video: Exploring Positron settings <i class="fa-solid fa-external-link" aria-label="external-link"></i></a></li>
<li>The <a href="https://code.visualstudio.com/docs" target="_blank">VS Code documentation <i class="fa-solid fa-external-link" aria-label="external-link"></i></a> — note that most VS Code documentation applies, since Positron is built on it.</li>
</ul>
</div>
</div>


</section>
</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p> Or if that’s somehow not showing, by using the menu <code>File</code> &gt; <code>Open Folder...</code>.↩︎</p></li>
<li id="fn2"><p>For example, a table copied from a webpage directly into R, Python, Julia, SQL, or Markdown code.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>positron</category>
  <guid>https://osu-codeclub.github.io/posts/S12E01_positron_01/</guid>
  <pubDate>Thu, 27 Aug 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S12E01_positron_01/img/user-interface-for-rstudio-migration.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Building Your First R Package: Day 3</title>
  <dc:creator>Neel Agarwal</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E11_packages_03/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction-from-local-to-global" class="level2" data-number="0.1">
<h2 data-number="0.1" class="anchored" data-anchor-id="introduction-from-local-to-global"><span class="header-section-number">0.1</span> Introduction: From Local to Global</h2>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E11_packages_03/img/package-box.png" class="img-fluid figure-img" style="width:100.0%" alt="Illustration of an open cardboard box with the R logo on it, with plots and tables flying out of it."></p>
<figcaption>Packaging your code saves future you a lot of time.</figcaption>
</figure>
</div>
</div>
</div>
<p>Welcome to Day 3! Over the last two days, we transitioned from script-writers to package-developers. We built the <code>vizwizard</code> package, packed it with custom <code>ggplot2</code> and <code>gt</code> wrappers, and learned how to bundle our own custom datasets.</p>
<p>To reiterate, effective package design can be summarized in the below diagram/flowchart. Whether you’re leading a classroom instruction and want to create a package for students to share datasets, functions, etc. or spreading your love for medical analysis and alleviating headaches for researchers/physicians, the below chart is helpful.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E11_packages_03/img/rflow.png" class="img-fluid figure-img" style="width:100.0%" alt="Illustration of an open cardboard box with the R logo on it, with plots and tables flying out of it."></p>
<figcaption>Packaging your code saves future you a lot of time.</figcaption>
</figure>
</div>
</div>
</div>
<p>Today is all about deployment. An R package is incredibly useful for your own reproducibility, but its true power is unlocked when you share it.</p>
<p>Today we will cover:</p>
<ol type="1">
<li><strong>Tying Up Loose Ends:</strong> Making sure our package is completely finished and error-free.</li>
<li><strong>Publishing to GitHub (From Scratch):</strong> The modern, fast standard for sharing packages with colleagues, including how to authenticate your account.</li>
<li><strong>Publishing to CRAN:</strong> Understanding the strict rules and rigorous checks required for the official R repository.</li>
<li><strong>Troubleshooting:</strong> Solving common deployment problems.</li>
</ol>
<p>Let’s load our tools one last time!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(devtools)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required package: usethis</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(usethis)</span></code></pre></div></div>
</div>
</section>
<section id="part-1-tying-up-loose-ends" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Part 1: Tying Up Loose Ends</h1>
<p>Before we can publish anything, we need to ensure our house is in order.</p>
<section id="the-final-check" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="the-final-check"><span class="header-section-number">1.1</span> The Final Check</h2>
<p>On Day 2, we learned about the ultimate package spellcheck: <code>devtools::check()</code>. This function is the gatekeeper for publishing.</p>
<p>Run it now in your console:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check</span>()</span></code></pre></div></div>
</div>
<p>Look at the bottom of your console output. Your goal before proceeding is 0 Errors, 0 Warnings, and 0 Notes!</p>
<p>Errors: Your package is fundamentally broken. Must be fixed.</p>
<p>Warnings: Your package works, but you did something risky. Must be fixed if you want to go to CRAN.</p>
<p>Notes: Minor formatting suggestions. CRAN reviewers might let a Note slide, but it’s best practice to resolve them.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>A common error from previous weeks surrounds dependency errors - when a package is needed but not called/referenced. If this occurs, ensure that there are no function calls in your R scripts and that every script declares 1 function! See the previous trouble shooting tip from other sessions:</p>
<p>Remember, while you can/should put the <code>pubscatter()</code> command in the same file as the <code>function()</code> for evaluation purposes (to test if your code works), remember to delete this line before you build/compile/send your package out for others to use.</p>
</div>
</div>
</section>
</section>
<section id="part-2-publishing-to-github-from-scratch" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Part 2: Publishing to GitHub (From Scratch)</h1>
<p>CRAN is great, but it has extremely strict rules. If you want to share a package with your lab mates immediately, or if you are hosting internal clinical code that shouldn’t be public, GitHub is the way to go.</p>
<section id="step-1-version-control-with-git" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="step-1-version-control-with-git"><span class="header-section-number">2.1</span> Step 1: Version Control with Git</h2>
<p>Your project needs to be a Git repository before it can go to GitHub. The usethis package makes this a one-liner. Run this in your console:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_git</span>()</span></code></pre></div></div>
</div>
<p>RStudio will ask if it is okay to commit your files and restart. Select “Yes” to both.</p>
</section>
<section id="step-2-authentication-the-tricky-part" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="step-2-authentication-the-tricky-part"><span class="header-section-number">2.2</span> Step 2: Authentication (The Tricky Part!)</h2>
<p>To send code from your local computer to GitHub, GitHub needs to know it’s really you. We do this using a Personal Access Token.</p>
<ol type="1">
<li>Create the Token: Run this command. It will open a GitHub webpage in your browser.</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_github_token</span>()</span></code></pre></div></div>
</div>
<ol start="2" type="1">
<li><p>Scroll down on the webpage and click “Generate Token”.</p></li>
<li><p>Copy the token (it looks like a long string of random letters and numbers). Do not lose this!</p></li>
<li><p>Give the Token to R: Go back to RStudio and run:</p></li>
<li><p>Paste your token into the console when prompted and hit Enter.</p></li>
</ol>
</section>
<section id="step-3-push-to-github" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="step-3-push-to-github"><span class="header-section-number">2.3</span> Step 3: Push to GitHub</h2>
<p>Now that RStudio and GitHub are linked, you can push your package to the cloud!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_github</span>()</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>How users install your GitHub package Once your package is on GitHub, you can tell your colleagues to run this code to download and install your tools:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># They only need to run this once</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"remotes"</span>)</span>
<span id="cb8-3">remotes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"your-username/vizwizard"</span>)</span></code></pre></div></div>
</div>
</div>
</div>
</section>
</section>
<section id="part-3-publishing-to-cran" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Part 3: Publishing to CRAN</h1>
<p>The Comprehensive R Archive Network (CRAN) is the official repository for R packages. Publishing here means your package can be installed with a simple <code>install.packages("vizwizard")</code>.</p>
<p>Understanding CRAN Rules and Requirements</p>
<p>CRAN is notoriously strict. They are protecting the entire R ecosystem from malicious code, broken dependencies, and bloated file sizes. Here is what CRAN strictly requires:</p>
<p>Zero Errors or Warnings: Your R CMD check must pass cleanly on Windows, Mac, Linux, and Solaris.</p>
<p>File Size Limits: Your package must generally be under 5MB. If you include massive datasets, you will be rejected.</p>
<p>Impeccable Documentation: Every single exported function must have a complete help file with working examples. If an example throws an error, the package is rejected.</p>
<p>Standard Licenses: You must clearly declare an open-source license (e.g., MIT or GPL-3) in your DESCRIPTION file.</p>
<p>No “Hidden” Changes: Your package cannot modify the user’s global .Rprofile, secretly install other software, or change their working directory without permission.</p>
<section id="step-1-cross-platform-testing" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="step-1-cross-platform-testing"><span class="header-section-number">3.1</span> Step 1: Cross-Platform Testing</h2>
<p>Your computer is only one operating system. R has a built-in “inspector” that checks your package, but CRAN requires your package to work on all operating systems. devtools provides ways to send your package to external servers to be tested.</p>
<p>Run these and wait for the email results:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test on a CRAN-like Windows server</span></span>
<span id="cb9-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_win_devel</span>()</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test on a CRAN-like Mac server</span></span>
<span id="cb9-5">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check_mac_release</span>()</span></code></pre></div></div>
</div>
</section>
<section id="step-2-the-final-submission" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="step-2-the-final-submission"><span class="header-section-number">3.2</span> Step 2: The Final Submission</h2>
<p>If your local <code>devtools::check()</code> is totally clean, and your cross-platform checks came back green, you are ready to submit!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">release</span>()</span></code></pre></div></div>
</div>
<p>R will ask you a series of questions to double-check that you are absolutely ready. Once you say yes, it bundles your package and emails it to the CRAN team. You will usually hear back from a volunteer reviewer within a few days.</p>
</section>
</section>
<section id="part-4-practice-problems-troubleshooting" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Part 4: Practice Problems &amp; Troubleshooting</h1>
<p>Package development rarely goes perfectly on the first try. Let’s walk through some common problems.</p>
<section id="problem-1-the-description-file-dependency-error" class="level2" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="problem-1-the-description-file-dependency-error"><span class="header-section-number">4.1</span> Problem 1: The DESCRIPTION File Dependency Error</h2>
<p>Scenario: You ran <code>devtools::check()</code> and got the following Error:</p>
<p>Error: namespace ‘ggplot2’ is not available and has been NOTED.</p>
<details>
<summary>
Click for the solution
</summary>
<p>Why it happened: You wrote a function that relies on ggplot2, but you forgot to tell your package that it requires it! R doesn’t know to load it.</p>
<p>The Fix: As we learned on Day 1, never type package names into the DESCRIPTION file manually. Run this in your console to automatically add it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span></code></pre></div></div>
</div>
</details>
<details>
<summary>
Bonus: What if the problem was missing <code>dplyr</code> or <code>gt</code>?
</summary>
The fix is exactly the same! Just replace <code>ggplot2</code> with the missing package name.
</details>
</section>
<section id="problem-2-the-undocumented-argument-warning" class="level2" data-number="4.2">
<h2 data-number="4.2" class="anchored" data-anchor-id="problem-2-the-undocumented-argument-warning"><span class="header-section-number">4.2</span> Problem 2: The “Undocumented Argument” Warning</h2>
<p>Scenario: You ran <code>devtools::check()</code> and received a Warning:</p>
<p>Warning: Undocumented arguments in documentation object ‘pub_scatter’: ‘color_var’</p>
<details>
<summary>
Click for the solution
</summary>
<p>Why it happened: You added a new argument (color_var) to your custom scatterplot function, but you forgot to write a roxygen2 comment for it. CRAN will reject this immediately because the help file doesn’t match the function.</p>
<p>The Fix:</p>
<p>Open your R/plots.R script.</p>
<p>Find the pub_scatter function.</p>
<p>Add the missing tag above the function: #’ <span class="citation" data-cites="param">@param</span> color_var The variable to color points by (unquoted)</p>
<p>Run <code>devtools::document()</code> to update the manual.</p>
<p>Re-run <code>devtools::check()</code>.</p>
</details>
</section>
<section id="problem-3-the-cran-rejection" class="level2" data-number="4.3">
<h2 data-number="4.3" class="anchored" data-anchor-id="problem-3-the-cran-rejection"><span class="header-section-number">4.3</span> Problem 3: The CRAN Rejection</h2>
<p>Scenario: You submitted to CRAN, but two days later you get an email from a reviewer saying:</p>
<p>“Please do not use T and F in your code. Spell out TRUE and FALSE. Please fix and resubmit.”</p>
<details>
<summary>
Click for the solution
</summary>
<p>Why it happened: In R, T and F are technically variables that can be overwritten by the user (e.g., someone could run T &lt;- FALSE in their console and break everything). CRAN prohibits the use of T and F abbreviations in package code.</p>
<p>The Fix:</p>
<p>Use Ctrl+F (or Cmd+F) to search your entire project for instances of T and F.</p>
<p>Change them to TRUE and FALSE.</p>
<p>Open your DESCRIPTION file and bump your version number up (e.g., change 0.1.0 to 0.1.1).</p>
<p>Re-run <code>devtools::check()</code> to ensure everything is perfect.</p>
<p>Submit again using <code>devtools::release()</code>.</p>
</details>
<p>Conclusion: You Did It! Congratulations! Over the last three days, you have conquered one of the steepest learning curves in the R ecosystem. You’ve learned how to harness usethis to generate boilerplate code , use tidy evaluation ({{ }}) for custom functions , bundle data, and ultimately share your work with the world. +2</p>
<p>Whether you keep your packages local, put them on GitHub, or send them to CRAN, you are now an R Package Developer. Happy coding!</p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R packages</category>
  <category>GitHub</category>
  <category>CRAN</category>
  <category>publishing</category>
  <category>open source</category>
  <guid>https://osu-codeclub.github.io/posts/S11E11_packages_03/</guid>
  <pubDate>Mon, 20 Apr 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E11_packages_03/img/package-box.png" medium="image" type="image/png" height="79" width="144"/>
</item>
<item>
  <title>Building Your First R Package: Day 2</title>
  <dc:creator>Neel Agarwal</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E10_packages_02/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction-leveling-up-your-package" class="level2" data-number="0.1">
<h2 data-number="0.1" class="anchored" data-anchor-id="introduction-leveling-up-your-package"><span class="header-section-number">0.1</span> Introduction: Leveling Up Your Package</h2>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E10_packages_02/img/package-box.png" class="img-fluid figure-img" style="width:100.0%" alt="Illustration of an open cardboard box with the R logo on it, with plots and tables flying out of it."></p>
<figcaption>Packaging your code saves future you a lot of time.</figcaption>
</figure>
</div>
</div>
</div>
<p>Welcome to Day 2! In our first session, we learned that copying and pasting the same code across projects is a recipe for headaches. We solved this by wrapping our favorite <code>ggplot2</code> and <code>gt</code> code into reusable functions using the <code>usethis</code> package and the “curly-curly” (<code>{ }</code>) operator.</p>
<p>Today, we are going to make our <code>vizwizard</code> package even more useful for your daily work. We will cover:</p>
<ol type="1">
<li><strong>Package Data:</strong> How to properly store data inside your package so it travels with your functions.</li>
<li><strong>Multi-Layer Plots:</strong> Writing slightly more advanced, multi-layered publication figures.</li>
<li><strong>Documentation:</strong> Best practices for documenting your datasets so future-you knows what they are.</li>
<li><strong>Package Checking:</strong> Running the ultimate “spellcheck” on your package (with special notes for Mac vs.&nbsp;Windows!).</li>
<li><strong>AI Assistance:</strong> A brief guide on using AI tools to assist in your R package development.</li>
</ol>
<p>Let’s start by loading our essential tools.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(devtools)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required package: usethis</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(usethis)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'

The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Mac vs.&nbsp;Windows: The “Under the Hood” Tools Before we go further, package development requires your computer to have certain tools installed to build code. If you haven’t already:</p>
<p>Windows Users: You must download and install Rtools from the CRAN website. Rtools is a set of programs that allow Windows to build R packages.</p>
<p>Mac Users: You need the Xcode Command Line Tools. You can install this by opening your Mac’s Terminal app and typing <code>xcode-select --install</code> and hitting Enter.</p>
</div>
</div>
</section>
<section id="including-data-in-your-package" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Including Data in Your Package</h1>
<section id="the-problem-where-does-the-data-go" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="the-problem-where-does-the-data-go"><span class="header-section-number">1.1</span> The Problem: Where does the data go?</h2>
<p>When you write a normal R script, you usually load data from your computer.</p>
<p>Windows users might use a path like <code>read_csv("C:/Users/Name/Documents/my_data.csv")</code>.</p>
<p>Mac users might use a path like <code>read_csv("~/Documents/my_data.csv")</code>.</p>
<p>If you share your package with a colleague on a different operating system, those hard-coded paths will instantly break!</p>
</section>
<section id="the-solution-r-packages-have-a-special-built-in-way-to-store-data-so-that-it-is-always-available-whenever-you-type-libraryvizwizard-regardless-of-whether-the-user-is-on-a-mac-or-a-pc." class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="the-solution-r-packages-have-a-special-built-in-way-to-store-data-so-that-it-is-always-available-whenever-you-type-libraryvizwizard-regardless-of-whether-the-user-is-on-a-mac-or-a-pc."><span class="header-section-number">1.2</span> The Solution: R packages have a special built-in way to store data so that it is always available whenever you type library(vizwizard), regardless of whether the user is on a Mac or a PC.</h2>
</section>
</section>
<section id="tutorial-step-by-step-package-data" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Tutorial: Step-by-Step Package Data</h1>
<p>Let’s say your lab has a standardized “Demographics Table” that you always use as a reference, and you want it baked right into your package.</p>
<section id="step-1-set-up-the-raw-data-folder" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="step-1-set-up-the-raw-data-folder"><span class="header-section-number">2.1</span> Step 1: Set up the raw data folder</h2>
<p>Run this command in your console:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_data_raw</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lab_demographics"</span>)</span></code></pre></div></div>
</div>
<p>What this does: It creates a new folder called data-raw/ and opens a script inside it. This script is basically your “scratchpad.” It’s where you put the code that reads your messy CSVs and cleans them up.</p>
</section>
<section id="step-2-write-your-cleaning-code-and-save-the-data" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="step-2-write-your-cleaning-code-and-save-the-data"><span class="header-section-number">2.2</span> Step 2: Write your cleaning code and save the data</h2>
<p>Inside that new data-raw/lab_demographics.R script, you might write something like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1. Pretend we are loading and cleaning a dataset here</span></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (For this example, we will just create a small dataframe from scratch)</span></span>
<span id="cb9-3">lab_demographics <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb9-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Control"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treatment A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Treatment B"</span>),</span>
<span id="cb9-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">target_n =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>),</span>
<span id="cb9-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">active =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb9-7">)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2. THE MAGIC STEP: Save it to the package!</span></span>
<span id="cb10-2">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_data</span>(lab_demographics, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overwrite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
<p>What this does: <code>use_data()</code> takes your cleaned dataframe and secretly compresses it into a special .rda (R Data) file inside a hidden data/ folder.</p>
<p>Now, if you install your package and restart R, you can just type lab_demographics into the console and the data will appear, as if by magic!</p>
<p>Practice: Exporting your own simple dataset</p>
<p>Your Turn: Create a very simple dataset called my_favorite_colors that contains a dataframe of two columns: color_name and hex_code. Save it to your package using <code>usethis::use_data()</code>.</p>
<details>
<summary>
Need a hint?
</summary>
You don’t need to read a CSV for this! Just use <code>data.frame()</code> or <code>tibble()</code> to type out two or three colors manually, assign it to the variable my_favorite_colors, and pass that variable to <code>use_data()</code>.
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the dataset directly in your workspace</span></span>
<span id="cb11-2">my_favorite_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb11-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Navy"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Forest Green"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Crimson"</span>),</span>
<span id="cb11-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hex_code =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000080"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#228B22"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#DC143C"</span>)</span>
<span id="cb11-5">)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save it so it lives inside your package forever!</span></span>
<span id="cb12-2">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_data</span>(my_favorite_colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overwrite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
</div>
</details>
</section>
</section>
<section id="multi-layer-plotting-functions" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Multi-Layer Plotting Functions</h1>
<section id="tutorial-density-plots-with-means" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="tutorial-density-plots-with-means"><span class="header-section-number">3.1</span> Tutorial: Density Plots with Means</h2>
<p>On Day 1, we learned that when we pass unquoted column names (like body_mass_g) into our custom functions, we have to wrap them in “curly-curly” braces ({{ }}).</p>
<p>Let’s use that rule to build a function that adds multiple layers to a plot. We are going to make a <code>pub_density()</code> function. It will:</p>
<p>Draw a density plot (a smoothed-out histogram).</p>
<p>Automatically calculate the overall average (mean) of the data.</p>
<p>Draw a dashed vertical line right at that average.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Publication-Ready Density Plot with a Mean Line</span></span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param numeric_var The numeric variable to plot (unquoted)</span></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A ggplot object</span></span>
<span id="cb13-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb13-7">pub_density <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, numeric_var) {</span>
<span id="cb13-8">  </span>
<span id="cb13-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Calculate the mean of the variable the user provides</span></span>
<span id="cb13-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We use drop_na() so the mean calculation doesn't fail if there are blanks!</span></span>
<span id="cb13-11">  mean_val <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>({{ numeric_var }}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>({{ numeric_var }})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb13-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(avg) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 'pull' extracts just the number out of the dataframe</span></span>
<span id="cb13-15">  </span>
<span id="cb13-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Build the plot</span></span>
<span id="cb13-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> {{ numeric_var }})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Layer 1: The density curve</span></span>
<span id="cb13-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-20">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Layer 2: The vertical line for the mean</span></span>
<span id="cb13-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> mean_val, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-22">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Layer 3: Clean up the theme</span></span>
<span id="cb13-23">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The red dashed line represents the mean:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(mean_val, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb13-25">}</span>
<span id="cb13-26"></span>
<span id="cb13-27"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test it out on the penguins dataset!</span></span>
<span id="cb13-28"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pub_density</span>(penguins, body_mass_g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Distribution of Penguin Body Mass"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body Mass (g)"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_density()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E10_packages_02/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Practice: The Simple Violin Plot</p>
<p>Your Turn: Write a function called pub_violin. It should take a dataframe, a categorical x_var, and a numeric y_var. It should output a <code>geom_violin()</code> plot. Make sure the violins are filled with a color based on the x_var.</p>
<details>
<summary>
Need a hint?
</summary>
Start your function exactly like we did on Day 1. Remember to put {{ x_var }} and {{ y_var }} inside the aes() function. To get the colors right, you also need to add fill = {{ x_var }} inside <code>aes()</code>.
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Simple Violin Plot</span></span>
<span id="cb16-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param x_var A categorical variable for the x-axis (unquoted)</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param y_var A numeric variable for the y-axis (unquoted)</span></span>
<span id="cb16-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A ggplot object</span></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb16-8">pub_violin <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, x_var, y_var) {</span>
<span id="cb16-9">  </span>
<span id="cb16-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> {{ x_var }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> {{ y_var }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> {{ x_var }})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Trim = FALSE makes the violin tails look a bit more natural</span></span>
<span id="cb16-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">trim =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb16-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hide the legend since the x-axis already tells us the groups!</span></span>
<span id="cb16-16">    )</span>
<span id="cb16-17">}</span>
<span id="cb16-18"></span>
<span id="cb16-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test it out!</span></span>
<span id="cb16-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pub_violin</span>(penguins, species, flipper_length_mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Flipper Lengths by Species"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_ydensity()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E10_packages_02/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
</section>
<section id="documenting-datasets" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Documenting Datasets</h1>
<p>We know that running <code>devtools::document()</code> turns our #’ comments into help files for our functions.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Shortcut Alert! Instead of typing <code>devtools::document()</code> into the console every time, use your keyboard:</p>
<p>Mac Users: Press Cmd + Shift + D</p>
<p>Windows Users: Press Ctrl + Shift + D</p>
</div>
</div>
<p>But how do you document a dataset?</p>
<p>Datasets don’t have code underneath them to attach comments to. Instead, we create a script just for documentation.</p>
</section>
<section id="tutorial-documenting-lab_demographics" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Tutorial: Documenting lab_demographics</h1>
<p>Create a standard R script and save it as R/data.R. Inside, write out the roxygen2 comments, and then just put the name of the dataset in quotes at the very bottom.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Inside R/data.R</span></span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Laboratory Demographics Reference</span></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' A small dataset containing the target enrollment numbers for our</span></span>
<span id="cb18-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' three main study groups.</span></span>
<span id="cb18-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb18-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @format A data frame with 3 rows and 3 columns:</span></span>
<span id="cb18-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' \describe{</span></span>
<span id="cb18-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   \item{group}{The name of the study group}</span></span>
<span id="cb18-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   \item{target_n}{The number of participants we want to enroll}</span></span>
<span id="cb18-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'   \item{active}{TRUE if the group is currently enrolling, FALSE otherwise}</span></span>
<span id="cb18-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' }</span></span>
<span id="cb18-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lab_demographics"</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "lab_demographics"</code></pre>
</div>
</div>
<p>Now, when you document your package, you can type ?lab_demographics in your console, and a beautiful help page will pop up explaining exactly what every column means!</p>
</section>
<section id="the-ultimate-package-spellcheck" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> The Ultimate Package Spellcheck</h1>
<p>Whether you want to share your package with the world on CRAN (The Comprehensive R Archive Network), put it on GitHub, or just keep it for yourself, you want to make sure your package isn’t fundamentally broken.</p>
<p>R has a built-in “inspector” that checks your package for missing commas, undocumented arguments, and messy code.</p>
<p>Run this in your console:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">check</span>()</span>
<span id="cb20-2"></span>
<span id="cb20-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keyboard Shortcut: </span></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Mac: Cmd + Shift + E</span></span>
<span id="cb20-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Windows: Ctrl + Shift + E</span></span></code></pre></div></div>
</div>
<p>When you run this, your console will output a lot of text. It is building your package from scratch and testing it. Note: If this step fails immediately with an error about missing compilers, refer back to the warning at the top of this page about Rtools (Windows) and Xcode (Mac).</p>
<p>At the very end, it will give you a score:</p>
<p>Errors: Your package is broken and will not install. You must fix these.</p>
<p>Warnings: Your package works, but you did something risky (like forgetting to add ggplot2 to your DESCRIPTION file dependencies).</p>
<p>Notes: Minor formatting suggestions.</p>
<p>Goal: Try to get 0 Errors, 0 Warnings, and 0 Notes!</p>
</section>
<section id="ai-assisted-coding-for-casual-coders" class="level1" data-number="7">
<h1 data-number="7"><span class="header-section-number">7</span> AI-Assisted Coding for Casual Coders</h1>
<p>Writing out documentation and boilerplate code can feel tedious. Generative AI (like Gemini, ChatGPT, or Claude) is a fantastic tool for casual coders to speed up this process, if you know how to talk to it.</p>
<section id="tips-for-getting-good-r-package-code-from-ai" class="level2" data-number="7.1">
<h2 data-number="7.1" class="anchored" data-anchor-id="tips-for-getting-good-r-package-code-from-ai"><span class="header-section-number">7.1</span> Tips for getting good R Package code from AI:</h2>
<p>Be specific about the Tidyverse: R has changed a lot in 10 years. If you ask an AI for a plot, it might give you ancient Base R code.</p>
<section id="good-prompt-write-an-r-function-using-ggplot2-and-dplyr" class="level3" data-number="7.1.1">
<h3 data-number="7.1.1" class="anchored" data-anchor-id="good-prompt-write-an-r-function-using-ggplot2-and-dplyr"><span class="header-section-number">7.1.1</span> Good Prompt: “Write an R function using ggplot2 and dplyr…”</h3>
<p>Tell it you are making a package: AI needs context.</p>
</section>
<section id="good-prompt-i-am-writing-an-r-package.-generate-the-roxygen2-documentation-header-for-this-function" class="level3" data-number="7.1.2">
<h3 data-number="7.1.2" class="anchored" data-anchor-id="good-prompt-i-am-writing-an-r-package.-generate-the-roxygen2-documentation-header-for-this-function"><span class="header-section-number">7.1.2</span> Good Prompt: “I am writing an R package. Generate the roxygen2 documentation header for this function…”</h3>
<p>Remind it about Tidy Evaluation: AIs often forget the {{ }} syntax. Remind them!</p>
</section>
<section id="good-prompt-write-a-function-that-takes-a-dataframe-and-an-unquoted-column-name.-you-must-use-the-curly-curly-operator-for-the-column-name." class="level3" data-number="7.1.3">
<h3 data-number="7.1.3" class="anchored" data-anchor-id="good-prompt-write-a-function-that-takes-a-dataframe-and-an-unquoted-column-name.-you-must-use-the-curly-curly-operator-for-the-column-name."><span class="header-section-number">7.1.3</span> Good Prompt: “Write a function that takes a dataframe and an unquoted column name. You MUST use the curly-curly {{ }} operator for the column name.”</h3>
</section>
</section>
</section>
<section id="practice-prompt-engineering" class="level1" data-number="8">
<h1 data-number="8"><span class="header-section-number">8</span> Practice: Prompt Engineering</h1>
<p>Imagine you have a messy script that creates a scatterplot, and you want AI to turn it into a neat package function for you. Which of these prompts will give you a better result?</p>
<p>Prompt A: “Make a scatterplot function for my data.”</p>
<p>Prompt B: “I am building an R package. Please write a function using ggplot2 that takes a dataframe, an unquoted x variable, and an unquoted y variable to make a scatterplot. Use the {{ }} operator for the variables, and include a roxygen2 header with <span class="citation" data-cites="param">@param</span> and <span class="citation" data-cites="export">@export</span> tags.”</p>
<details>
<summary>
Click for the answer
</summary>
Prompt B is significantly better! Prompt A will likely give you a function that works in a standard script but fails completely when put inside a package because it will lack the correct parameter definitions, {{ }} formatting, and <span class="citation" data-cites="export">@export</span> documentation.
</details>
<p>You’ve made it through Day 2! By now, you know how to bundle custom datasets into your package, write multi-layered plotting wrappers, and use tools like <code>devtools::check()</code> and AI to make your life easier.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R packages</category>
  <category>data</category>
  <category>advanced ggplot2</category>
  <category>CRAN</category>
  <category>AI</category>
  <guid>https://osu-codeclub.github.io/posts/S11E10_packages_02/</guid>
  <pubDate>Mon, 13 Apr 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E10_packages_02/img/package-box.png" medium="image" type="image/png" height="79" width="144"/>
</item>
<item>
  <title>Building Your First R Package: Day 1</title>
  <dc:creator>Neel Agarwal</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E09_packages_01/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction-why-build-an-r-package" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction-why-build-an-r-package"><span class="header-section-number">1</span> Introduction: Why build an R Package?</h2>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E09_packages_01/img/package-box.png" class="img-fluid figure-img" style="width:100.0%" alt="Illustration of an open cardboard box with the R logo on it, with plots and tables flying out of it."></p>
<figcaption>Packaging your code saves future you a lot of time.</figcaption>
</figure>
</div>
</div>
</div>
<p>When you first learn R, you write scripts to get a specific job done. As you progress, you might find yourself copying and pasting the same 30 lines of ggplot2 code across different projects just to make sure your axis labels match your lab’s publication standards. Or, you might have a specific way you always format your summary statistic tables.</p>
<p>Copying and pasting is generally not a good idea. It is error-prone, inefficient, and creates a nightmare if you decide to change a single color in your lab’s theme—you’d have to update it in dozens of scripts!</p>
<p>The solution is writing an R package. A common misconception is that R packages are only for sharing groundbreaking new statistical methods on CRAN. In reality, the most useful packages are often internal or personal packages. By bundling your custom functions, themes, and templates into a package, you ensure:</p>
<!-- added bullets -->
<ul>
<li>Reproducibility: Your code behaves identically across all your projects.</li>
<li>Efficiency: You load your tools with a single library(myLabTools) command.</li>
<li>Collaboration: You can easily share your exact workflow with colleagues.</li>
</ul>
<!-- added another header -->
<section id="an-r-package-example" class="level3" data-number="1.1">
<h3 data-number="1.1" class="anchored" data-anchor-id="an-r-package-example"><span class="header-section-number">1.1</span> An R package example</h3>
<p>An example of an R package I, Neel, made was in order to solve an issue that recurs in the field of health sciences often - what is the best (standardized) way to code for various clinical indices? For instance, everyone might code a fibrosis index differently in a programming language due to logical nuances or due to complex logic, omit it entirely due to technological complexity. To solve this issue, I spent around 1.5 years coding every single clinical formula into R as a simple programming workflow to ensure this issue wouldn’t be faced!</p>
<p>This is a helpful resource whenever you want to see documentation for packages online, and you can find many more on rdocumentation.org! By lesson 3, you will have all the tools to have your own package listed on rdocumentation.org, and published to the entire R community to download and use for their own purposes!</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E09_packages_01/img/cliot1.png" class="img-fluid figure-img" style="width:100.0%" alt="Illustration of an open cardboard box with the R logo on it, with plots and tables flying out of it."></p>
<figcaption>Packaging your code saves future you a lot of time.</figcaption>
</figure>
</div>
</div>
</div>
<!-- added some markdown formatting and links -->
<p>Today, we are going to focus on:</p>
<ul>
<li><p>Setting up an R package skeleton using the <a href="https://usethis.r-lib.org/"><code>usethis</code></a> package.</p></li>
<li><p>Understanding how to pass unquoted variable names into functions (Tidy Evaluation).</p></li>
<li><p>Writing a function for a publication-ready <code>ggplot2</code> histogram.</p></li>
<li><p>Writing a function for a professional summary table using <code>gt</code>.</p></li>
<li><p>Let’s start by loading the tools we will use to build and test our new package.</p></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(devtools) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Core package development tools</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required package: usethis</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(usethis)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Automates the tedious parts of package setup</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For ggplot2 and dplyr</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gt)       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For generating professional tables</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For testing our functions with real data</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'

The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
</div>
<!-- added headers -->
<!-- added to this section some links and more markdown formatting to call out specifically packages names -->
</section>
</section>
<section id="creating-your-package-skeleton" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="creating-your-package-skeleton"><span class="header-section-number">2</span> Creating Your Package Skeleton</h2>
<section id="the-basics-of-usethis" class="level3" data-number="2.1">
<h3 data-number="2.1" class="anchored" data-anchor-id="the-basics-of-usethis"><span class="header-section-number">2.1</span> The Basics of <code>usethis</code></h3>
<p>Historically, setting up an R package meant manually creating specific folders (R/, man/), writing a strictly formatted DESCRIPTION file, and managing a NAMESPACE. It was tedious and frustrating.</p>
<p>The <code>usethis</code> package automates all of this boilerplate work. It sets up the exact directory structure R expects.</p>
<!-- converted your Important Note to a callout-tip -->
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Run this in your console!
</div>
</div>
<div class="callout-body-container callout-body">
<p>You should run this command directly in your console, pointing to a new directory outside of your current project workspace. Do not nest an R package inside an existing R Project.</p>
</div>
</div>
<p>To create a new package, you simply use the <a href="https://usethis.r-lib.org/reference/create_package.html"><code>create_package()</code></a> function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a new package called "vizwizard" on your desktop</span></span>
<span id="cb8-2">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"~/Desktop/vizwizard"</span>)</span></code></pre></div></div>
</div>
<p>Running this will open a brand new RStudio session. Take a look at the Files pane—you’ll see a DESCRIPTION file (which holds metadata about your package) and an R/ folder.</p>
<p>The R/ folder is where all your custom functions will live. We never create R scripts manually in a package. Instead, we use use_r() to generate them properly.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a script to hold our plotting functions</span></span>
<span id="cb9-2">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_r</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plots"</span>)</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a script to hold our table functions</span></span>
<span id="cb9-5">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_r</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tables"</span>)</span></code></pre></div></div>
</div>
<!-- added a header -->
</section>
<section id="writing-a-plotting-function" class="level3" data-number="2.2">
<h3 data-number="2.2" class="anchored" data-anchor-id="writing-a-plotting-function"><span class="header-section-number">2.2</span> Writing a Plotting Function</h3>
<p>The Problem: Functions and ggplot2</p>
<p>Let’s say you want to write a function that takes a dataset and plots a histogram of a specific variable. A naive approach might look like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># THIS WILL NOT WORK!</span></span>
<span id="cb10-2">bad_histogram <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, my_variable) {</span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> my_variable)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>()</span>
<span id="cb10-5">}</span></code></pre></div></div>
</div>
<!-- added code in backticks -->
<p>If you try to run <code>bad_histogram(penguins, flipper_length_mm)</code>, R will throw an error saying object ‘flipper_length_mm’ not found.</p>
<p>Why? Because R is looking for a standalone object named flipper_length_mm in your global environment, rather than looking inside the penguins dataset. Standard ggplot2 functions know how to look inside the dataframe, but our custom function doesn’t automatically pass that behavior along.</p>
<!-- added a header -->
</section>
<section id="the-solution-tidy-evaluation-and" class="level3" data-number="2.3">
<h3 data-number="2.3" class="anchored" data-anchor-id="the-solution-tidy-evaluation-and"><span class="header-section-number">2.3</span> The Solution: Tidy Evaluation and {{ }}</h3>
<p>To solve this, we use something called Tidy Evaluation, specifically the “curly-curly” operator: {{ }}.</p>
<p>Wrapping our variable arguments in {{ }} tells R: “Don’t evaluate this right now. Wait until you are inside the ggplot2 or dplyr function, and then evaluate it in the context of the provided dataframe.”</p>
<p>Let’s write a proper function inside our new R/plots.R file. We will create a standardized, publication-ready histogram.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Publication-Ready Histogram</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb11-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param var A numeric variable to plot (unquoted)</span></span>
<span id="cb11-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param title A string for the plot title</span></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A ggplot object</span></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb11-8">pub_histogram <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Histogram"</span>) {</span>
<span id="cb11-9">  </span>
<span id="cb11-10">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Notice the curly-curly around 'var'</span></span>
<span id="cb11-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> {{ var }})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2c3e50"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bins =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb11-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">hjust =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>),</span>
<span id="cb11-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb11-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>)</span>
<span id="cb11-18">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> title, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Count"</span>)</span>
<span id="cb11-20">}</span></code></pre></div></div>
</div>
<p>Let’s test it out using our penguins dataset to see if our custom styling is applied automatically!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test our new function</span></span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pub_histogram</span>(penguins, flipper_length_mm, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Flipper Lengths"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E09_packages_01/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<!-- added a header and some backticks around package/file names-->
</section>
<section id="writing-a-professional-table-function" class="level3" data-number="2.4">
<h3 data-number="2.4" class="anchored" data-anchor-id="writing-a-professional-table-function"><span class="header-section-number">2.4</span> Writing a Professional Table Function</h3>
<p>The same {{ }} rules apply when writing wrappers for <code>dplyr</code> pipelines and <code>gt</code> tables.</p>
<p>Let’s write a function for <code>R/tables.R</code>. We want to take any dataset, group it by a categorical variable, summarize a numeric variable (getting the Mean, SD, and Count), and output a clean, styled HTML table using the gt package.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Summary Statistics Table</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param group_var A categorical variable to group by (unquoted)</span></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param sum_var A numeric variable to summarize (unquoted)</span></span>
<span id="cb14-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A gt table object</span></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb14-8">pub_summary_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, group_var, sum_var) {</span>
<span id="cb14-9">  </span>
<span id="cb14-10">  data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove NAs specifically for the columns we care about</span></span>
<span id="cb14-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>({{ group_var }}, {{ sum_var }}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>({{ group_var }}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb14-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Mean =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>({{ sum_var }}),</span>
<span id="cb14-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>({{ sum_var }}),</span>
<span id="cb14-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb14-18">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pass the summarized data into gt()</span></span>
<span id="cb14-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gt</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tab_header</span>(</span>
<span id="cb14-22">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Summary Statistics**"</span>)</span>
<span id="cb14-23">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt_number</span>(</span>
<span id="cb14-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(Mean, SD),</span>
<span id="cb14-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decimals =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb14-27">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb14-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply a built-in clean theme</span></span>
<span id="cb14-29">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">opt_stylize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">style =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb14-30">}</span></code></pre></div></div>
</div>
<p>Let’s test our table function to quickly summarize penguin body mass by species!</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pub_summary_table</span>(penguins, species, body_mass_g)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="kaxqrteeac" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#kaxqrteeac table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#kaxqrteeac thead, #kaxqrteeac tbody, #kaxqrteeac tfoot, #kaxqrteeac tr, #kaxqrteeac td, #kaxqrteeac th {
  border-style: none;
}

#kaxqrteeac p {
  margin: 0;
  padding: 0;
}

#kaxqrteeac .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #004D80;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #004D80;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#kaxqrteeac .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#kaxqrteeac .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#kaxqrteeac .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#kaxqrteeac .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kaxqrteeac .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
}

#kaxqrteeac .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #0076BA;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kaxqrteeac .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#kaxqrteeac .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#kaxqrteeac .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#kaxqrteeac .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#kaxqrteeac .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#kaxqrteeac .gt_spanner_row {
  border-bottom-style: hidden;
}

#kaxqrteeac .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #0076BA;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#kaxqrteeac .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #0076BA;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
  vertical-align: middle;
}

#kaxqrteeac .gt_from_md > :first-child {
  margin-top: 0;
}

#kaxqrteeac .gt_from_md > :last-child {
  margin-bottom: 0;
}

#kaxqrteeac .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: none;
  border-top-width: 1px;
  border-top-color: #89D3FE;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #89D3FE;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #89D3FE;
  vertical-align: middle;
  overflow-x: hidden;
}

#kaxqrteeac .gt_stub {
  color: #FFFFFF;
  background-color: #0076BA;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #0076BA;
  padding-left: 5px;
  padding-right: 5px;
}

#kaxqrteeac .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#kaxqrteeac .gt_row_group_first td {
  border-top-width: 2px;
}

#kaxqrteeac .gt_row_group_first th {
  border-top-width: 2px;
}

#kaxqrteeac .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kaxqrteeac .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #0076BA;
}

#kaxqrteeac .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#kaxqrteeac .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
}

#kaxqrteeac .gt_grand_summary_row {
  color: #333333;
  background-color: #89D3FE;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kaxqrteeac .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #0076BA;
}

#kaxqrteeac .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #0076BA;
}

#kaxqrteeac .gt_striped {
  background-color: #F4F4F4;
}

#kaxqrteeac .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #0076BA;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #0076BA;
}

#kaxqrteeac .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kaxqrteeac .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kaxqrteeac .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kaxqrteeac .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kaxqrteeac .gt_left {
  text-align: left;
}

#kaxqrteeac .gt_center {
  text-align: center;
}

#kaxqrteeac .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#kaxqrteeac .gt_font_normal {
  font-weight: normal;
}

#kaxqrteeac .gt_font_bold {
  font-weight: bold;
}

#kaxqrteeac .gt_font_italic {
  font-style: italic;
}

#kaxqrteeac .gt_super {
  font-size: 65%;
}

#kaxqrteeac .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#kaxqrteeac .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#kaxqrteeac .gt_indent_1 {
  text-indent: 5px;
}

#kaxqrteeac .gt_indent_2 {
  text-indent: 10px;
}

#kaxqrteeac .gt_indent_3 {
  text-indent: 15px;
}

#kaxqrteeac .gt_indent_4 {
  text-indent: 20px;
}

#kaxqrteeac .gt_indent_5 {
  text-indent: 25px;
}

#kaxqrteeac .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#kaxqrteeac div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<thead>
<tr class="gt_heading header">
<td colspan="4" class="gt_heading gt_title gt_font_normal gt_bottom_border"><strong>Summary Statistics</strong></td>
</tr>
<tr class="gt_col_headings even">
<th id="species" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col">species</th>
<th id="Mean" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">Mean</th>
<th id="SD" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">SD</th>
<th id="N" class="gt_col_heading gt_columns_bottom_border gt_right" data-quarto-table-cell-role="th" scope="col">N</th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_center" headers="species">Adelie</td>
<td class="gt_row gt_right" headers="Mean">3,700.66</td>
<td class="gt_row gt_right" headers="SD">458.57</td>
<td class="gt_row gt_right" headers="N">151</td>
</tr>
<tr class="even">
<td class="gt_row gt_center gt_striped" headers="species">Chinstrap</td>
<td class="gt_row gt_right gt_striped" headers="Mean">3,733.09</td>
<td class="gt_row gt_right gt_striped" headers="SD">384.34</td>
<td class="gt_row gt_right gt_striped" headers="N">68</td>
</tr>
<tr class="odd">
<td class="gt_row gt_center" headers="species">Gentoo</td>
<td class="gt_row gt_right" headers="Mean">5,076.02</td>
<td class="gt_row gt_right" headers="SD">504.12</td>
<td class="gt_row gt_right" headers="N">123</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<!-- added a header and backticks around function -->
</section>
<section id="practice" class="level3" data-number="2.5">
<h3 data-number="2.5" class="anchored" data-anchor-id="practice"><span class="header-section-number">2.5</span> Practice</h3>
<p>Your Turn: Custom Scatterplot</p>
<p>Write a function called pub_scatter that takes a dataset, an x variable, a y variable, and a color variable, and returns a cleanly formatted scatterplot.</p>
<details>
<summary>
Need a hint?
</summary>
Because you are passing unquoted variable names into <code>ggplot()</code>, you must remember to use the {{ }} operator for all of your variables (x, y, and color) inside the <code>aes()</code> wrapper.
</details>
<details>
<summary>
Need another hint?
</summary>
Start with a standard <code>ggplot()</code> call, then add <code>geom_point()</code>. Add <code>theme_minimal()</code> to quickly clean up the background, and <code>scale_color_viridis_d()</code> to ensure the colors are colorblind-friendly!
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Publication-Ready Scatterplot</span></span>
<span id="cb16-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb16-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param x_var The variable for the x-axis (unquoted)</span></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param y_var The variable for the y-axis (unquoted)</span></span>
<span id="cb16-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param color_var The variable to color points by (unquoted)</span></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A ggplot object</span></span>
<span id="cb16-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb16-9">pub_scatter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, x_var, y_var, color_var) {</span>
<span id="cb16-10">  </span>
<span id="cb16-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> {{ x_var }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> {{ y_var }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> {{ color_var }})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb16-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb16-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb16-17">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use a colorblind friendly palette</span></span>
<span id="cb16-19">}</span>
<span id="cb16-20"></span>
<span id="cb16-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test it out</span></span>
<span id="cb16-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pub_scatter</span>(penguins, flipper_length_mm, body_mass_g, species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Size by Species"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E09_packages_01/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
<p>The “Metadata”: DESCRIPTION, Documentation, and NAMESPACE Writing the functions is only half the battle. To make your code a true R package, R needs to know some metadata: what the package is called, who wrote it, what other packages it relies on, and how users can get help.</p>
<p>The DESCRIPTION File</p>
<!-- added backticks -->
<p>When we ran <code>usethis::create_package()</code>, it automatically generated a file called DESCRIPTION in our project directory. If you click on it in the Files pane, it looks something like this:</p>
<p>Package: vizwizard Title: What the Package Does (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person(“First”, “Last”, , “first.last@example.com”, role = c(“aut”, “cre”), comment = c(ORCID = “YOUR-ORCID-ID”)) Description: What the package does (one paragraph). License: <code>use_mit_license()</code>, <code>use_gpl3_license()</code> or friends to pick a license Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3</p>
<p>This is the ID card of your package. As a package developer, you should fill out the Title, update the Authors@R with your own information, and write a brief Description.</p>
<!-- added backticks -->
</section>
<section id="handling-dependencies" class="level3" data-number="2.6">
<h3 data-number="2.6" class="anchored" data-anchor-id="handling-dependencies"><span class="header-section-number">2.6</span> Handling Dependencies</h3>
<p>Our custom functions use <code>ggplot2</code>, <code>dplyr</code>, and <code>gt</code>. If a colleague installs <code>vizwizard</code>, R needs to know to install those packages for them, too.</p>
<p>A golden rule of package development: never type package names into the DESCRIPTION file manually. It is too easy to make a typo or format it incorrectly. Instead, let usethis do the heavy lifting safely:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run these directly in your console to add dependencies to your DESCRIPTION file</span></span>
<span id="cb18-2">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span>
<span id="cb18-3">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dplyr"</span>)</span>
<span id="cb18-4">usethis<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">use_package</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gt"</span>)</span></code></pre></div></div>
</div>
<p>you check your DESCRIPTION file now, you’ll see a new Imports: section listing these packages!</p>
<p>Generating Manual (man/) Files with roxygen2</p>
<p>Whenever you get stuck in R, what do you do? You type ?function_name into the console to pull up the help file. Those help files live in the man/ (manual) folder of a package.</p>
<p>Historically, writing these .Rd (R documentation) files was a nightmare of custom formatting. Today, we use the roxygen2 package to make it incredibly easy.</p>
<p>If you look back at our pub_histogram and pub_summary_table functions from earlier, you’ll notice I snuck in some special comments starting with #’ right above the code:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Publication-Ready Histogram</span></span>
<span id="cb19-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb19-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param var A numeric variable to plot (unquoted)</span></span>
<span id="cb19-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param title A string for the plot title</span></span>
<span id="cb19-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A ggplot object</span></span>
<span id="cb19-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span></code></pre></div></div>
</div>
<p>These are roxygen comments!</p>
<p><span class="citation" data-cites="param">@param</span> defines the arguments your function takes.</p>
<p><span class="citation" data-cites="return">@return</span> explains what the function outputs.</p>
<p><span class="citation" data-cites="export">@export</span> is crucial: it tells R to make this function available to the public when they run library(vizwizard). Without it, the function is “internal” and hidden from the user.</p>
<p>added headers and backticks ### Putting it all together: <code>devtools::document()</code></p>
<p>Right now, those are just comments in a script. To actually generate the help files and update our package’s metadata, we run one magical command in the console:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Translates your #' comments into actual help files!</span></span>
<span id="cb20-2">devtools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">document</span>()</span></code></pre></div></div>
</div>
<p>When you run this, two amazing things happen:</p>
<p>The man/ folder is populated: You will see files like pub_histogram.Rd appear in your file directory. You can now type ?pub_histogram into your console to see your very own custom help page!</p>
<p>The NAMESPACE file is updated: This file acts as the “border control” of your package, explicitly listing which functions are <span class="citation" data-cites="exported">@exported</span> for public use. Just like the dependencies, never edit the NAMESPACE file by hand. Let devtools::document() handle it.</p>
</section>
<section id="bonus-practice-the-jitter-boxplot" class="level3" data-number="2.7">
<h3 data-number="2.7" class="anchored" data-anchor-id="bonus-practice-the-jitter-boxplot"><span class="header-section-number">2.7</span> Bonus Practice: The “Jitter” Boxplot</h3>
<p>Boxplots are great, but showing the raw data points on top of them is even better for transparency.</p>
<p>Write a function called pub_boxplot that takes a dataset, a categorical variable for the x-axis, and a numeric variable for the y-axis. It should return a cleanly formatted boxplot with the raw data points overlaid as a “jitter” plot.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Just like the scatterplot, you will need to use the {{ }} operator for your x and y variables inside aes(). You might also want to set fill = {{ x_var }} so each box gets its own color!</p>
</details>
<details>
<summary>
Need another hint?
</summary>
<p>You will need two geoms for this: geom_boxplot() and geom_jitter(). To make it look professional, try making the jittered points slightly transparent by setting alpha = 0.5 inside geom_jitter(), and remove the redundant legend using theme(legend.position = “none”).</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Create a Publication-Ready Boxplot with Jittered Points</span></span>
<span id="cb21-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb21-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param data A dataframe</span></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param cat_var A categorical variable for the x-axis (unquoted)</span></span>
<span id="cb21-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @param num_var A numeric variable for the y-axis (unquoted)</span></span>
<span id="cb21-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @return A ggplot object</span></span>
<span id="cb21-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb21-8">pub_boxplot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, cat_var, num_var) {</span>
<span id="cb21-9">  </span>
<span id="cb21-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> {{ cat_var }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> {{ num_var }}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> {{ cat_var }})) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the boxplot, making the outliers invisible since we are plotting raw points anyway</span></span>
<span id="cb21-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outlier.shape =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the raw data points, slightly jittered so they don't overlap perfectly</span></span>
<span id="cb21-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb21-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hide the legend since the x-axis already labels the groups</span></span>
<span id="cb21-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>),</span>
<span id="cb21-19">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean up vertical grid lines</span></span>
<span id="cb21-20">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_viridis_d</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mako"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use a clean, colorblind-friendly palette</span></span>
<span id="cb21-22">}</span>
<span id="cb21-23"></span>
<span id="cb21-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test it out</span></span>
<span id="cb21-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pub_boxplot</span>(penguins, species, bill_length_mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb21-27">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin Bill Length by Species"</span>,</span>
<span id="cb21-28">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>,</span>
<span id="cb21-29">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill Length (mm)"</span></span>
<span id="cb21-30">  )</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E09_packages_01/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>R packages</category>
  <category>functions</category>
  <category>ggplot2</category>
  <category>gt</category>
  <guid>https://osu-codeclub.github.io/posts/S11E09_packages_01/</guid>
  <pubDate>Mon, 06 Apr 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E09_packages_01/img/package-box.png" medium="image" type="image/png" height="79" width="144"/>
</item>
<item>
  <title>Iterating part II: writing custom functions and purrr:map()</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E08_functions_02/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/img/custom-function.png" class="img-fluid figure-img" style="width:100.0%" alt="Generalized code for creating a new function."></p>
<figcaption>Generalized code for creating a new function</figcaption>
</figure>
</div>
</div>
</div>
<p>You might find yourself in a situation where you are copying and pasting your code multiple times, changing a single variable. This could be situation where you want to run a series of statistical tests, each with a different response variable. Or, you want to make a series of plots, adjusting which variables are on the x-axis. This is generally not a good idea, as it is error prone and inefficient.</p>
<p>We are spending this module going through things you can do to avoid copy and pasting your code. Last week we went over <a href="../S11E07_functions_01/">writing and using <code>for</code> loops</a>. Today we are going to talk about:</p>
<ul>
<li>writing your own functions</li>
<li>iterating with those functions using <a href="https://purrr.tidyverse.org/reference/map.html"><code>purrr:map()</code></a></li>
</ul>
<p>We will start, like we typically do, by loading the packages we will use today.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for wrangling and everything</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for penguins data</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for easy pasting of names</span></span></code></pre></div></div>
</div>
</section>
<section id="writing-functions" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="writing-functions"><span class="header-section-number">2</span> Writing functions</h2>
<p>There are many functions that exist in R, both built into the base functionality, and addition ones we can access by loading different packages. We have talked about <a href="https://osu-codeclub.github.io/posts/S07E03_basics_03/index.html#introduction">functions</a> before in Code Club, and you may be using them without explicitly realizing they are functions. For example, <code>mean()</code> is a function that calculates the mean of an object (usually a vector).</p>
<p>There are some functions which are by default available when you load R (like <code>mean()</code>), and others that exist within packages that require you to load those packages (e.g., <code>tidyverse</code>) before they can be used (e.g., <code>ggplot()</code>).</p>
<p>But, there may be situations where we want to do something in R, and a function does not exist to do this task. This could be because the operation you want to do is so specific to you that no other person could have possibly wanted to do that, or simply that no one has gotten around to writing a built in function yet. This is where writing your own functions can be very useful.</p>
<section id="general-syntax" class="level3" data-number="2.1">
<h3 data-number="2.1" class="anchored" data-anchor-id="general-syntax"><span class="header-section-number">2.1</span> General syntax</h3>
<p>The general syntax of a function is like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">name_of_function <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(arguments) {</span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">insert_code_here</span>()</span>
<span id="cb2-3">}</span></code></pre></div></div>
</div>
<p>You can pass multiple arguments to a function, but we are going to start simply with a function that takes one argument.</p>
</section>
<section id="functions-with-one-argument" class="level3" data-number="2.2">
<h3 data-number="2.2" class="anchored" data-anchor-id="functions-with-one-argument"><span class="header-section-number">2.2</span> Functions with one argument</h3>
<p>Here, I am writing a little function to do something simple, like calculate from millimeters to inches.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">mm_to_inch <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(value_in_mm) {</span>
<span id="cb3-2">  value_in_mm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0393701</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># code to convert from mm to inch</span></span>
<span id="cb3-3">}</span></code></pre></div></div>
</div>
<p>Here, we would call <code>value_in_mm</code> our argument or parameter. Remember that this is a user-defined term, you could call this <code>x</code>, <code>i</code>, or really whatever you want. I like giving the arguments easy-to-read and easy-to-remember names.</p>
<p>Now that we’ve made a function, we can see it in our environment under the “Functions” header.</p>
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/img/function-in-environment.png" class="img-fluid"></p>
<p>If we click on this item, we can see the details of the function.</p>
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/img/function-details.png" class="img-fluid"></p>
<p>If we start typing our function, and put our cursor between the parentheses and press tab, we can also see some information about our function.</p>
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/img/function-hover-details.png" class="img-fluid"></p>
<p>We can now pass arguments to our new function, here we will try passing 100 mm and see what that would be in inches.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mm_to_inch</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value_in_mm =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.93701</code></pre>
</div>
</div>
<p>Because our function only takes one argument, we can be less explicit and remove <code>value_in_mm</code> and get the same result.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mm_to_inch</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.93701</code></pre>
</div>
</div>
<p>We also might want to pass a whole column or vector to our function, which we can also do.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mm_to_inch</span>(penguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>bill_length_mm)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  [1] 1.539371 1.555119 1.586615       NA 1.444883 1.547245 1.531497 1.543308
  [9] 1.342520 1.653544 1.488190 1.488190 1.618111 1.519686 1.362205 1.440946
 [17] 1.523623 1.673229 1.354331 1.811025 1.488190 1.484253 1.413387 1.503938
 [25] 1.527560 1.389765 1.598426 1.594489 1.492127 1.594489 1.555119 1.464568
 [33] 1.555119 1.610237 1.433072 1.543308 1.527560 1.661418 1.480316 1.566930
 [41] 1.437009 1.606300 1.417324 1.736221 1.456694 1.559056 1.618111 1.476379
 [49] 1.417324 1.665355 1.559056 1.578741 1.377953 1.653544 1.358268 1.629922
 [57] 1.535434 1.598426 1.437009 1.480316 1.405513 1.625985 1.480316 1.618111
 [65] 1.433072 1.637796 1.397639 1.618111 1.413387 1.645670 1.318898 1.562993
 [73] 1.559056 1.803151 1.397639 1.685040 1.610237 1.464568 1.425198 1.657481
 [81] 1.362205 1.688977 1.444883 1.381891 1.468505 1.625985 1.429135 1.452757
 [89] 1.507875 1.531497 1.405513 1.618111 1.338583 1.559056 1.425198 1.606300
 [97] 1.500001 1.586615 1.303150 1.700788 1.377953 1.614174 1.484253 1.488190
[105] 1.492127 1.562993 1.519686 1.503938 1.500001 1.700788 1.500001 1.795277
[113] 1.562993 1.661418 1.559056 1.681103 1.519686 1.468505 1.405513 1.618111
[121] 1.425198 1.484253 1.582678 1.629922 1.385828 1.598426 1.527560 1.633859
[129] 1.535434 1.736221 1.515749 1.696851 1.448820 1.476379 1.500001 1.618111
[137] 1.401576 1.582678 1.456694 1.562993 1.582678 1.598426 1.263780 1.602363
[145] 1.468505 1.535434 1.543308 1.440946 1.417324 1.488190 1.417324 1.633859
[153] 1.814962 1.968505 1.917324 1.968505 1.874017 1.830710 1.787403 1.838584
[161] 1.704725 1.842521 1.610237 1.929135 1.791340 1.905513 1.803151 1.940946
[169] 1.653544 1.937009 1.818899 1.917324 1.976379 1.775592 1.830710 1.822836
[177] 1.688977 1.814962 1.751969 1.881891 1.897639 1.968505 1.862206 1.685040
[185] 1.775592 2.346458 1.933072 1.905513 1.677166 1.748032 1.732284 1.917324
[193] 1.681103 1.952757 1.783466 1.952757 1.988190 1.716536 1.791340 1.988190
[201] 1.767717 1.779529 1.834647 1.909450 1.775592 1.972442 1.830710 1.771654
[209] 1.724410 1.791340 1.700788 1.984253 1.783466 1.818899 1.799214 2.137796
[217] 1.803151 1.960631 1.818899 1.948820 1.712599 1.996064 1.877954 1.826773
[225] 1.897639 1.830710 1.826773 1.913387 1.870080 2.011812 1.779529 1.779529
[233] 1.933072 2.066930 1.866143 1.968505 1.767717 2.000001 1.708662 2.019686
[241] 1.870080 2.051182 1.870080 2.055119 1.791340 1.948820 1.751969 2.000001
[249] 1.944883 1.846458 1.905513 2.011812 1.909450 2.200789 1.858269 1.933072
[257] 1.862206 1.842521 1.641733 2.102363 1.704725 1.893702 1.988190 1.960631
[265] 1.712599 2.027560 1.818899 2.169293 1.751969 1.921261 1.858269       NA
[273] 1.842521 1.984253 1.779529 1.964568 1.830710 1.968505 2.019686 1.787403
[281] 2.074804 1.779529 1.814962 2.019686 1.811025 2.019686 1.834647 2.035434
[289] 1.850395 2.047245 1.807088 1.988190 1.980316 2.283466 1.826773 1.937009
[297] 1.669292 1.909450 1.700788 1.992127 1.838584 2.047245 1.988190 1.948820
[305] 1.826773 2.078741 1.610237 2.133859 1.673229 2.007875 1.956694 1.870080
[313] 1.874017 2.047245 1.846458 2.106300 1.929135 1.818899 2.003938 1.791340
[321] 2.003938 2.000001 1.972442 1.929135 2.027560 1.960631 1.893702 2.023623
[329] 1.799214 1.996064 1.673229 2.055119 1.779529 1.940946 1.976379 1.795277
[337] 2.043308 1.842521 1.799214 2.196852 1.712599 1.952757 2.000001 1.976379</code></pre>
</div>
</div>
<p>Instead of just printing this information, we might want to save it in our dataframe, which we can do by nesting our function within a <code>mutate()</code> function. Remember that <code>mutate()</code> works with the syntax <code>new_column = function(existing_variables)</code>. Also remember that by default, new columns are added after the last column (and if you want to also reorder, you can do that with <code>select()</code>).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bill_length_inch =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mm_to_inch</span>(bill_length_mm))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 9
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 3 more variables: sex &lt;fct&gt;, year &lt;int&gt;, bill_length_inch &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="embedding-built-in-functions" class="level3" data-number="2.3">
<h3 data-number="2.3" class="anchored" data-anchor-id="embedding-built-in-functions"><span class="header-section-number">2.3</span> Embedding built in functions</h3>
<p>We can also use other functions inside our function. For example, R does not by default have a function that calculates the coefficient of variation for a variable. We can write that ourselves though so it’s fine.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculate the coefficient of variation by </span></span>
<span id="cb12-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dividing the standard deviation by the mean</span></span>
<span id="cb12-3">cv <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb12-4">        (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(x)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x))</span>
<span id="cb12-5">}</span></code></pre></div></div>
</div>
</section>
<section id="functions-with-two-arguments" class="level3" data-number="2.4">
<h3 data-number="2.4" class="anchored" data-anchor-id="functions-with-two-arguments"><span class="header-section-number">2.4</span> Functions with two arguments</h3>
<p>We can also write functions that take more than one argument. Here is a simple example for calculating BMI, which is weight in kg divided by height in meters squared.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">calculate_bmi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(weight_kg, height_cm) {</span>
<span id="cb13-2">  weight_kg<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>(height_cm<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb13-3">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_bmi</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight_kg =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height_cm =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 24.69136</code></pre>
</div>
</div>
<p>If you provide your arguments in the order they are expected (here, <code>weight_kg</code> and then <code>height_cm</code>), then you don’t need to specify which is which. But, iif you want to give the arguments in a different order you need to be explicit.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is wrong</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_bmi</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 281.25</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is better</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_bmi</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height_cm =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">180</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weight_kg =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 24.69136</code></pre>
</div>
</div>
</section>
<section id="functions-with-one-argument-practice" class="level3" data-number="2.5">
<h3 data-number="2.5" class="anchored" data-anchor-id="functions-with-one-argument-practice"><span class="header-section-number">2.5</span> Functions with one argument practice</h3>
<p>Write two functions that convert between degrees Fahrenheit and degrees Celsius and test to see if they work.</p>
<details>
<summary>
Need a hint?
</summary>
<p>°C = (°F - 32) × 5/9 or °F = (°C * 9/5) + 32</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a function to convert F to C</span></span>
<span id="cb20-2">degF_to_degC <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(deg_F) {</span>
<span id="cb20-3">  (deg_F <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb20-4">}</span>
<span id="cb20-5"></span>
<span id="cb20-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a function to convert C to F</span></span>
<span id="cb20-7">degC_to_degF <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(deg_C) {</span>
<span id="cb20-8">  (deg_C <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span></span>
<span id="cb20-9">}</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degF_to_degC</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deg_F =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degF_to_degC</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deg_F =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 37.77778</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degC_to_degF</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deg_C =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 32</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">degC_to_degF</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">deg_C =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] -40</code></pre>
</div>
</div>
</details>
</section>
<section id="plotting-functions" class="level3" data-number="2.6">
<h3 data-number="2.6" class="anchored" data-anchor-id="plotting-functions"><span class="header-section-number">2.6</span> Plotting functions</h3>
<p>Now that we are a little comfortable with writing functions, we can make ones that are a little more complicated.</p>
<p>We can also write a function to make our plot.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">species_bill_plotting <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(focal_species) {</span>
<span id="cb29-2">    penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb29-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb29-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb29-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb29-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb29-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a linear model</span></span>
<span id="cb29-8">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turn off confidence interval</span></span>
<span id="cb29-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb29-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> focal_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># put the species name as the title</span></span>
<span id="cb29-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb29-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb29-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>, </span>
<span id="cb29-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data collected from LTER, Antarctica"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb29-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() </span>
<span id="cb29-16">}</span></code></pre></div></div>
</div>
<p>Then, we can pass our argument to the plot, here, the name of the species we want to plot. This is a useful tool for making many plots with the same structure.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">species_bill_plotting</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can then pass this function in a <code>for</code> loop with some simpler looking code (because the bulk of it is elsewhere).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">penguin_species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(penguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)</span>
<span id="cb32-2"></span>
<span id="cb32-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (focal_species <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> penguin_species) {</span>
<span id="cb32-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass each focal species to the species_bill_plotting function</span></span>
<span id="cb32-5">  plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">species_bill_plotting</span>(focal_species)</span>
<span id="cb32-6"></span>
<span id="cb32-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plots</span></span>
<span id="cb32-8">  filename <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figs/{focal_species}_bill-dimensions.png"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create filename</span></span>
<span id="cb32-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(filename, plot) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plot to file</span></span>
<span id="cb32-10">}</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'
Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'
Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
</div>
</section>
<section id="writing-plotting-functions-practice" class="level3" data-number="2.7">
<h3 data-number="2.7" class="anchored" data-anchor-id="writing-plotting-functions-practice"><span class="header-section-number">2.7</span> Writing plotting functions practice</h3>
<p>Write a function to make a series of boxplots that show the difference in <code>body_mass_g</code> between male and female penguins by species. Feed one species to your function to check if it works (you don’t need to iterate with it yet).</p>
<details>
<summary>
Need a hint?
</summary>
<p>Check back to our example where we used a <code>for</code> loop to see how to make this plot.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create the function</span></span>
<span id="cb34-2">body_mass_by_sex_plotting <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(focal_species) {</span>
<span id="cb34-3">    penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb34-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb34-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(sex, body_mass_g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb34-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-7">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-8">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-9">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't need one bc duplicative with x-axis</span></span>
<span id="cb34-11">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>,</span>
<span id="cb34-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (grams)"</span>,</span>
<span id="cb34-13">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> focal_species)</span>
<span id="cb34-14">}</span>
<span id="cb34-15"></span>
<span id="cb34-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># apply the function to Adelie penguins</span></span>
<span id="cb34-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">body_mass_by_sex_plotting</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
</section>
<section id="iteration-with-purrrmap" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="iteration-with-purrrmap"><span class="header-section-number">3</span> Iteration with <code>purrr:map</code></h2>
<p>To simplify our code, we could replace our <code>for</code> loop with one of the <code>tidyverse:purrr</code> functions intended for functional program that being with <code>map*()</code>.</p>
<p>But before we do this, we need to learn a little bit about lists.</p>
<section id="about-lists" class="level3" data-number="3.1">
<h3 data-number="3.1" class="anchored" data-anchor-id="about-lists"><span class="header-section-number">3.1</span> About lists</h3>
<p>A list is a new data structure for Code Club. A list can contain other data structures, like data frames, vectors or others, within it.</p>
<p>To see what a list looks like, we will look under the hood at the object that comprises a ggplot plot. We have one in our environment called <code>plot</code> so let’s look at that.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">View</span>(plot)</span></code></pre></div></div>
</div>
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/img/view-plot.png" class="img-fluid"></p>
<p>We can see that in this case, <code>plot</code> is a list which contains 11 objects, many of which also contain sub-objects. For example, <code>data</code> is a dataframe that. contains the data underlying our plot. It is a tibble type of dataframe with 68 rows and 8 columns.</p>
<p>If I want to access that first item in the list, I can do that in a few different ways.</p>
<p>I can use the dollar sign operator <code>$</code> to extract parts of the list</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">plot<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>data</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 68 × 8
   species   island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;     &lt;fct&gt;           &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Chinstrap Dream            46.5          17.9               192        3500
 2 Chinstrap Dream            50            19.5               196        3900
 3 Chinstrap Dream            51.3          19.2               193        3650
 4 Chinstrap Dream            45.4          18.7               188        3525
 5 Chinstrap Dream            52.7          19.8               197        3725
 6 Chinstrap Dream            45.2          17.8               198        3950
 7 Chinstrap Dream            46.1          18.2               178        3250
 8 Chinstrap Dream            51.3          18.2               197        3750
 9 Chinstrap Dream            46            18.9               195        4150
10 Chinstrap Dream            51.3          19.9               198        3700
# ℹ 58 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<p>Or, I can use indexing to extract out the first item in our list.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">plot[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>$data
# A tibble: 68 × 8
   species   island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;     &lt;fct&gt;           &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Chinstrap Dream            46.5          17.9               192        3500
 2 Chinstrap Dream            50            19.5               196        3900
 3 Chinstrap Dream            51.3          19.2               193        3650
 4 Chinstrap Dream            45.4          18.7               188        3525
 5 Chinstrap Dream            52.7          19.8               197        3725
 6 Chinstrap Dream            45.2          17.8               198        3950
 7 Chinstrap Dream            46.1          18.2               178        3250
 8 Chinstrap Dream            51.3          18.2               197        3750
 9 Chinstrap Dream            46            18.9               195        4150
10 Chinstrap Dream            51.3          19.9               198        3700
# ℹ 58 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
</section>
<section id="map" class="level3" data-number="3.2">
<h3 data-number="3.2" class="anchored" data-anchor-id="map"><span class="header-section-number">3.2</span> <code>map()</code></h3>
<p>The simplest of the functions in <code>purrr</code> is <a href="https://purrr.tidyverse.org/reference/map.html"><code>map()</code></a>, which applies a function over each element of a vector, and returns a list. There are some basics we will have to go over for this to make sense.</p>
<p>The <code>map()</code>, we provide a vector/list to iterate over, and a function, like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.x =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># our vector to apply our function over</span></span>
<span id="cb40-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># what the function is</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] 2

[[2]]
[1] 3

[[3]]
[1] 4

[[4]]
[1] 5

[[5]]
[1] 6</code></pre>
</div>
</div>
<p>Now let’s get back to the function we wrote to make our plots - instead of incorporating this into a <code>for</code> loop, we can apply it within <code>map()</code> like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(penguin_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vector to iterate over</span></span>
<span id="cb42-2">    species_bill_plotting) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># function to use</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-24-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
[[2]]</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-24-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
[[3]]</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-24-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Wow that was simple!</p>
<p>We could also incorporate both the printing and saving of the plot into one function like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1">species_bill_plotting_saving <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(focal_species) {</span>
<span id="cb49-2">  </span>
<span id="cb49-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create plot and assign it to something </span></span>
<span id="cb49-4">  plot_via_function <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb49-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb49-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb49-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb49-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a linear model</span></span>
<span id="cb49-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turn off confidence interval</span></span>
<span id="cb49-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> focal_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># put the species name as the title</span></span>
<span id="cb49-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb49-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb49-15">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>, </span>
<span id="cb49-16">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data collected from LTER, Antarctica"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb49-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() </span>
<span id="cb49-18">  </span>
<span id="cb49-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print</span></span>
<span id="cb49-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plot_via_function)</span>
<span id="cb49-21">  </span>
<span id="cb49-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save</span></span>
<span id="cb49-23">  filename <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figs/{focal_species}_bill-dimensions-via-function.png"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># choose filename</span></span>
<span id="cb49-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(filename, plot_via_function) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plot to file</span></span>
<span id="cb49-25">}</span></code></pre></div></div>
</div>
<p>Then, this code will:</p>
<ol type="1">
<li>Generate your plots</li>
<li>Print those plots for you to view in RStudio</li>
<li>Save those plots in a directory called <code>figs</code> in your working directory</li>
</ol>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(penguin_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># vector to iterate over</span></span>
<span id="cb50-2">    species_bill_plotting_saving) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># function to apply</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-26-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-26-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-26-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
figs/Adelie_bill-dimensions-via-function.png

[[2]]
figs/Gentoo_bill-dimensions-via-function.png

[[3]]
figs/Chinstrap_bill-dimensions-via-function.png</code></pre>
</div>
</div>
</section>
<section id="functions-with-more-arguments-practice" class="level3" data-number="3.3">
<h3 data-number="3.3" class="anchored" data-anchor-id="functions-with-more-arguments-practice"><span class="header-section-number">3.3</span> Functions with more arguments practice</h3>
<p>Try applying your function you made to create a series of boxplots for body mass by sex over each of the penguin species, though edit it so it both prints and saves your plots.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Here is my hint</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<p>Edit the function to include printing and saving.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb56" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1">species_body_mass_sex_plotting_saving <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(focal_species) {</span>
<span id="cb56-2">  my_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb56-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb56-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(sex, body_mass_g) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb56-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb56-6">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb56-7">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb56-8">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb56-9">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't need one bc duplicative with x-axis</span></span>
<span id="cb56-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>,</span>
<span id="cb56-11">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (grams)"</span>,</span>
<span id="cb56-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> focal_species)</span>
<span id="cb56-13"></span>
<span id="cb56-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print</span></span>
<span id="cb56-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(my_plot)</span>
<span id="cb56-16">  </span>
<span id="cb56-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save</span></span>
<span id="cb56-18">  filename <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figs/{focal_species}_body-mass-by-sex.png"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># choose filename</span></span>
<span id="cb56-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(filename, my_plot) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plot to file</span></span>
<span id="cb56-20">}</span></code></pre></div></div>
</div>
<p>Apply the function across the species</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(penguin_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># what to iterate over</span></span>
<span id="cb57-2">    species_body_mass_sex_plotting_saving) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the function to iterate with</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-28-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-28-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E08_functions_02/index_files/figure-html/unnamed-chunk-28-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
figs/Adelie_body-mass-by-sex.png

[[2]]
figs/Gentoo_body-mass-by-sex.png

[[3]]
figs/Chinstrap_body-mass-by-sex.png</code></pre>
</div>
</div>
</details>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>functions</category>
  <category>functional programming</category>
  <category>purrr</category>
  <guid>https://osu-codeclub.github.io/posts/S11E08_functions_02/</guid>
  <pubDate>Mon, 30 Mar 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E08_functions_02/img/custom-function.png" medium="image" type="image/png" height="27" width="144"/>
</item>
<item>
  <title>Iterating part I: for loops</title>
  <dc:creator>Jessica Cooperstone and Jelmer Poelstra</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E07_functions_01/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/img/for-loop-monsters.png" class="img-fluid figure-img" style="width:100.0%" alt="Illustrated for loop example. Input vector is a parade of monsters, including monsters that are circles, triangles, and squares. The for loop they enter has an if-else statement: if the monster is a triangle, it gets sunglasses. Otherwise, it gets a hat. The output is the parade of monsters where the same input parade of monsters shows up, now wearing either sunglasses (if triangular) or a hat (if any other shape)."></p>
<figcaption>Cartoon from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p>You might find yourself in a situation where you are copying and pasting your code multiple times, changing a single variable. This could be situation where you want to run a series of statistical tests, each with a different response variable. Or, you want to make a series of plots, adjusting which variables are on the x-axis. This is generally not a good idea, as it is error prone and inefficient.</p>
<p>We are going to spend the next few sections of Code Club talking about things you can do to avoid copy and pasting your code. Today, we are going to talk about:</p>
<ul>
<li><code>for</code> loops</li>
<li>writing your own functions</li>
</ul>
<p>We will start, like we typically do, by loading the packages we will use today.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for wrangling and everything</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for penguins data</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for easy pasting of names</span></span></code></pre></div></div>
</div>
</section>
<section id="for-loops" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="for-loops"><span class="header-section-number">2</span> <code>for</code> loops</h2>
<section id="for-loops-the-basics" class="level3" data-number="2.1">
<h3 data-number="2.1" class="anchored" data-anchor-id="for-loops-the-basics"><span class="header-section-number">2.1</span> <code>for</code> loops, the basics</h3>
<p>For loops iterate over some kind of sequence, performing an action, one element at a time. The basic syntax is like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (item <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> list_of_items) { <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># items to iterate over</span></span>
<span id="cb2-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># code that does something</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do_something</span>(item) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is a made up function</span></span>
<span id="cb2-4">}</span></code></pre></div></div>
</div>
<p>Let’s go through a simple example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (some_value <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) {</span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-3">}</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2
[1] 4
[1] 6
[1] 8
[1] 10</code></pre>
</div>
</div>
<p>What is happening under the hood, is that your <code>for</code> loop is assigning each item in your vector (here, the integer values 1 through 5) to the object <code>some_value</code>, multiplying it by 2, and then printing it. If we want to be very explicit, this is exactly what is happening:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iteration 1</span></span>
<span id="cb5-2">some_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iteration 2</span></span>
<span id="cb7-2">some_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb7-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iteration 3</span></span>
<span id="cb9-2">some_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 6</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iteration 4</span></span>
<span id="cb11-2">some_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 8</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># iteration 5</span></span>
<span id="cb13-2">some_value <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb13-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10</code></pre>
</div>
</div>
<p>If we do this again in the loop:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (some_value <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) {</span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(some_value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb15-3">}</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2
[1] 4
[1] 6
[1] 8
[1] 10</code></pre>
</div>
</div>
<p>And check what <code>some_value</code> is at the end:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">some_value</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5</code></pre>
</div>
</div>
<p>We can see that the value 5 (last of <code>some_value</code>) is currently assigned. This is not totally intuitive - you might think that <code>some_value</code> would hold all of the values that we set to that object in our loop but <em>this is not true</em>. This is good to remember since this is now an object lingering in our environment.</p>
<p>I have shown this use-case of a <code>for</code> loop because I think it helps you to see how it works - not because you would actually use it to do this task. Because R is a vectorized language, you don’t actually need a <code>for</code> loop to multiply every value by a number, we can do this task more simply below.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># multiply each element of the vector 1:5 by 2</span></span>
<span id="cb19-2"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  2  4  6  8 10</code></pre>
</div>
</div>
<p>Let’s do another example. I could create a <code>for</code> loop that simply prints something out for me. Here’s an example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a vector of the names of my special flowers</span></span>
<span id="cb21-2">my_special_flowers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dahlias"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roses"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ranunculus"</span>)</span>
<span id="cb21-3"></span>
<span id="cb21-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a for loop to print this message for each flower</span></span>
<span id="cb21-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (flower <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_special_flowers) {</span>
<span id="cb21-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"I really like {flower}"</span>))</span>
<span id="cb21-7">}</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>I really like dahlias
I really like roses
I really like ranunculus</code></pre>
</div>
</div>
<p>You can also make this conditional, meaning your <code>for</code> loop does one thing under certain conditions, and something else in another condition. We can accomplish this by nesting an <code>if else</code> statement within our <code>for</code> loop.</p>
<p>We are also making use of the function <a href="https://glue.tidyverse.org/"><code>glue()</code></a> (which we’ve gone over before) which allows easy pasting, like this:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">my_location <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Code Club"</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Welcome to {my_location}"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Welcome to Code Club</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a vector of solanaceous vegetables</span></span>
<span id="cb25-2">my_vegetables <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tomato"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Potato"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pepper"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Eggplant"</span>)</span>
<span id="cb25-3"></span>
<span id="cb25-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (vegetable <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_vegetables) {</span>
<span id="cb25-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if the vegetable is tomato, print this message  </span></span>
<span id="cb25-6">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (vegetable <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tomato"</span>) {</span>
<span id="cb25-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{vegetable} is my favorite Solanaceae vegetable"</span>))</span>
<span id="cb25-8">  }</span>
<span id="cb25-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if the vegetable is anything else, print this message  </span></span>
<span id="cb25-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb25-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{vegetable} is a perfectly fine Solanaceae vegetable"</span>))</span>
<span id="cb25-12">  }</span>
<span id="cb25-13">}</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Tomato is my favorite Solanaceae vegetable
Potato is a perfectly fine Solanaceae vegetable
Pepper is a perfectly fine Solanaceae vegetable
Eggplant is a perfectly fine Solanaceae vegetable</code></pre>
</div>
</div>
</section>
<section id="for-loops-basics-practice" class="level3" data-number="2.2">
<h3 data-number="2.2" class="anchored" data-anchor-id="for-loops-basics-practice"><span class="header-section-number">2.2</span> <code>for</code> loops basics practice</h3>
<p>Write a <code>for</code> loop that takes the vector of numbers -5, -3, 2, 4, 7 and prints their absolute value.</p>
<details>
<summary>
Need a hint?
</summary>
<p><br> The function for absolute value is <code>abs()</code>.</p>
</details>
<details>
<summary>
Need another hint?
</summary>
<p><br> First create a vector of your numbers, then iterate over it.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create your vector of numbers to iterate over</span></span>
<span id="cb27-2">my_vector <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb27-3"></span>
<span id="cb27-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a for loop to do the iterating</span></span>
<span id="cb27-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (number <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_vector) {</span>
<span id="cb27-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(number))</span>
<span id="cb27-7">}</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5
[1] 3
[1] 2
[1] 4
[1] 7</code></pre>
</div>
</div>
</details>
<p>Write a <code>for</code> loop that, when given a number, will tell you whether an positive integer is odd or even. Don’t worry about negatives or zero here.</p>
<details>
<summary>
Need a hint?
</summary>
<p><br> A function that would be useful here is the modulo, <code>%%</code>, where from the remainder, you can determine odd or even.</p>
</details>
<details>
<summary>
Need another hint?
</summary>
<p><br> Create a vector of numbers you want to see if they are odd or even. Then write a <code>for</code> loop to print whether they are odd or even.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">some_integers <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">401</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb29-2"></span>
<span id="cb29-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (number <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> some_integers) {</span>
<span id="cb29-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if the number is divisible by 2, print that its even </span></span>
<span id="cb29-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (number <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) {</span>
<span id="cb29-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{number} is an even number"</span>))</span>
<span id="cb29-7">  }</span>
<span id="cb29-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># if the number is anything else, print that its odd </span></span>
<span id="cb29-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb29-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{number} is an odd number"</span>))</span>
<span id="cb29-11">  }</span>
<span id="cb29-12">}</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>1 is an odd number
5 is an odd number
12 is an even number
401 is an odd number
1000 is an even number</code></pre>
</div>
</div>
</details>
</section>
<section id="for-loops-for-plotting" class="level3" data-number="2.3">
<h3 data-number="2.3" class="anchored" data-anchor-id="for-loops-for-plotting"><span class="header-section-number">2.3</span> <code>for</code> loops, for plotting</h3>
<p>So far, we have done things in a <code>for</code> loop do not really require a <code>for</code> loop to accomplish, though I want you to see how they work first with simpler examples. Making a bunch of plots at once is something that is really easier and less error prone with a <code>for</code> loop.</p>
<p>First let’s revisit our <code>penguins</code> data to create a base plot, and then iterate it over a variable to make multiples.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(penguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species           &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               &lt;fct&gt; male, female, female, NA, female, male, female, male…
$ year              &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
<p>What if we want to create a plot comparing the <code>bill_length_mm</code> vs.&nbsp;<code>bill_depth_mm</code> for each species? We can:</p>
<ul>
<li>take our <code>penguins</code> data</li>
<li><code>filter()</code> for a single <code>species</code></li>
<li>make our plot</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb33-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select only the Adelie penguins  </span></span>
<span id="cb33-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb33-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values for our variables of interest  </span></span>
<span id="cb33-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(bill_length_mm, bill_depth_mm, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb33-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb33-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a linear model</span></span>
<span id="cb33-9">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turn off confidence interval</span></span>
<span id="cb33-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># put the species name as the title</span></span>
<span id="cb33-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb33-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb33-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() </span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Now we have a plot, if we want to create one per <code>species</code> (in this case there are 3), we can use a <code>for</code> loop to do this.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a vector that has all the penguin species names</span></span>
<span id="cb35-2">penguin_species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(penguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>species)</span>
<span id="cb35-3"></span>
<span id="cb35-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a for loop to iterate over the penguin species</span></span>
<span id="cb35-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (focal_species <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> penguin_species) {</span>
<span id="cb35-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># filter your df for each focal_species  </span></span>
<span id="cb35-7">  one_penguin_species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb35-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb35-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(bill_length_mm, bill_depth_mm, sex) </span>
<span id="cb35-10">  </span>
<span id="cb35-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb35-12">  plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> one_penguin_species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>   </span>
<span id="cb35-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb35-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb35-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a linear model</span></span>
<span id="cb35-16">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turn off confidence interval</span></span>
<span id="cb35-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb35-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> focal_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># put the species name as the title</span></span>
<span id="cb35-19">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb35-20">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb35-21">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb35-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() </span>
<span id="cb35-23"></span>
<span id="cb35-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print the plot</span></span>
<span id="cb35-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plot)</span>
<span id="cb35-26">}</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-16-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-16-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can also add to our <code>for</code> loop to save the plots in a location of our choosing. We can start by creating a new directory (in your working directory) called <code>figs</code> which is where we will store our figures.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># write code to create a directory called "figs" in our working directory</span></span>
<span id="cb36-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dir.create</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">path =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figs"</span>)</span></code></pre></div></div>
</div>
<p>Instead of printing the plot, we are going to save it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (focal_species <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> penguin_species) {</span>
<span id="cb37-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># filter your df for each focal_species  </span></span>
<span id="cb37-3">  one_penguin_species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb37-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb37-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(bill_length_mm, bill_depth_mm, sex) </span>
<span id="cb37-6">  </span>
<span id="cb37-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb37-8">  plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> one_penguin_species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>   </span>
<span id="cb37-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb37-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use a linear model</span></span>
<span id="cb37-12">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">se =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># turn off confidence interval</span></span>
<span id="cb37-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> focal_species, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># put the species name as the title</span></span>
<span id="cb37-15">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb37-16">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill depth (mm)"</span>,</span>
<span id="cb37-17">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() </span>
<span id="cb37-19"></span>
<span id="cb37-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plots</span></span>
<span id="cb37-21">  filename <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figs/{focal_species}_bill-dimensions.png"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create filename</span></span>
<span id="cb37-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> filename, </span>
<span id="cb37-23">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> plot) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plot to file</span></span>
<span id="cb37-24">}</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'
Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'
Saving 7 x 5 in image
`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
</div>
<p>Now time to practice.</p>
</section>
<section id="for-loops-for-plotting-practice" class="level3" data-number="2.4">
<h3 data-number="2.4" class="anchored" data-anchor-id="for-loops-for-plotting-practice"><span class="header-section-number">2.4</span> <code>for</code> loops, for plotting practice</h3>
<p>Write a <code>for</code> loop to make a series of boxplots that show the difference in <code>body_mass_g</code> between male and female penguins by species. Try both printing and saving the plots.</p>
<details>
<summary>
Need a hint?
</summary>
<p><br> You are going to iterate over species.</p>
</details>
<details>
<summary>
Need another hint?
</summary>
<p><br> Here is a sample little base plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb39-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb39-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(body_mass_g, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb39-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't need one bc duplicative with x-axis</span></span>
<span id="cb39-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>,</span>
<span id="cb39-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (grams)"</span>,</span>
<span id="cb39-10">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (focal_species <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> penguin_species) {</span>
<span id="cb40-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># filter your df for each focal_species  </span></span>
<span id="cb40-3">  one_penguin_species <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb40-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> focal_species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb40-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(bill_length_mm, bill_depth_mm, sex) </span>
<span id="cb40-6">  </span>
<span id="cb40-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot</span></span>
<span id="cb40-8">  plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> one_penguin_species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>   </span>
<span id="cb40-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-11">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb40-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># don't need one bc duplicative with x-axis</span></span>
<span id="cb40-14">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span>,</span>
<span id="cb40-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (grams)"</span>,</span>
<span id="cb40-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>)</span>
<span id="cb40-17"></span>
<span id="cb40-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># print the plots  </span></span>
<span id="cb40-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(plot)</span>
<span id="cb40-20"></span>
<span id="cb40-21"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plots</span></span>
<span id="cb40-22">  filename <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figs/{focal_species}_body-mass-by-sex.png"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create filename</span></span>
<span id="cb40-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> filename, </span>
<span id="cb40-24">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> plot) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save the plot to file</span></span>
<span id="cb40-25">}</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-20-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E07_functions_01/index_files/figure-html/unnamed-chunk-20-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
</div>
</details>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>for loops</category>
  <category>base R</category>
  <guid>https://osu-codeclub.github.io/posts/S11E07_functions_01/</guid>
  <pubDate>Mon, 23 Mar 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E07_functions_01/img/for-loop-monsters.png" medium="image" type="image/png" height="88" width="144"/>
</item>
<item>
  <title>Preparing (gg)plots for publication: part II</title>
  <dc:creator>Jelmer Poelstra</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E06_ggplot_06/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<section id="what-well-cover" class="level3" data-number="1.1">
<h3 data-number="1.1" class="anchored" data-anchor-id="what-well-cover"><span class="header-section-number">1.1</span> What we’ll cover</h3>
<p>Today, we’ll continue on the topic of preparing your plots for publication. Specifically, we’ll cover:</p>
<ul>
<li><p><strong>Saving plots to file</strong> – with <em>ggplot</em>’s <code>ggsave()</code> <em>(moved from last weeksince we didn’t get there)</em></p></li>
<li><p>Advanced <strong>text formatting</strong> with <em>ggtext</em>. For example, italicizing individual words in axis titles and legends.</p></li>
<li><p>Adding any kind of <strong>image to a plot</strong>, including:</p>
<ul>
<li>An image inside a plot</li>
<li>A plot of an image in a multi-panel layout</li>
</ul></li>
</ul>
</section>
<section id="setup" class="level3" data-number="1.2">
<h3 data-number="1.2" class="anchored" data-anchor-id="setup"><span class="header-section-number">1.2</span> Setup</h3>
<section id="loading-packages" class="level4">
<h4 class="anchored" data-anchor-id="loading-packages">Loading packages</h4>
<p>We’ll start by loading the <em>tidyverse</em>, which includes <em>ggplot2</em>, and <em>palmerpenguins</em>, which contains the penguins dataset we’ve been working with:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'ggplot2' was built under R version 4.5.2</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'tidyr' was built under R version 4.5.2</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'

The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
</div>
<p>Like last week, we will load the <em>patchwork</em> package as well, which provides a convenient syntax for multi-panel layouts:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span></code></pre></div></div>
</div>
</section>
<section id="setting-the-theme" class="level4">
<h4 class="anchored" data-anchor-id="setting-the-theme">Setting the theme</h4>
<p>Like we learned last week, we can set a plotting theme with <code>theme_set()</code>, and customize the theme with <code>theme_update()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>))</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_update</span>(</span>
<span id="cb8-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)</span>
<span id="cb8-4">)</span></code></pre></div></div>
</div>
</section>
<section id="a-base-plot" class="level4">
<h4 class="anchored" data-anchor-id="a-base-plot">A base plot</h4>
<p>We’ll also create the same basic plot as last week, so that we have a plot to work with when saving plots:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get rid of rows with NAs</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_comma</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>)</span>
<span id="cb9-8"></span>
<span id="cb9-9">p</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
</section>
</section>
</section>
<section id="saving-plots-to-file" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="saving-plots-to-file"><span class="header-section-number">2</span> Saving plots to file</h2>
<p>So far, we printed plots to the screen in RStudio but did not save them to file. You may have noticed the “Export” button in the Plots pane — it works, but a more flexible and reproducible approach is the <code>ggsave()</code> function.</p>
<section id="basic-usage" class="level3" data-number="2.1">
<h3 data-number="2.1" class="anchored" data-anchor-id="basic-usage"><span class="header-section-number">2.1</span> Basic usage</h3>
<p>Tell <code>ggsave()</code> about the filename and the plot you want to save:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image</code></pre>
</div>
</div>
<ul>
<li><p>The <strong>file type</strong> is determined by the extension you provide. Above, we used <code>.png</code> i.e.&nbsp;a PNG file, but there are other options, as we’ll see in a bit.</p></li>
<li><p>We did not specify the <code>width</code> and <code>height</code> of the figure above, so it reports saving it to a default size of 7 × 5 inches.</p></li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Saving plots without specifying the plot object <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>By default, <code>ggsave()</code> saves the last plot you produced, and you only need to specify the filename:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plot.png"</span>)</span></code></pre></div></div>
</div>
<p>This can e.g.&nbsp;be handy because you may not have saved your plot to an object.</p>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Saving the file in a different folder on your computer <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>In both cases above, the file would be saved to the folder that represents your current “working directory” (directory = folder) in R. But you can save the file to any location on your computer by specifying the path as part of the filename:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save to a "figures" subfolder in the current working directory:</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"figures/plot.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save to a specific folder on your computer:</span></span>
<span id="cb13-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C:/Users/YourName/Documents/figures/plot.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p)</span></code></pre></div></div>
</div>
<p>To learn more about file paths and how R deals with them, see <a href="../../posts/S10E03_reprod_03/">this Code Club session from last fall</a>.</p>
<p>If the folder you’re trying to save to does not already exist, a prompt like this will appear to create the folder:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/img/ggsave_create-folder.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:45.0%"></p>
</figure>
</div>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="controlling-figure-size" class="level3" data-number="2.2">
<h3 data-number="2.2" class="anchored" data-anchor-id="controlling-figure-size"><span class="header-section-number">2.2</span> Controlling figure size</h3>
<p><code>ggsave()</code> accepts <code>width</code> and <code>height</code> arguments, with a default unit of inches<sup>1</sup>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"wide.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span></code></pre></div></div>
</div>
<p>The figure’s size may not seem to matter much, because you could always resize the figure on the computer, right? In fact, what does an inch even mean in the context of a figure displayed on a screen?</p>
<p>But these dimensions are important when considering the figure’s <strong>aspect ratio</strong> and the <strong>relative text size</strong>.</p>
<section id="aspect-ratio" class="level4">
<h4 class="anchored" data-anchor-id="aspect-ratio">Aspect ratio</h4>
<p>The figure’s <code>width</code> relative to the <code>height</code>, somewhat obviously, determines its aspect ratio – and this can be useful to be mindful of and customize when appropriate.</p>
<p>Above, our figure is 50% wider than it is high, and that is sensible because we have a legend on the right-hand side of the plot that takes up horizontal space. But for the sake of practice, let’s save the same plot with a different aspect ratio:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tall.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div></div>
</div>
<p>Here is what the resulting figures should look like:</p>
<div class="columns">
<div class="column" style="width:48%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/wide.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%"></p>
</figure>
</div>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/tall.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="relative-text-size" class="level4">
<h4 class="anchored" data-anchor-id="relative-text-size">Relative text size</h4>
<p><strong>Text and points do not scale with the figure</strong> — they stay at their absolute sizes (in points/mm). This means:</p>
<ul>
<li>A plot saved at 3 × 3 will have large-looking text relative to the data</li>
<li>A plot saved at 10 × 8 will have small-looking text</li>
</ul>
<p>This behavior may seem strange but is actually useful: if your text looks too large or too small in the saved file, you can adjust <code>width</code> and <code>height</code> rather than modifying the plotting code. Let’s see this in action, too:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Small" figure — text will look relatively large</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"small.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Large" figure — text will look relatively small</span></span>
<span id="cb16-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"large.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div></div>
</div>
<p>Here is what the resulting figures should look like:</p>
<div class="columns">
<div class="column" style="width:48%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/small.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%"></p>
</figure>
</div>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/large.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:100.0%"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="raster-vs.-vector-formats" class="level3" data-number="2.3">
<h3 data-number="2.3" class="anchored" data-anchor-id="raster-vs.-vector-formats"><span class="header-section-number">2.3</span> Raster vs.&nbsp;vector formats</h3>
<p>When saving a figure, you have two fundamentally different types of format to choose from:</p>
<ul>
<li><p><strong>Raster formats</strong> (PNG, JPEG, TIFF) store the image as a grid of pixels.<br>
Quality is fixed at save time using the figure’s “<em>resolution</em>” — but zoom in far enough and you’ll see pixelation. Among these formats, TIFF is generally preferred for scientific publications while PNG may also work, while JPEG should typically be avoided.</p></li>
<li><p><strong>Vector formats</strong> (SVG, PDF) store the image as geometric instructions — lines, curves, text.<br>
They render sharply at any size, and text is selectable and searchable. Among these formats, SVG is generally preferred or scientific publications.</p></li>
</ul>
<p>For publication, <strong>vector formats are generally preferable</strong> when the journal accepts them. Figures will be crisp at any print size, and editors or co-authors can make minor tweaks in a vector editor such as InkScape or Illustrator.</p>
<p>To save to SVG format, we need the <code>svglite</code> package – let’s install that:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"svglite"</span>)</span>
<span id="cb17-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (We don't need to load it with library() because ggsave() will use it </span></span>
<span id="cb17-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  automatically when we save to SVG)</span></span></code></pre></div></div>
</div>
<p>As mentioned above, the filetype is simply specified in <code>ggsave()</code> using the file extension. Earlier, we saved to PNG, so let’s now practice saving to SVG:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"large.svg"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Controlling resolution for raster formats
</div>
</div>
<div class="callout-body-container callout-body">
<p>For raster formats (PNG, JPEG, TIFF), resolution of the figure is controlled with the <code>dpi</code> argument (dots per inch).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(</span>
<span id="cb19-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filename =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"large_dpi300.png"</span>,</span>
<span id="cb19-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> p,</span>
<span id="cb19-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>,</span>
<span id="cb19-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>,</span>
<span id="cb19-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dpi =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span></span>
<span id="cb19-7">  )</span></code></pre></div></div>
</div>
<p>A dpi of 300 is sufficient in almost any context, though some journals may request a resolution up to 600 dpi or so.</p>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-compare-formats" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-compare-formats"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Compare formats</h4>
<p>Open the SVG file, and the two <code>large</code> PNG files, and compare how they look when you zoom in closely.</p>
<div class="columns">
<div class="column" style="width:47%;">
<p><strong>PNG (at 300 dpi!)</strong>:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="img/png_zoom.png" class="lightbox" data-gallery="quarto-lightbox-gallery-1"><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/img/png_zoom.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:90.0%"></a></p>
</figure>
</div>
</div><div class="column" style="width:1%;">

</div><div class="column" style="width:52%;">
<p><strong>SVG</strong>:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="img/svg_zoom.png" class="lightbox" data-gallery="quarto-lightbox-gallery-2"><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/img/svg_zoom.png" class="img-fluid quarto-figure quarto-figure-center figure-img" style="width:90.0%"></a></p>
</figure>
</div>
</div>
</div>
</section>
<hr style="height:1pt; visibility:hidden;">
</section>
</section>
<section id="format-individual-words-with-ggtext" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="format-individual-words-with-ggtext"><span class="header-section-number">3</span> Format individual words with <em>ggtext</em></h2>
<p>While you can set the fontface (plain, bold, italic, etc.) of axis titles, legend titles, and so on with <code>theme()</code>, you can’t make individual words in these titles bold or italic with that approach. (The same is true for applying colors.)</p>
<p><em>ggtext</em> is a package that lets you use Markdown and HTML markup to format text in ggplot2 plots. It is useful for things like bolding, italicizing or coloring individual words in a label. Install it if you don’t have it yet:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggtext"</span>)</span></code></pre></div></div>
</div>
<p>Then load it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggtext)</span></code></pre></div></div>
</div>
<p>The main function in <em>ggtext</em> is <code>element_markdown()</code>. It works as a drop-in replacement for <code>element_text()</code> in <code>theme()</code> calls, and enables Markdown and HTML parsing for that text element.</p>
<p>Let’s say for the sake of argument that in our penguin scatterplot, we wanted to make parts of the axis titles bold and italic. The <a href="https://www.markdownguide.org/basic-syntax/">Markdown syntax</a> to make text <strong>bold</strong> is <code>**text**</code> and <code>*text*</code> to make it <em>italic</em>.</p>
<p>Without <code>element_markdown()</code>, the asterisks are printed literally:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Flipper length** (*mm*)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Body mass** (*g*)"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Once we switch to <code>element_markdown()</code>, the formatting is applied:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Flipper length** (*mm*)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Body mass** (*g*)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb23-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>(),</span>
<span id="cb23-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>()</span>
<span id="cb23-6">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><code>element_markdown()</code> can be used for any text element in <code>theme()</code>: axis titles, axis tick labels, plot titles, captions, and so on.</p>
<p>As another example, say we wanted italic species names in the legend. First, note that if it’s OK that everything becomes italic, we can just do that with <code>theme()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italic"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>But what if we needed to make only one part of the text italic? We could do that once again with <em>ggtext</em>. First, let’s define the labels we want to use for the legend, with Markdown syntax for italics:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">species_labs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(</span>
<span id="cb25-2">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adélie (*P. adeliae*)"</span>,</span>
<span id="cb25-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap (*P. antarcticus*)"</span>, </span>
<span id="cb25-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gentoo (*P. papua*)"</span></span>
<span id="cb25-5">  )</span></code></pre></div></div>
</div>
<p>Next, we can use these labels in the legend with <code>scale_fill_brewer()</code>, and then tell <code>theme()</code> to interpret the legend text as Markdown with <code>element_markdown()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> species_labs) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.text =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>())</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="768"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-4-contents" aria-controls="callout-4" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Colored text! <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-4" class="callout-4-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>You probably won’t need this in a scientific publication, but it can be useful for other, more informal, contexts. For example, you could color species names in a plot title to match the data colors, making the legend redundant.</p>
<p>To color individual words, use an HTML <code>&lt;span&gt;</code> tag with inline CSS:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb28-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">span</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> style</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color:#1B9E77"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>some text<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">span</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div></div>
<p>Above, <code>#1B9E77</code> is the hex code for a specific color. Let’s find the three colors that <code>scale_fill_brewer(palette = "Dark2")</code> assigns to the three species:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">RColorBrewer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "#1B9E77" "#D95F02" "#7570B3"</code></pre>
</div>
</div>
<p>The species factor levels in the penguins data are alphabetical (Adelie, Chinstrap, Gentoo), so those colors are assigned in that order. Now let’s use them in the title:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;span style='color:#1B9E77'&gt;**Adelie**&lt;/span&gt;,</span></span>
<span id="cb31-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;span style='color:#D95F02'&gt;**Chinstrap**&lt;/span&gt;, and</span></span>
<span id="cb31-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;span style='color:#7570B3'&gt;**Gentoo**&lt;/span&gt; penguins"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb31-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>(),</span>
<span id="cb31-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"none"</span></span>
<span id="cb31-8">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Because the title now carries the color information, we can choose to remove the legend with <code>legend.position = "none"</code>.</p>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-text-formatting" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-text-formatting"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Text formatting</h4>
<p>It’s common to need subscripts and superscipts in axis titles. R has a special syntax for that, but it can be a bit tricky to get right. Now that we’re using <em>ggtext</em>, we can use Markdown syntax for subscripts and superscripts instead, which is:</p>
<ul>
<li><code>^...^</code> for superscripts, e.g.&nbsp;<code>3x10^-3^</code> to produce 3x10<sup>-3</sup></li>
<li><code>~...~</code> for subscripts, e.g.&nbsp;<code>H~2~O</code> to produce H<sub>2</sub>O</li>
</ul>
<p>Let’s pretend that the x-axis of our plot is showing <strong>flipper surface in mm<sup>2</sup></strong>, and that the y-axis is on a <strong>log<sub>10</sub>-scale</strong>. Modify the axis titles to reflect this, using Markdown syntax for the superscripts and subscripts.</p>
<p>(Bonus: actually change the y-axis to a log<sub>10</sub>-scale.)</p>
<details>
<summary>
<em>Click to see a solution</em>
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb32-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper surface (mm^2^)"</span>,</span>
<span id="cb32-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g on a log~10~-scale)"</span></span>
<span id="cb32-5">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb32-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb32-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>(),</span>
<span id="cb32-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>()</span>
<span id="cb32-9">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Bonus: changing the y-axis to a log<sub>10</sub>-scale – though because our y-axis shows a fairly small range and doesn’t start at zero, the log transformation doesn’t really do anything useful:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_log10</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb33-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper surface (mm^2^)"</span>,</span>
<span id="cb33-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g on a log~10~-scale)"</span></span>
<span id="cb33-6">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb33-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>(),</span>
<span id="cb33-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_markdown</span>()</span>
<span id="cb33-10">  )</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Scale for y is already present.
Adding another scale for y, which will replace the existing scale.</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-24-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="adding-images-to-plots" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="adding-images-to-plots"><span class="header-section-number">4</span> Adding images to plots</h2>
<p>Say that we wanted to add this beatiful AI-generated image of the three penguin species in the <code>penguins</code> dataset to our plot:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/3penguins.png" class="img-fluid figure-img" style="width:50.0%"></p>
<figcaption>From left to right: Adélie, Chinstrap, and Gentoo.</figcaption>
</figure>
</div>
<p>To follow along with the code below, download the file <code>3penguins.png</code> with this image from our GitHub repo:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">penguin_img_url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/osu-codeclub/osu-codeclub.github.io/main/posts/S11E06_ggplot_06/3penguins.png"</span></span>
<span id="cb35-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> penguin_img_url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3penguins.png"</span>)</span></code></pre></div></div>
</div>
<p>We’ll also load two additional packages for working with images in R:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (This is needed for the second part: adding images as a panel)</span></span>
<span id="cb36-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(grid)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Should always be installed</span></span>
<span id="cb36-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(png)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If you don't have it, install with install.packages("png")</span></span></code></pre></div></div>
</div>
<section id="an-image-inside-a-plot" class="level3" data-number="4.1">
<h3 data-number="4.1" class="anchored" data-anchor-id="an-image-inside-a-plot"><span class="header-section-number">4.1</span> An image inside a plot</h3>
<p>I think this is most easily done with the <em>cowplot</em> package, which has a <code>draw_image()</code> function to add images to ggplots. Install <em>cowplot</em> if you don’t have it already, then load it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cowplot"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cowplot)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'cowplot'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following object is masked from 'package:patchwork':

    align_plots</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following object is masked from 'package:lubridate':

    stamp</code></pre>
</div>
</div>
<p>Now, we can add the image to our plot with <code>ggdraw()</code> and <code>draw_image()</code>, where:</p>
<ul>
<li><p>The <code>x</code> and <code>y</code> arguments specify the position of the image in the plot (between 0 and 1), with x being the left-most position and y being the bottom-most position. These are not the same as the x and y axes of the plot, but rather the position of the image relative to the plot area.</p></li>
<li><p>The <code>height</code> and <code>width</code> arguments specify the size of the image relative to the plot (between 0 and 1). One thing to note is that the aspect ratio of the image is preserved, so even if you specify a square image like we do below, the actual image may not be square if the original image is not square. While it may seem odd in that case to specify both width and height, when omitting one of these, the function somehow has strange behavior with respect to the <code>x</code> and <code>y</code> coordinates. 🤷‍♂️</p></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdraw</span>(p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3penguins.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.65</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-29-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="an-example-with-multiple-images" class="level3" data-number="4.2">
<h3 data-number="4.2" class="anchored" data-anchor-id="an-example-with-multiple-images"><span class="header-section-number">4.2</span> An example with multiple images</h3>
<p>Say that we wanted to add a separate image for each penguin species in an appopriate part of the plot. First, download the following penguin images:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1">adelie_img_url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/osu-codeclub/osu-codeclub.github.io/main/posts/S11E06_ggplot_06/adelie.png"</span></span>
<span id="cb43-2">chinstrap_img_url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/osu-codeclub/osu-codeclub.github.io/main/posts/S11E06_ggplot_06/chinstrap.png"</span></span>
<span id="cb43-3">gentoo_img_url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/osu-codeclub/osu-codeclub.github.io/main/posts/S11E06_ggplot_06/gentoo.png"</span></span>
<span id="cb43-4"></span>
<span id="cb43-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> adelie_img_url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adelie.png"</span>)</span>
<span id="cb43-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> chinstrap_img_url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chinstrap.png"</span>)</span>
<span id="cb43-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> gentoo_img_url, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gentoo.png"</span>)</span></code></pre></div></div>
</div>
<p>And add them to the plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdraw</span>(p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb44-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adelie.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb44-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chinstrap.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb44-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gentoo.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-32-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-add-images-to-a-boxplot" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-add-images-to-a-boxplot"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Bonus Exercise: Add images to a boxplot</h4>
<p>Create a boxplot with penguin species along the x-axis, and any of the penguin measurements along the y-axis. Then add the three penguin images to the plot, placing each image either above the boxplot for the corresponding species, or just below the plot panel (i.e.&nbsp;in the plot margin).</p>
<details>
<summary>
<em>Click to see a solution</em>
</summary>
<p>At the top of the plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb45-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb45-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(</span>
<span id="cb45-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_comma</span>(),</span>
<span id="cb45-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manually specify the upper axis limitsand breaks to make room for the image</span></span>
<span id="cb45-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">limits =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6800</span>),</span>
<span id="cb45-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">breaks =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3000</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb45-10">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>)</span>
<span id="cb45-12"></span>
<span id="cb45-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdraw</span>(p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adelie.png"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chinstrap.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.48</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb45-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gentoo.png"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-33-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In the lower margin:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb46-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_comma</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb46-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.margin =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">margin</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">t =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">l =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># extra bottom margin</span></span>
<span id="cb46-9">    )  </span>
<span id="cb46-10"></span>
<span id="cb46-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggdraw</span>(p) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adelie.png"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chinstrap.png"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.48</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb46-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gentoo.png"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.75</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.01</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-34-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
<hr style="height:1pt; visibility:hidden;">
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-5-contents" aria-controls="callout-5" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Multiple images in a plot, with boxes <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-5" class="callout-5-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>I thought drawing colored boxes around the penguins images could be a useful example. This turned out to be more difficult than expected, so we won’t cover it during the Code Club session. But for your and my future reference, I am including the code here.</p>
<p>First off, I couldn’t get this to work in combination with the <code>draw_image()</code> approach used above to insert the images. Instead, we’ll have to switch to a <em>patchwork</em>-based approach. To insert a single image like we did above, that would look as follows:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting the image width as a variables</span></span>
<span id="cb47-2">width <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.35</span></span>
<span id="cb47-3"></span>
<span id="cb47-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read in the image</span></span>
<span id="cb47-5">raw <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readPNG</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3penguins.png"</span>)</span>
<span id="cb47-6">img <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rasterGrob</span>(raw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>))</span>
<span id="cb47-7"></span>
<span id="cb47-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Obtain the image's aspect ratio and compute the height from that:</span></span>
<span id="cb47-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is necessary because we need to specify the coordinates of all corners</span></span>
<span id="cb47-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># of the image when using inset_element(), and we want the aspect ratio to be correct.</span></span>
<span id="cb47-11">aspect_ratio <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb47-12">height <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> width <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> aspect_ratio</span>
<span id="cb47-13"></span>
<span id="cb47-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create the plot with the inserted image</span></span>
<span id="cb47-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (Setting the plot panel's aspect ratio to 1 seems necessary for the image's</span></span>
<span id="cb47-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#  aspect ratio to be correctly displayed.)</span></span>
<span id="cb47-17">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb47-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aspect.ratio =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb47-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(img, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.4</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> height)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-35-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>That works, but you can hopefully see why I choose to use <code>draw_image()</code> instead for the earlier examples. Now, let’s expand this approach in order to insert individual penguins’ images with colored boxes:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the width and save the desired box colors</span></span>
<span id="cb48-2">width <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span></span>
<span id="cb48-3">colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> RColorBrewer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brewer.pal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Adelie</span></span>
<span id="cb49-2">raw_ade <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readPNG</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"adelie.png"</span>)</span>
<span id="cb49-3">img_ade <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rasterGrob</span>(raw_ade, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>))</span>
<span id="cb49-4">aspect_ade <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw_ade)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw_ade)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb49-5">ht_ade <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> width <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> aspect_ade</span>
<span id="cb49-6">box_ade <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb49-9"></span>
<span id="cb49-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Chinstrap</span></span>
<span id="cb49-11">raw_chi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readPNG</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chinstrap.png"</span>)</span>
<span id="cb49-12">img_chi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rasterGrob</span>(raw_chi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>))</span>
<span id="cb49-13">aspect_chi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw_chi)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw_chi)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb49-14">ht_chi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> width <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> aspect_chi</span>
<span id="cb49-15">box_chi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb49-18"></span>
<span id="cb49-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Gentoo</span></span>
<span id="cb49-20">raw_gen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readPNG</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gentoo.png"</span>)</span>
<span id="cb49-21">img_gen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rasterGrob</span>(raw_gen, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">height =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unit</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"npc"</span>))</span>
<span id="cb49-22">aspect_gen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw_gen)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(raw_gen)[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb49-23">ht_gen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> width <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> aspect_gen</span>
<span id="cb49-24">box_gen <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.border =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> colors[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb49-27"></span>
<span id="cb49-28">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aspect.ratio =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(img_ade, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ht_ade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(box_ade, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ht_ade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(img_chi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ht_chi) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(box_chi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ht_chi) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(img_gen, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ht_gen) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inset_element</span>(box_gen, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">right =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">left =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> width, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">top =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bottom =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> ht_gen)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-37-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="an-image-in-a-multi-panel-layout" class="level3" data-number="4.3">
<h3 data-number="4.3" class="anchored" data-anchor-id="an-image-in-a-multi-panel-layout"><span class="header-section-number">4.3</span> An image in a multi-panel layout</h3>
<p>Now, let’s explore adding an image to a multi-panel layout, so basically treating the image as just another plot in the layout.</p>
<p>For that, we’ll start with the same three-panel layout as in the previous session:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1">p_scatter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb50-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb50-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb50-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb50-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species))</span>
<span id="cb50-6">p_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb50-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb50-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>()</span>
<span id="cb50-9">p_box <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb50-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb50-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb50-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span>
<span id="cb50-13"></span>
<span id="cb50-14">p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_box)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-38-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>As a first step in including the penguin image, we’ll read it in as a graphical object with <code>readPNG()</code> and <code>rasterGrob()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1">img <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rasterGrob</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readPNG</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3penguins.png"</span>))</span></code></pre></div></div>
</div>
<p>Next, we can add the image to the layout with <em>patchwork</em>’s <code>wrap_elements()</code> function:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Store the image as a plot element with wrap_elements():</span></span>
<span id="cb52-2">p_img <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_elements</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel =</span> img)</span>
<span id="cb52-3"></span>
<span id="cb52-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add the image to the layout with patchwork syntax:</span></span>
<span id="cb52-5">(p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_img) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_box)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-40-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Above, the plot with the image is quite small, and this is because the image is being resized to fit the plot <em>panel</em> (that is, the area inside the axes of a plot). Note that it’s taking up the entire plot panel height, but because the image has a 1:1 aspect ratio, it is not taking up the entire plot panel width. While that makes it appear even smaller, it’s a good thing that the image’s aspect ratio is respected, or the image would look stretched.</p>
<p>Alternatively, we can tell <code>wrap_elements()</code> to use the entire <em>plot</em> area for the image with <code>plot =</code> (above, we used <code>panel =</code>):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1">p_img_big <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">wrap_elements</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot =</span> img)</span>
<span id="cb53-2">(p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_img_big) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_box)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-41-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-6-contents" aria-controls="callout-6" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>A different method with different image-fitting behavior <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-6" class="callout-6-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>A final alternative is to read the image in with the <em>magick</em> package. That’s an easy way to fix the aspect ratio of the image, such that patchwork will automatically take that aspect ratio into account when allocating space for each plot in the layout.</p>
<p>First, install and then load the package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"magick"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(magick)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Linking to ImageMagick 6.9.12.93
Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
Disabled features: fftw, ghostscript, x11</code></pre>
</div>
</div>
<p>Now use <em>magick</em>’s <code>image_read()</code> to read the image in, and <code>image_ggplot()</code> to convert it to a ggplot object:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1">p_img_fixed <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">image_read</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3penguins.png"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
ℹ Please use tidy evaluation idioms with `aes()`.
ℹ See also `vignette("ggplot2-in-packages")` for more information.
ℹ The deprecated feature was likely used in the magick package.
  Please report the issue at &lt;https://github.com/ropensci/magick/issues&gt;.</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb59" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb59-1">(p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_img_fixed) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> p_box)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/index_files/figure-html/unnamed-chunk-44-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>


</section>
</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p> You can change this with the <code>units</code> argument, other options are: (<code>"cm"</code>, <code>"mm"</code>, or <code>"px"</code>).↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>plotting</category>
  <category>tidyverse</category>
  <guid>https://osu-codeclub.github.io/posts/S11E06_ggplot_06/</guid>
  <pubDate>Mon, 09 Mar 2026 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E06_ggplot_06/featured_plot.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>Preparing (gg)plots for publication</title>
  <dc:creator>Jelmer Poelstra</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E05_ggplot_05/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<section id="what-well-cover" class="level3" data-number="1.1">
<h3 data-number="1.1" class="anchored" data-anchor-id="what-well-cover"><span class="header-section-number">1.1</span> What we’ll cover</h3>
<p>Today, we’ll cover two topics to help you prepare your plots for publication. Specifically, we’ll have figures for scientific publications in mind, and will cover:</p>
<ul>
<li><strong>Formatting plots</strong> – theme presets and customizing plot appearance with <em>ggplot</em>’s <code>theme()</code></li>
<li><strong>Multi-panel layouts</strong> – with the <em>patchwork</em> package</li>
<li><strong>Saving plots to file</strong> – with <em>ggplot</em>’s <code>ggsave()</code></li>
</ul>
</section>
<section id="setup" class="level3" data-number="1.2">
<h3 data-number="1.2" class="anchored" data-anchor-id="setup"><span class="header-section-number">1.2</span> Setup</h3>
<section id="loading-packages" class="level4">
<h4 class="anchored" data-anchor-id="loading-packages">Loading packages</h4>
<p>Like in the previous ggplot2 sessions, we’ll load two packages: the <em>tidyverse</em>, which includes <em>ggplot2</em>, and <em>palmerpenguins</em>, which contains the penguins dataset we’ve been working with:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'

The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
</div>
<p>We’ll need an additional packages later on, but we’ll install and load that one along the way.</p>
</section>
<section id="a-base-plot" class="level4">
<h4 class="anchored" data-anchor-id="a-base-plot">A base plot</h4>
<p>We’ll use a scatterplot of the <code>penguins</code> dataset as our running example throughout – assigning the plot to object <code>p</code>, so we can keep layering onto it without retyping everything:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get rid of rows with NAs</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dark2"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_continuous</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</span> scales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">label_comma</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>)</span>
<span id="cb5-8"></span>
<span id="cb5-9">p</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>A couple of notes about the plot above:</p>
<ul>
<li><p>I did not give the plot a title, because these are not typically present in figures in scientific papers</p></li>
<li><p>I used <code>shape = 21</code> to get points with a fill color and black border, which I like for scatterplots</p></li>
<li><p>A trick we didn’t covered in the last few weeks is <code>scales::label_comma()</code> to add thousand-separator commas for readability. Very similar <code>scales::</code> functions are available for formatting axes in other ways, e.g.&nbsp;<code>label_percent()</code> for percentages, <code>label_dollar()</code> for prices, etc.</p></li>
</ul>
<hr style="height:1pt; visibility:hidden;">
</section>
</section>
</section>
<section id="formatting-plots" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="formatting-plots"><span class="header-section-number">2</span> Formatting plots</h2>
<p>The default ggplot2 look is fine for quick exploration, but can be improved on for publication. For example, we likely don’t want a gray background and may need larger text. There are two main tools for formatting your plots:</p>
<ol type="1">
<li>Theme presets — functions like <code>theme_bw()</code> that swap out the whole look at once</li>
<li><strong><code>theme()</code></strong> — fine-grained control over individual elements</li>
</ol>
<hr style="height:1pt; visibility:hidden;">
<section id="theme-preset-options" class="level3" data-number="2.1">
<h3 data-number="2.1" class="anchored" data-anchor-id="theme-preset-options"><span class="header-section-number">2.1</span> Theme preset options</h3>
<p><em>ggplot2</em> ships with several built-in theme functions. Here are some of the most useful alternatives to the default (which is called <code>theme_gray()</code>):</p>
<table class="table-striped caption-top table">
<thead>
<tr class="header">
<th>Function</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>theme_bw()</code></td>
<td>White background, grey gridlines, black border</td>
</tr>
<tr class="even">
<td><code>theme_classic()</code></td>
<td>White background, no gridlines, axis lines only</td>
</tr>
<tr class="odd">
<td><code>theme_minimal()</code></td>
<td>White background, minimal gridlines, no border</td>
</tr>
<tr class="even">
<td><code>theme_void()</code></td>
<td>Completely empty — good for maps or diagrams</td>
</tr>
</tbody>
</table>
<hr style="height:1pt; visibility:hidden;">
<p>Let’s compare a few:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="scaling-all-text-sizes-with-base_size" class="level3" data-number="2.2">
<h3 data-number="2.2" class="anchored" data-anchor-id="scaling-all-text-sizes-with-base_size"><span class="header-section-number">2.2</span> Scaling all text sizes with <code>base_size()</code></h3>
<p>All theme preset functions accept a <code>base_size</code> argument that scales the size of text and points in the plot – the larger the value, the larger the text (the default value is <code>11</code>):</p>
<div class="columns">
<div class="column" style="width:48%;">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</div><div class="column" style="width:4%;">

</div><div class="column" style="width:48%;">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
<p>Note that this doesn’t mean all text will have the same size: instead, it is a scaling factor. So, axis titles will still remain similarly larger than axis text at tick labels, regardless of the <code>base_size()</code>.</p>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="customizing-with-theme" class="level3" data-number="2.3">
<h3 data-number="2.3" class="anchored" data-anchor-id="customizing-with-theme"><span class="header-section-number">2.3</span> Customizing with <code>theme()</code></h3>
<p>Preset themes get you most of the way there, but <code>theme()</code> lets you adjust individual elements. You layer it on top of a preset:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(...)</span></code></pre></div></div>
<div class="callout callout-style-simple callout-warning">
<div class="callout-body d-flex">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-body-container">
<p>If you use both a <code>theme_</code> preset and <code>theme()</code>, the <code>theme()</code> call <strong>must</strong> go <em>after</em> the preset, otherwise the preset will override your customizations.</p>
</div>
</div>
</div>
<p>A first example of a <code>theme()</code> call – to make the legend title bold:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>))</span></code></pre></div></div>
<p>The <code>theme()</code> function has dozens of arguments to adjust elements such as <code>legend.title</code>, <code>axis.text</code>, <code>panel.background</code>, and so on. What makes its usage perhaps a bit overwhelming at first is that you will also need to know the type of element you want to adjust, which is specified with <code>element_*()</code> functions:</p>
<table class="table-striped caption-top table">
<colgroup>
<col style="width: 28%">
<col style="width: 71%">
</colgroup>
<thead>
<tr class="header">
<th>Element type</th>
<th>What it controls</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>element_text()</code></td>
<td>Applies to text and adjusts font size, face, color, angle, alignment</td>
</tr>
<tr class="even">
<td><code>element_line()</code></td>
<td>Applies to lines and adjusts Line color, size, linetype</td>
</tr>
<tr class="odd">
<td><code>element_rect()</code></td>
<td>Applies to rectangles (panels, legend box, etc.) and adjusts their fill and border</td>
</tr>
<tr class="even">
<td><code>element_blank()</code></td>
<td>This is used to removes an element entirely</td>
</tr>
</tbody>
</table>
<p>Below, we’ll see some examples of several of these.</p>
<section id="text-size-and-style" class="level4">
<h4 class="anchored" data-anchor-id="text-size-and-style">Text size and style</h4>
<p>You may want to emphasize the axis titles and legend title by making them bold:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb13-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">14</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold.italic"</span>),</span>
<span id="cb13-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>)</span>
<span id="cb13-6">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>Text options
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><code>face</code> accepts <code>"plain"</code>, <code>"bold"</code>, <code>"italic"</code>, or <code>"bold.italic"</code></li>
<li><code>axis.title</code> controls both axes; use <code>axis.title.x</code> or <code>axis.title.y</code> to target one</li>
</ul>
</div>
</div>
</section>
<section id="legend-position" class="level4">
<h4 class="anchored" data-anchor-id="legend-position">Legend position</h4>
<p>By default, the legend is to the right of the plot. That’s fine in many cases, but if you for example have a wide plot, you may want to place it at the top:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"top"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-2-contents" aria-controls="callout-2" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Want to place the legend <em>inside</em> the plot? <em>(Click to expand)</em>
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-2" class="callout-2-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<p>You can also place the legend <em>inside</em> the plot by passing coordinates as a fraction of the plot area (0 = left/bottom, 1 = right/top):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb15-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.85</span>),  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># upper-left corner</span></span>
<span id="cb15-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.background =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_rect</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>)</span>
<span id="cb15-6">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="gridlines" class="level4">
<h4 class="anchored" data-anchor-id="gridlines">Gridlines</h4>
<p>I like <code>theme_bw()</code> because of the box it draws around the plot, but think the gridlines are a bit excessive and distracting. You can remove gridlines selectively rather than switching themes wholesale:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb16-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.minor =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb16-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb16-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"longdash"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>)</span>
<span id="cb16-7">  )</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Above:</p>
<ul>
<li>We turned off all “minor” gridlines (which are in between axis tickmarks),</li>
<li>Turned off “major” gridlines (which are at the tickmarks) for the x-axis</li>
<li>For the sake of showing <code>element_line()</code> in action, changed the formatting major gridlines on the y-axis</li>
</ul>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="setting-a-default-theme-for-the-whole-session" class="level4">
<h4 class="anchored" data-anchor-id="setting-a-default-theme-for-the-whole-session">Setting a default theme for the whole session</h4>
<p>If you make a bunch of figures with a single script, and want a consistent look across all of them, it can be tedious to add the same <code>theme_*()</code> call to every plot.</p>
<p>Instead, at the top of the script, you can set a default with <code>theme_set()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_set</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>))</span></code></pre></div></div>
</div>
<p>After this, every new plot will use <code>theme_classic()</code> automatically. You can further customize with <code>theme_update()</code> to set specific elements globally:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_update</span>(</span>
<span id="cb18-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.grid.major.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey70"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>)</span>
<span id="cb18-3">)</span></code></pre></div></div>
</div>
<p>Let’s see this in action: make sure you have run the code above, and our next few plots should have the look defined above.</p>
<hr style="height:1pt; visibility:hidden;">
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-format-according-to-your-preferences" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-format-according-to-your-preferences"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Format according to your preferences</h4>
<p>You may have your own ideas about how you’d like to format your plots, and that is fine! There is usually room for a personal touch, even in scientific publications.</p>
<p>Play around with scatterplot <code>p</code>, adding a <code>theme_</code> preset and several <code>theme()</code> options, to create a plot styled the way you like.</p>
<p>If you are wondering how to change elements in ways we haven’t covered, feel free to ask us or the internet/genAI.</p>
</section>
<hr style="height:1pt; visibility:hidden;">
</section>
</section>
<section id="multi-panel-figures" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="multi-panel-figures"><span class="header-section-number">3</span> Multi-panel figures</h2>
<p>The <a href="https://patchwork.data-imaginist.com/">patchwork</a> package allows you to combine multiple plots into a single multi-panel figure. This is something you might be used to doing with programs like Powerpoint or Illustrator. However, if you’re already making all the individual plots that should make up a figure in R, it is beneficial to also use R to combine them.</p>
<p>That way, you can easily rerun your code to recreate plots with some modifications. Otherwise, after any change, you’d have to put the plots back together in another program.</p>
<p>First, let’s install and load the package:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"patchwork"</span>)</span></code></pre></div></div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(patchwork)</span></code></pre></div></div>
</div>
<p>Patchwork assumes that you have created and saved the individual plots as separate R objects. Let’s start by making three plots with the <code>penguins</code> dataset that we’ll combine later on:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">p_scatter <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb21-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species))</span>
<span id="cb21-6"></span>
<span id="cb21-7">p_scatter</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">p_bar <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>()</span>
<span id="cb22-4"></span>
<span id="cb22-5">p_bar</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">p_box <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span>
<span id="cb23-5"></span>
<span id="cb23-6">p_box</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>To tell <em>patchwork</em> how to arrange these plots, the syntax is based on common mathematical operators. For example:</p>
<ul>
<li><code>plot1 | plot2</code> puts two plots side-by-side</li>
<li><code>plot1 / plot2</code> stacks two plots vertically</li>
<li><code>(plot1 | plot2) / plot3)</code> puts <code>plot1</code> and <code>plot2</code> on the top row, and <code>plot3</code> on the bottom row</li>
</ul>
<p>For example, to combine the three plots we just made, with the first (faceted) plot on top, and the other two side-by-side below it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_box)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Patchwork has quite a lot more functionality, and this is well-explained in various vignettes/tutorials on <a href="https://patchwork.data-imaginist.com/articles/patchwork.html">its website</a>.</p>
<p>Here, we’ll just try one more feature, adding <strong>tags</strong> for the individual plots — where we tell patchwork about the type of numbering we would like (e.g.&nbsp;A-B-C vs.&nbsp;1-2-3) by specifying the first character:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">p_combined <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p_scatter <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (p_bar <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> p_box) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tag_levels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>)</span>
<span id="cb25-3"></span>
<span id="cb25-4">p_combined</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-format-the-above-figure" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-format-the-above-figure"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise: Format the above figure</h4>
<ol type="1">
<li><p>Put the faceted plot (currently A) on the bottom instead of the top row</p></li>
<li><p>Improve the axis labels</p></li>
<li><p>Improve/modify the formatting of the individual plots in other ways you see fit</p></li>
</ol>
</section>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-create-your-own-multi-panel-figure" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-bonus-exercise-create-your-own-multi-panel-figure"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Bonus Exercise: Create your own multi-panel figure</h4>
<p>Try to recreate this figure:</p>
<div class="cell">
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<details>
<summary>
<em>Click here to see the the solution</em>
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">p_bill_flipper <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill Length (mm)"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length (mm)"</span>)</span>
<span id="cb26-7">  </span>
<span id="cb26-8">p_mass_yr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb26-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb26-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body Mass (g)"</span>)</span>
<span id="cb26-12"></span>
<span id="cb26-13">p_bill_flipper <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> p_mass_yr <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb26-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot_annotation</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tag_levels =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'I'</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>plotting</category>
  <category>tidyverse</category>
  <guid>https://osu-codeclub.github.io/posts/S11E05_ggplot_05/</guid>
  <pubDate>Mon, 02 Mar 2026 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E05_ggplot_05/featured_plot.png" medium="image" type="image/png" height="103" width="144"/>
</item>
<item>
  <title>Introduction to ggplot2 - 03 &amp; 04</title>
  <dc:creator>Horacio Lopez-Nicora and Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E03_ggplot_03/</link>
  <description><![CDATA[ 




<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/img/ggplot2_exploratory.png" class="img-fluid figure-img" style="width:70.0%" alt="A group of fuzzy round monsters with binoculars, backpacks and guide books looking up a graphs flying around with wings (like birders, but with exploratory data visualizations). Stylized text reads “ggplot2: visual data exploration.”"></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p>We have spent the last two Code Club sessions talking about plotting with <a href="https://ggplot2.tidyverse.org/"><code>ggplot2</code></a>. We have gone through:</p>
<ul>
<li><a href="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/">Code structure, data, aesthetic mappings, and some geoms</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/">Changing colors ad checking accessibility</a></li>
</ul>
<p>This week, we will go over:</p>
<ul>
<li>Additional geoms</li>
<li>Creating a faceted plot</li>
<li>Adding labels</li>
</ul>
<section id="loading-packages-and-data" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="loading-packages-and-data"><span class="header-section-number">1.1</span> Loading packages and data</h2>
<p>The first thing we will do is load the <code>tidyverse</code> so we can use the functions it contains.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
</div>
<p>We are going to continue to use the <code>penguins</code> dataset we’ve been working with for the last couple of weeks. Let’s load that data so we can use it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
</div>
<p>We can also take a “glimpse” at our data with the function <code>glimpse()</code> to remind ourselves of what data is contained within <code>penguins</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(penguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species           &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               &lt;fct&gt; male, female, female, NA, female, male, female, male…
$ year              &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
</section>
</section>
<section id="more-geoms" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> More geoms</h1>
<p>So far we’ve focused really on scatterplots (using <code>geom_point()</code>), so let’s try some other plot types.</p>
<p>What if we want to see what the body mass of penguins looks like on different islands? We can do that using a variety of different geoms.</p>
<section id="bar-plot-be-wary" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="bar-plot-be-wary"><span class="header-section-number">2.1</span> Bar plot (be wary!)</h2>
<p>A bar plot is a very commonly used plot type, but often can hide the true distribution of your data, so be careful when you use it.</p>
<section id="means" class="level3" data-number="2.1.1">
<h3 data-number="2.1.1" class="anchored" data-anchor-id="means"><span class="header-section-number">2.1.1</span> Means</h3>
<p>If we want to plot what the average body mass is per island, we need to either:</p>
<ul>
<li>calculate a mean, and then plot it</li>
<li>use a function that calculates the mean for us</li>
</ul>
<p>First let’s calculate the mean manually. This way we can know what those averages should be, and we can reference this against our plot later.</p>
<p>We can go back to some <a href="https://osu-codeclub.github.io/posts/S08E03_wrangling_03/#the-summarize-function">content we’ve gone over in Code Club in the past</a> about the function <a href="https://dplyr.tidyverse.org/reference/summarise.html"><code>summarize()</code></a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># calculating the mean body mass for each island</span></span>
<span id="cb8-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, </span>
<span id="cb8-4">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb8-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> island) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summarize by island</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 2
  island    mean_body_mass
  &lt;fct&gt;              &lt;dbl&gt;
1 Torgersen          3706.
2 Biscoe             4716.
3 Dream              3713.</code></pre>
</div>
</div>
<p>Now we can see what the average mass should be for each island. We can take that same code and pipe that into <code>ggplot()</code> to use that as the data to plot.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># then plotting</span></span>
<span id="cb10-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, </span>
<span id="cb10-4">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb10-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summarize by island</span></span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean_body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Note that the variable we are plotting to <code>y</code> is <code>mean_body_mass</code> - a new variable we calculated using <code>summarize()</code>.</p>
<p>See how this is different from passing. your data directly to <code>geom_col()</code>? Pay careful attention to the y-axis values.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this is wrong</span></span>
<span id="cb11-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># do not do this</span></span>
<span id="cb11-3">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_col()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>If you don’t want to bother with the pre-calculation, you can use the function <a href="https://ggplot2.tidyverse.org/reference/stat_summary.html"><code>stat_summary()</code></a> which will pre-calculate the mean for you, and plot via the geom of your choice.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># geom should be column</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>No summary function supplied, defaulting to `mean_se()`</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_summary()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>It is a good idea to do these calculations yourself first, even if you end up using a function like <code>stat_summary()</code>, this way you can be sure that the result you are getting makes sense.</p>
</section>
<section id="error-bars" class="level3" data-number="2.1.2">
<h3 data-number="2.1.2" class="anchored" data-anchor-id="error-bars"><span class="header-section-number">2.1.2</span> Error bars</h3>
<p>If we want to add error bars, we can do that too, and in the same way. We can either calculate the values we want to map to the error bars, and then add them, or use another <code>stat_summary()</code> to have <code>ggplot2</code> calculate the values for us. I will show you code for both, but only go through doing this manually (as the other way is a little bit more complicated than I want to get into right now).how to do both.</p>
<p>Just like we used <code>summarize()</code> to calculate the means, we can also use similar code to calculate a standard deviation. Let’s grab that code, and we can add to it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, </span>
<span id="cb16-3">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb16-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(body_mass_g,</span>
<span id="cb16-5">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb16-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> island) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summarize by island</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 3
  island    mean_body_mass sd_body_mass
  &lt;fct&gt;              &lt;dbl&gt;        &lt;dbl&gt;
1 Torgersen          3706.         445.
2 Biscoe             4716.         783.
3 Dream              3713.         417.</code></pre>
</div>
</div>
<p>Now we see both a standard deviation along with our mass. And we can plot this now.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass_g, </span>
<span id="cb18-3">                                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb18-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd_body_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(body_mass_g,</span>
<span id="cb18-5">                              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb18-6">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summarize by island</span></span>
<span id="cb18-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> mean_body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_col</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb18-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_errorbar</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymin =</span> (mean_body_mass <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> sd_body_mass), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set min</span></span>
<span id="cb18-10">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ymax =</span> (mean_body_mass <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> sd_body_mass)), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set max</span></span>
<span id="cb18-11">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># set width of the bars</span></span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>You can have <code>ggplot()</code> make your error bars for you but it’s a little more complicated than what I want to get into. You can read more about it below.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center collapsed" data-bs-toggle="collapse" data-bs-target=".callout-1-contents" aria-controls="callout-1" aria-expanded="false" aria-label="Toggle callout">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Or, we could have <code>ggplot()</code> calculate and make our error bars for us. We can start with our code from our original bar plot, and add to it.
</div>
<div class="callout-btn-toggle d-inline-block border-0 py-1 ps-1 pe-0 float-end"><i class="callout-toggle"></i></div>
</div>
<div id="callout-1" class="callout-1-contents callout-collapse collapse">
<div class="callout-body-container callout-body">
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># makes the bars</span></span>
<span id="cb19-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stat_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun.data =</span> mean_sdl, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># function is mean_sd</span></span>
<span id="cb19-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fun.args =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mult=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># more arguments arguments to mean_sdl as a list</span></span>
<span id="cb19-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">geom =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"errorbar"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>No summary function supplied, defaulting to `mean_se()`</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_summary()`).
Removed 2 rows containing non-finite outside the scale range
(`stat_summary()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Computation failed in `stat_summary()`.
Caused by error in `fun.data()`:
! The package "Hmisc" is required.</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section id="counting" class="level3" data-number="2.1.3">
<h3 data-number="2.1.3" class="anchored" data-anchor-id="counting"><span class="header-section-number">2.1.3</span> Counting</h3>
<p>We could also use bar plots to “count” instead of to display a mean. For this, we use <a href="https://ggplot2.tidyverse.org/reference/geom_bar.html"><code>geom_bar()</code></a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="boxplot" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="boxplot"><span class="header-section-number">2.2</span> Boxplot</h2>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/img/summary_statistics.png" class="img-fluid figure-img" style="width:70.0%" alt="A cartoon showing a clip art style bar graph, on the left where all the data point dots are held at the bottom by a net, and one on the right where the fidelity of the datapoints are shown. The center says 'are your summary statistics hiding something interesting?'"></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations/blob/main/other-stats-artwork/summary_statistics.png">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p>There is rarely a situation where a bar plot wouldn’t be improved by turning it into a boxplot. A boxplot allows us to see the distribution of our data (instead of just the mean). Lots can be hiding when you summarize before plotting!</p>
<p>We can make a boxplot simply by using <a href="https://ggplot2.tidyverse.org/reference/geom_boxplot.html"><code>geom_boxplot()</code></a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can supplement our boxplot with all our actual data too, as that might help us see trends in our data. This is a special type of <code>geom_point()</code> called <code>geom_jitter()</code>, which takes your data points and puts them, in this case, on top of your boxplot.</p>
<p>Here I will color the points by <code>sex</code> so we can see what that effect is.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_jitter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="practice" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="practice"><span class="header-section-number">2.3</span> Practice</h2>
<p>We are going to try looking at some new geoms. Create a plot that shows you the distribution of value for flipper length across all the penguins.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Take a look at the <a href="https://rstudio.github.io/cheatsheets/data-visualization.pdf"><code>ggplot</code> cheatsheet</a> and pick a geom that you think would be good for this data that is one continuous variable.</p>
</details>
<details>
<summary>
Need another hint?
</summary>
<p>Try <a href="https://ggplot2.tidyverse.org/reference/geom_density.html"><code>geom_density()</code></a> or <a href="https://ggplot2.tidyverse.org/reference/geom_histogram.html"><code>geom_histogram()</code></a>. Keep in mind that when you have one continuous variable, count gets mapped to <code>y</code> and this happens automatically.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<p>A density plot:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb29-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb29-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_density()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>A histogram, you can play around with <code>bins =</code> and <code>binwidth =</code> to make the plot look how you want:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb31-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb31-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_histogram</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`stat_bin()` using `bins = 30`. Pick better value `binwidth`.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
</section>
<section id="facets" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Facets</h1>
<p>Faceting allows to create small multiples of plots, enabling the easy comparison across the entirety of your data. A benefit of plots like this is they are all structured the same way, so once you understand one, you can begin to look at trends across groups/treatments/conditions simply and easily.</p>
<p>What if we want to see the distribution of body mass across penguins, by both islands and sex? Facets can help with this.</p>
<p>There are two faceting functions:</p>
<ul>
<li><a href="https://ggplot2.tidyverse.org/reference/facet_wrap.html"><code>facet_wrap</code></a>: allows to lay out your facets in a wrapped type. You can use <code>facet_wrap</code> if you have 1 variable you’d like to facet on.</li>
<li><a href="https://ggplot2.tidyverse.org/reference/facet_grid.html"><code>facet_grid</code></a>: allows you to lay out your facets in a grid. You can use <code>facet_grid</code> if you have 1 or 2 variables you’d like to facet on.</li>
</ul>
<p>Let’s try faceting by <code>species</code> first.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb34-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb34-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We can also facet by both <code>species</code> and <code>sex</code> - let’s try that.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb36-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(island, body_mass_g, species, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb36-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(sex), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>If we prefer to be able to compare by <code>sex</code> more easily, we can switch the order of rows and cols.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb37-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(island, body_mass_g, species, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb37-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(sex))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The default in both <code>facet_wrap</code> and <code>facet_grid</code> are for the x and y-axis to be fixed and constant among all the plots. This is often what you want to take advance of the comparisons between small multiples, but this is something you can change if you want. You can adjust the scales within facets to:</p>
<ul>
<li><code>scales = "fixed"</code>: both the x- and y-axes are fixed for all plots to be the same (this is the default)</li>
<li><code>scales = "free"</code>: both the x- and y-axes are set per plot</li>
<li><code>scales = "free_x"</code>: the x-axis scales are free between plots</li>
<li><code>scales = "free_y"</code>: the y-axis scales are free between plots</li>
</ul>
<section id="practice-1" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="practice-1"><span class="header-section-number">3.1</span> Practice</h2>
<p>Create a faceted plot for different penguins species that shows the relationship between flipper length and bill length. Color your points by sex. Remove any missing values.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Try using the variables <code>flipper_length</code>, <code>bill_length_mm</code>, <code>species</code> and <code>sex</code>. Try faceting by <code>species</code> and coloring by <code>sex</code>. You can drop missing values with the function <code>drop_na()</code>.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb38-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb38-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb38-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb38-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
<p>Take your faceted plot and add a linear line showing the relationship flipper length and bill length for each species, by sex.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Add another layer with <code>geom_smooth()</code>. If you want a linear line, you can set <code>method = "lm"</code>.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb39-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb39-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb39-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb39-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
</section>
<section id="labels" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Labels</h1>
<p>Having good labels helps your reader (and you, when you come back to the plot in the future) understand what its all about.</p>
<p>In the <a href="https://ggplot2.tidyverse.org/reference/labs.html"><code>labs()</code></a> function, you can indicate:</p>
<ul>
<li><code>x</code> for the x-axis label</li>
<li><code>y</code> for the y-axis label</li>
<li><code>title</code> for a title</li>
<li><code>subtitle</code> for a subtitle underneath your title</li>
<li><code>caption</code> for a caption</li>
</ul>
<p>Let’s take a plot we worked on and adjust the labels.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb41-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(island, body_mass_g, species, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove missing values</span></span>
<span id="cb41-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_grid</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rows =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(sex), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Island"</span>,</span>
<span id="cb41-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body mass (g)"</span>,</span>
<span id="cb41-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Penguin body mass by island and species"</span>,</span>
<span id="cb41-9">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data collected from LTER, Antarctica in 2007/08"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="practice-2" class="level2" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="practice-2"><span class="header-section-number">4.1</span> Practice</h2>
<p>Take the final plot from the last series of exercises and add some more descriptive labels for x, y, and a title.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Use <code>labs()</code> and set <code>x</code>, <code>y</code>, and <code>title</code>. You can also add others.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb42-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb42-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb42-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vars</span>(species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper length (mm)"</span>,</span>
<span id="cb42-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bill length (mm)"</span>,</span>
<span id="cb42-9">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># remove the legend title, could also rename this way</span></span>
<span id="cb42-10">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Relationship between penguin flipper and bill length"</span>,</span>
<span id="cb42-11">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data collected from LTER, Antarctica"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>plotting</category>
  <category>tidyverse</category>
  <guid>https://osu-codeclub.github.io/posts/S11E03_ggplot_03/</guid>
  <pubDate>Mon, 16 Feb 2026 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E03_ggplot_03/img/ggplot2_exploratory.png" medium="image" type="image/png" height="133" width="144"/>
</item>
<item>
  <title>Introduction to ggplot2 - 02</title>
  <dc:creator>Horacio Lopez-Nicora</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E02_ggplot_02/</link>
  <description><![CDATA[ 




<p><br></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/img/penguins.png" class="img-fluid figure-img" style="width:80.0%"></p>
<figcaption>Artwork by <a href="https://twitter.com/allison_horst">Allison Horst</a>.</figcaption>
</figure>
</div>
<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<section id="recap-of-the-past-session" class="level4">
<h4 class="anchored" data-anchor-id="recap-of-the-past-session">Recap of the past session</h4>
<p>Last week’s session marked the beginning of our data visualization journey with <code>ggplot2</code>. We explored the philosophy of coding graphics, created a versatile ggplot template for various charts, and discovered how to add visual elements using aesthetics and layers. Exciting times ahead!</p>
</section>
<section id="session-goals" class="level4">
<h4 class="anchored" data-anchor-id="session-goals">Session Goals</h4>
<ul>
<li>Let’s pick up on <strong>aesthetics</strong> and learn some more about it. Furthermore, let’s introduce the <a href="https://ggplot2.tidyverse.org/reference/ggtheme.html"><code>theme()</code></a> function.</li>
<li>Learn the basic of <strong>theme</strong>, <strong>scale</strong>, <strong>color</strong>, <strong>transparency</strong>, <strong>size</strong>, and <strong>more!</strong></li>
</ul>
<p><br></p>
</section>
</section>
<section id="our-data-set" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="our-data-set"><span class="header-section-number">2</span> Our data set</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/img/palmerpenguins_hex.png" class="img-fluid figure-img" style="width:50.0%"></p>
<figcaption>Illustration by <a href="https://allisonhorst.github.io/palmerpenguins/articles/art.html">Allison Horst</a></figcaption>
</figure>
</div>
<p>We are going to continue using our 🐧 data set from the package <a href="https://allisonhorst.github.io/palmerpenguins/"><code>palmerpenguins</code></a>. If you haven’t done so, please install that package first:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"palmerpenguins"</span>)</span></code></pre></div></div>
</div>
<p><a href="https://allisonhorst.github.io/palmerpenguins/index.html"><code>palmerpenguins</code></a> is a package developed by Allison Horst, Alison Hill and Kristen Gorman, including a data set collected by Dr.&nbsp;Kristen Gorman at the Palmer Station Antarctica, as part of the Long Term Ecological Research Network. It is a nice, relatively simple data set to practice data exploration and visualization in R.</p>
<p>We’ll now load the package, along with the tidyverse (which includes ggplot2):</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
</div>
<p>Once you’ve loaded that package you will have a data frame called <code>penguins</code> at your disposal — let’s take a look:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">penguins</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 344 × 8
   species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   &lt;fct&gt;   &lt;fct&gt;              &lt;dbl&gt;         &lt;dbl&gt;             &lt;int&gt;       &lt;int&gt;
 1 Adelie  Torgersen           39.1          18.7               181        3750
 2 Adelie  Torgersen           39.5          17.4               186        3800
 3 Adelie  Torgersen           40.3          18                 195        3250
 4 Adelie  Torgersen           NA            NA                  NA          NA
 5 Adelie  Torgersen           36.7          19.3               193        3450
 6 Adelie  Torgersen           39.3          20.6               190        3650
 7 Adelie  Torgersen           38.9          17.8               181        3625
 8 Adelie  Torgersen           39.2          19.6               195        4675
 9 Adelie  Torgersen           34.1          18.1               193        3475
10 Adelie  Torgersen           42            20.2               190        4250
# ℹ 334 more rows
# ℹ 2 more variables: sex &lt;fct&gt;, year &lt;int&gt;</code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Or glimpse() for a sort of transposed view, so we can see all columns:</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(penguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species           &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               &lt;fct&gt; male, female, female, NA, female, male, female, male…
$ year              &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
<p><br></p>
</section>
<section id="the-absolute-power-of-aesthetics" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="the-absolute-power-of-aesthetics"><span class="header-section-number">3</span> The Absolute Power of Aesthetics</h2>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="http://r.qcbs.ca/workshop03/book-en/aesthetics.html"><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/img/4plot_aesthetic.png" class="img-fluid figure-img"></a></p>
<figcaption>Aesthetics enable us to showcase multiple dimensions of our dataset in a single plot by modifying elements such as color, shape, size, labels, and transparency.</figcaption>
</figure>
</div>
<hr style="height:1pt; visibility:hidden;">
<section id="last-weeks-example-on-the-power-of-aesthetics" class="level3" data-number="3.1">
<h3 data-number="3.1" class="anchored" data-anchor-id="last-weeks-example-on-the-power-of-aesthetics"><span class="header-section-number">3.1</span> Last week’s example on “The power of aesthetics”</h3>
<p>Last week we <strong>added a third aesthetic</strong> to our graph, <code>color</code>. Our current plot mapped <code>bill_length_mm</code> to the <code>x</code> aesthetic, and <code>bill_depth_mm</code> to the <code>y</code> aesthetic — , we then added a mapping of <code>species</code> to the <code>color</code> aesthetic:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm,</span>
<span id="cb12-3">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb12-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species))</span>
<span id="cb12-5">p</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-5-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-empty-content callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Please note that we begin by using our data set to create a <strong>plot object</strong> with the function <code>ggplot()</code>. We then assign this object to the variable <strong>p</strong>. From this point forward, we can add layers by using the <code>+</code> operator.
</div>
</div>
<div class="callout-body-container callout-body">

</div>
</div>
</section>
<section id="what-if-we-want-to-customize-our-plot" class="level3" data-number="3.2">
<h3 data-number="3.2" class="anchored" data-anchor-id="what-if-we-want-to-customize-our-plot"><span class="header-section-number">3.2</span> What if we want to customize our plot?</h3>
<p>We can do this using <a href="https://ggplot2.tidyverse.org/reference/ggtheme.html"><code>theme()</code></a>. There are several options of themes which control all non-data display. Use <code>theme()</code> if you just need to tweak the display of an existing theme.</p>
<p>For this session, let’s utilize <code>theme_bw()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">p <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_bw</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm,</span>
<span id="cb13-3">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb13-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species))</span>
<span id="cb13-5">p</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="what-if-we-want-to-change-colors" class="level3" data-number="3.3">
<h3 data-number="3.3" class="anchored" data-anchor-id="what-if-we-want-to-change-colors"><span class="header-section-number">3.3</span> What if we want to change colors?</h3>
<p>We can manually change colors.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Manual color change</span></span>
<span id="cb14-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># By using scale_colour_manual(),</span></span>
<span id="cb14-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># we can specify the exact colours we want to use</span></span>
<span id="cb14-4">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(</span>
<span id="cb14-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note that the color order will correspond to</span></span>
<span id="cb14-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the order of the species given in the legend</span></span>
<span id="cb14-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey55"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"orange"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"skyblue"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>By using the <code>colors()</code> function, you can explore numerous color options that are available for selection.</p>
</section>
<section id="using-a-different-color-palette-rcolorbrewer" class="level3" data-number="3.4">
<h3 data-number="3.4" class="anchored" data-anchor-id="using-a-different-color-palette-rcolorbrewer"><span class="header-section-number">3.4</span> Using a different color palette: <code>RColorBrewer</code></h3>
<p>There is a wide variety of <code>R</code> color packages specifically designed to offer a range of color palette options, each evoking a distinct mood. For instance, the <code>RColorBrewer</code> package provides a choice of 35 palettes!</p>
<p><em>At this point, you have become an expert in the fundamentals of <code>R</code>. Installing packages and loading them with <code>library()</code> is now second nature to you.</em></p>
<p>Now, we can install <code>RColorBrewer</code> and choose one from the many palettes it offers.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">RColorBrewer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">display.brewer.all</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"qual"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Above, you can observe the organization of the colors into distinct groups based on their sequential, diverging, or mixed characteristics. It is important to note that varied palettes are advantageous for varying data types.</p>
<p>Let’s use these palettes with our original penguin graph. Here is an example demonstrating how the <strong>Set1</strong> palette is utilized to group data points with the function <code>scale_color_brewer()</code> and the palette argument.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_brewer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">palette =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Set1"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In addition, the <code>ggplot2</code> package offers other functions. Two such functions are <code>scale_color_viridis()</code> and <code>scale_color_grey()</code>, which allows us to convert colors to <strong>grayscale</strong> without sacrificing information. This is especially important for individuals with colorblindness.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-empty-content callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Please note that when using <code>scale_color_viridis()</code> to color data points, we need to specify whether our variable is continuous [using <code>scale_color_viridis_c()</code>] or discrete [using <code>scale_color_viridis_d()</code>]. In this case, the variable <strong>species</strong> is discrete.
</div>
</div>
<div class="callout-body-container callout-body">

</div>
</div>
<p><br></p>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-1" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-1"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise 1</h4>
<ul>
<li><p>Let us revisit the scatter plot depicting the correlation between bill length and depth, distinguished by different species using colored data points.</p></li>
<li><p>What if we want to use only a grayscale palette for publication purposes?</p></li>
</ul>
<details>
<summary>
Hints (click here)
</summary>
<p>We can use the <code>scale_color_grey()</code> function to color our grouped data points.</p>
</details>
<details>
<summary>
Solutions (click here)
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_grey</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
<p><br></p>
</section>
<section id="colorblind-friendly-palettes" class="level3" data-number="3.5">
<h3 data-number="3.5" class="anchored" data-anchor-id="colorblind-friendly-palettes"><span class="header-section-number">3.5</span> Colorblind-friendly palettes</h3>
<p>Have you ever contemplated how your figure might appear when viewed by individuals with different types of color blindness? We can utilize the <a href="https://cran.r-project.org/web/packages/colorBlindness/index.html"><code>colorBlindness</code></a> package to explore this aspect.</p>
<p>Let’s install the <code>colorBlindness</code> package and load it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's install the colorBlindness package</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"colorBlindness"</span>)</span>
<span id="cb19-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(colorBlindness)</span></code></pre></div></div>
</div>
<p>To begin with, let’s test out various colors using the <code>cvdPlot()</code> function. This will demonstrate how our current plot appears to individuals with different types of color blindness.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1">colorBlindness<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cvdPlot</span>(p)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Our current color palette is not accessible, as can be observed. However, by using <code>viridis</code> palettes, we can ensure that our plots consistently convey the same information, regardless of the audience.</p>
<p>Let’s use the same <code>viridis</code> palette we used above to make our plot more accessible.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">p_viridis <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> p <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb21-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_d</span>()</span></code></pre></div></div>
</div>
<p>Were we successful? Let’s use <code>cvdPlot()</code> to check again.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">colorBlindness<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cvdPlot</span>(p_viridis)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="providing-transparency-with-alpha" class="level3" data-number="3.6">
<h3 data-number="3.6" class="anchored" data-anchor-id="providing-transparency-with-alpha"><span class="header-section-number">3.6</span> Providing transparency with <code>alpha</code></h3>
<p>How can we incorporate transparency into the data points in our graph? One way to achieve this is by utilizing the <code>alpha</code> feature.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, </span>
<span id="cb23-3">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, </span>
<span id="cb23-4">                                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Here, we present an example of how to utilize the <code>alpha</code> function to incorporate transparency into our data points. By doing so, we are able to exhibit four variables within a single graph.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm,</span>
<span id="cb24-3">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb24-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species,</span>
<span id="cb24-5">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> flipper_length_mm))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-2" class="level4 exercise">
<h4 class="anchored" data-anchor-id="b58fc729-690b-4000-b19f-365a4093b2ff7b7b3c20666120757365722d65646974203e7d7d-exercise-2"><i class="fa-solid fa-user-edit" aria-label="user-edit"></i> Exercise 2</h4>
<ul>
<li><p>We like the graph we produced above. We want, however, to also display <code>body_mass_g</code>.</p></li>
<li><p>How can we add this additional variable to our graph?</p></li>
</ul>
<details>
<summary>
Hints (click here)
</summary>
<p>We can use the <code>size</code> function within <code>aes()</code>.</p>
</details>
<details>
<summary>
Solutions (click here)
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb25-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm,</span>
<span id="cb25-3">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm,</span>
<span id="cb25-4">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species,</span>
<span id="cb25-5">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> flipper_length_mm,</span>
<span id="cb25-6">                           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> body_mass_g))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/index_files/figure-html/unnamed-chunk-18-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
<p><br></p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>plotting</category>
  <category>tidyverse</category>
  <guid>https://osu-codeclub.github.io/posts/S11E02_ggplot_02/</guid>
  <pubDate>Mon, 09 Feb 2026 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E02_ggplot_02/img/penguins.png" medium="image" type="image/png" height="86" width="144"/>
</item>
<item>
  <title>Introduction to ggplot2 - 01</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S11E01_ggplot_01/</link>
  <description><![CDATA[ 




<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/img/ggplot2_masterpiece.png" class="img-fluid figure-img" style="width:70.0%" alt="A fuzzy monster in a beret and scarf, critiquing their own column graph on a canvas in front of them while other assistant monsters (also in berets) carry over boxes full of elements that can be used to customize a graph (like themes and geometric shapes). In the background is a wall with framed data visualizations. Stylized text reads “ggplot2: build a data masterpiece."></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p><a href="https://en.wikipedia.org/wiki/A_picture_is_worth_a_thousand_words">A picture is worth a thousand words</a> is a popular adage and shows the power of a great figure. We are going to spend the next handfull of Code Club sessions on data visualization.</p>
<p>The very popular R package <a href="https://ggplot2.tidyverse.org/index.html"><code>ggplot2</code></a> is based on a system called the <a href="https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448/ref=as_li_ss_tl">Grammar of Graphics</a> by Leland Wilkinson which aims to create a grammatical rules for the development of graphics. It is part of a larger group of packages called “the tidyverse.”</p>
<section id="what-is-the-tidyverse" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="what-is-the-tidyverse"><span class="header-section-number">1.1</span> What is the tidyverse?</h2>
<p>The package <code>ggplot2</code> is a part of a larger collection of packages called <a href="https://www.tidyverse.org/">“the tidyverse”</a> that are designed for data science. You can certainly use R without using the tidyverse, but it has many packages that I think will make your life a lot easier. We can install just <code>ggplot2</code> or install all of the packages in the core tidyverse (which is what I’d recommend since we will use the others too), which include:</p>
<ul>
<li><a href="https://dplyr.tidyverse.org/"><code>dplyr</code></a>: for data manipulation</li>
<li><a href="https://ggplot2.tidyverse.org/"><code>ggplot2</code></a>: a “grammar of graphics” for creating beautiful plots</li>
<li><a href="https://readr.tidyverse.org/"><code>readr</code></a>: for reading in rectangular data (i.e., Excel-style formatting)</li>
<li><a href="https://tibble.tidyverse.org/"><code>tibble</code></a>: using tibbles as modern/better dataframes</li>
<li><a href="https://stringr.tidyverse.org/"><code>stringr</code></a>: handling strings (i.e., text or stuff in quotes)</li>
<li><a href="https://forcats.tidyverse.org/"><code>forcats</code></a>: for handling categorical variables (i.e., factors) (meow!)</li>
<li><a href="https://tidyr.tidyverse.org/"><code>tidyr</code></a>: to make “tidy data”</li>
<li><a href="https://purrr.tidyverse.org/"><code>purrr</code></a>: for enhancing functional programming (also meow!)</li>
<li><a href="https://lubridate.tidyverse.org/"><code>lubridate</code></a>: for working with dates</li>
</ul>
<p>We have used many of these other packages in Code Club. There are more tidyverse packages outside of these core nine, and we will talk about some of them another time.</p>
<blockquote class="blockquote">
<p><strong>tl;dr</strong> Tidyverse has a lot of packages that make data analysis easier. None of them are required, but I think you’ll find many tidyverse approaches easier and more intuitive than using base R.</p>
</blockquote>
<p>You can find <a href="https://tavareshugo.github.io/data_carpentry_extras/base-r_tidyverse_equivalents/base-r_tidyverse_equivalents.html">here</a> some examples of comparing tidyverse and base R syntax.</p>
</section>
<section id="installing-ggplot-tidyverse" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="installing-ggplot-tidyverse"><span class="header-section-number">1.2</span> Installing ggplot &amp; tidyverse</h2>
<p>To install packages in R that are on the <a href="https://cran.r-project.org/">Comprehensive R Archive Network (CRAN)</a>, you can use the function <code>install.packages()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tidyverse"</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot2"</span>)</span></code></pre></div></div>
</div>
<p>We only need to install packages once. But, every time we want to use them, we need to “load” them, and can do this using the function <code>library()</code>. Since you will likely often use the <code>tidyverse</code> functions, it’s a good habit to add the code <code>library(tidyverse)</code> to the top of each of your scripts/RMarkdown/Quarto documents.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.1     ✔ stringr   1.5.2
✔ ggplot2   4.0.0     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
</div>
</section>
</section>
<section id="what-is-ggplot" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> What is “ggplot?”</h1>
<p>The “gg” in ggplot stands for “grammar of graphics” and all plots share a common template. This is fundamentally different than plotting using a program like Excel, where you first pick your plot type, and then you add your data. With ggplot, you start with data, add a coordinate system, and then add “geoms,” which indicate what type of plot you want. A cool thing about ggplot is that you can add and layer different geoms together, to create a fully customized plot that is exactly what you want. If this sounds nebulous right now, that’s okay, we are going to talk more about this.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/img/ggplot2_exploratory.png" class="img-fluid figure-img" style="width:70.0%" alt="A group of fuzzy round monsters with binoculars, backpacks and guide books looking up a graphs flying around with wings (like birders, but with exploratory data visualizations). Stylized text reads “ggplot2: visual data exploration.”"></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="what-can-you-do-with-ggplot" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> What can you do with ggplot?</h1>
<p>Let’s start by looking at the different types of plots that can be made using <code>ggplot2</code>. We will do this by looking at the <a href="https://rstudio.github.io/cheatsheets/data-visualization.pdf"><code>ggplot2</code> cheatsheet</a>.</p>
</section>
<section id="a-plotting-framework" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> A plotting framework</h1>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/img/plotting_framework.png" class="img-fluid figure-img" style="width:70.0%" alt="A pictorial depiction of the different ggplot layers, starting with data, aesthetics, geometries, scales, facets, coordinates, labels, and themes"></p>
<figcaption>Figure from <a href="https://datavizs21.classes.andrewheiss.com/">Andrew Heiss</a></figcaption>
</figure>
</div>
</div>
</div>
<p>You can think about a ggplot as being composed of layers. You start with your data, and continue to add layers until you get the plot that you want. This might sound a bit abstract so I am going to talk through this with an example.</p>
<p>First, let’s load some practice data. We are going to use a fun 🐧 data set from the package <a href="https://allisonhorst.github.io/palmerpenguins/"><code>palmerpenguins</code></a>. If you don’t already have this, you can download it with the code below:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"palmerpenguins"</span>)</span></code></pre></div></div>
</div>
<p>Then we can load the data.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'palmerpenguins'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:datasets':

    penguins, penguins_raw</code></pre>
</div>
</div>
<p>The dataset itself is called <code>penguins</code>. Let’s look at it using the function <code>glimpse()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(penguins)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 344
Columns: 8
$ species           &lt;fct&gt; Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island            &lt;fct&gt; Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm    &lt;dbl&gt; 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm     &lt;dbl&gt; 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm &lt;int&gt; 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g       &lt;int&gt; 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex               &lt;fct&gt; male, female, female, NA, female, male, female, male…
$ year              &lt;int&gt; 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…</code></pre>
</div>
</div>
<p>Let’s start by trying to make a simple scatterplot, where we see the relationship between <code>bill_length_mm</code> and <code>bill_depth_mm</code>.</p>
<section id="data" class="level2" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="data"><span class="header-section-number">4.1</span> Data</h2>
<p>The first argument passed to your plot is the data. How did I know that? It’s in the documentation.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1">?<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>()</span></code></pre></div></div>
</div>
<p>The simplest ggplot code you can write, just using the <code>ggplot()</code> function and indicating the data we want to use. Because data is the default first argument, you can actually omit the <code>data =</code> part of this code and it will work just the same.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> penguins)</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/data only-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Why do we not see a plot? Well we haven’t told R what to plot! We are getting the first “base” layer of the plot.</p>
<p>You can also pipe <code>|&gt;</code> or <code>%&gt;%</code>, the data to the ggplot function. When reading code, you can interpret the pipe as “and then.” Here, take the <code>penguins</code> data, and then, run <code>ggplot()</code>. Writing code in this way is my preference so I tend to code like this. We talked in more detail about the pipe in past Code Clubs.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/data and pipe-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Still nothing. Well that’s what we would expect.</p>
</section>
<section id="aesthetic-mappings-aes" class="level2" data-number="4.2">
<h2 data-number="4.2" class="anchored" data-anchor-id="aesthetic-mappings-aes"><span class="header-section-number">4.2</span> Aesthetic mappings <code>aes()</code></h2>
<p>Now that we’ve indicated our data, we can add aesthetics mapping so we can work towards actually see a plot. We want to make a scatterplot where on the x-axis we have bill length (<code>bill_length_mm</code>), and on the y-axis we have bill depth (<code>bill_depth_mm</code>).</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm))</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/aes-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>So we have progressed from a blank plot, but we still do not have a plot by basically anyone’s defintion. Why not?</p>
<p>Even though we have indicated to R our data and aesthetic mappings, we have not indicated what precisely to do with our data. We have said what we want on x and y (and now we can see those labelled appearing) but we have not indicated what <em>type</em> of plot we want. And, we can do that in the next step, by adding a <code>geom_</code>.</p>
</section>
<section id="geoms-geom_" class="level2" data-number="4.3">
<h2 data-number="4.3" class="anchored" data-anchor-id="geoms-geom_"><span class="header-section-number">4.3</span> Geoms <code>geom_</code></h2>
<p>Now let’s indicate what type of plot we want. In this example, we are going to make a scatterplot, and to do that we will use <a href="https://ggplot2.tidyverse.org/reference/geom_point.html"><code>geom_point()</code></a></p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/geom point-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>We have a plot! It’s not a finished plot, but its a plot and we can work from here.</p>
<p>Let’s say we wanted to see whether penguins of different <code>species</code> are in different places on our plot. We can take the variable <code>species</code> and map it to the aesthetic <code>color</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb16-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/color-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Note what R has done for us - we now see each dot colored based on which species it is, and we also have a new legend.</p>
<p>What if we wanted to add a line that shows the relationship between <code>bill_length_mm</code> and <code>bill_depth_mm</code> for each <code>species</code>? We can layer in another geom, here we will use <a href="https://ggplot2.tidyverse.org/reference/geom_smooth.html">geom_smooth</a>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb18-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the method is a linear model</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_smooth()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<section id="global-vs.-local-aes" class="level3" data-number="4.3.1">
<h3 data-number="4.3.1" class="anchored" data-anchor-id="global-vs.-local-aes"><span class="header-section-number">4.3.1</span> Global vs.&nbsp;local <code>aes()</code></h3>
<p>A note about aesthetic mappings now that we have introduced geoms -<code>aes()</code> can go in two places:</p>
<ul>
<li>in the <code>ggplot()</code> call, and this means they will inherit for every layer of the plot</li>
<li>in a specific <code>geom_</code>, and those aesthetics will only be for that specific geom.</li>
</ul>
<p>So we can make the same plot we saw above by mapping aesthetics within <code>geom_point()</code>.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb22-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb22-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb24-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/local aes-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Let’s look at example where changing the location of the aesthetic mappings does make a difference.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb26-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the method is a linear model</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_smooth()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the method is a linear model</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-error">
<pre><code>Error in `geom_smooth()`:
! Problem while computing stat.
ℹ Error occurred in the 2nd layer.
Caused by error in `compute_layer()`:
! `stat_smooth()` requires the following missing aesthetics: x and y.</code></pre>
</div>
</div>
<p>What happened here? We got an “error in <code>geom_smooth()...</code>stat_smooth()` requires the following miss aesthetics: x and y”.</p>
<p>This happened because we have only set our x and y aesthetics in <code>geom_point()</code> and not in <code>geom_smooth()</code> so R doesn’t know what to map x and y to. When we map our aesthetics globally, we don’t have this problem because x and y inherit for every subsequent layer.</p>
<p>We can also do a combination of global and local setting.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb33-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb33-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the method is a linear model</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_smooth()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-10-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb37-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> species), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb37-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_smooth</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">method =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the method is a linear model</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_smooth()`).</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range (`stat_smooth()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-10-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>When we set <code>color</code> only in <code>geom_point()</code>, we do not “group” by <code>color</code> (here, by <code>species</code>) so we get our smoothed line for all the data (instead of by <code>species</code>).</p>
</section>
<section id="mapping-vs.-setting" class="level3" data-number="4.3.2">
<h3 data-number="4.3.2" class="anchored" data-anchor-id="mapping-vs.-setting"><span class="header-section-number">4.3.2</span> Mapping vs.&nbsp;‘setting’</h3>
<p>If you want to map a variable to an aesthetic, it MUST be within the <code>aes()</code> statement. If you just want to change the color to “blue” for example, it should be outside the <code>aes()</code> statement. Look at the difference.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb42-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#088F8F"</span>) </span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-11-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>If we put “blue” instead our aesthetic mappings, we get something that doesn’t make sense.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb44-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> bill_depth_mm)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb44-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-12-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<blockquote class="blockquote">
<p><strong>tl:dr</strong> if mapping a variable to an aesthetic, inside <code>aes()</code>, if not, then outside.</p>
</blockquote>
</section>
</section>
</section>
<section id="practice" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Practice</h1>
<p>Create a plot that shows the relationship between flipper length and body mass. Color your points based on the sex of the penguins.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Try using the variables <code>flipper_length</code>, <code>body_mass_g</code>, and <code>sex</code>. You can make <code>x</code>, <code>y</code>, and <code>color</code>.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb46-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb46-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-13-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Not happy with the missing value? We can remove it.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb48-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(flipper_length_mm, body_mass_g, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop missing values </span></span>
<span id="cb48-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> flipper_length_mm, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb48-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-14-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
<section id="different-geoms" class="level2" data-number="5.1">
<h2 data-number="5.1" class="anchored" data-anchor-id="different-geoms"><span class="header-section-number">5.1</span> Different geoms</h2>
<p>Create a boxplot that shows the distribution of body mass for penguins on the different islands.</p>
<details>
<summary>
Need a hint?
</summary>
<p>The geom for a boxplot is called <a href="https://ggplot2.tidyverse.org/reference/geom_boxplot.html"><code>geom_boxplot()</code></a>.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb49-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb49-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass_g)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb49-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-15-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>
</section>
<section id="mapping-to-other-aesthetics" class="level2" data-number="5.2">
<h2 data-number="5.2" class="anchored" data-anchor-id="mapping-to-other-aesthetics"><span class="header-section-number">5.2</span> Mapping to other aesthetics</h2>
<p>Create a scatterplot that shows the relationship between bill length and bill depth, but color the points based on what island the penguins are from, and make the points a different shape based on sex.</p>
<details>
<summary>
Need a hint?
</summary>
<p>You can make the aesthetic <code>shape =</code> in the same way you use color.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb51-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, bill_depth_mm, </span>
<span id="cb51-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb51-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>Not happy with missing values? We can remove them.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb53-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>(bill_length_mm, bill_depth_mm, island, sex) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># drop missing values</span></span>
<span id="cb53-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> bill_length_mm, bill_depth_mm, </span>
<span id="cb53-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> island, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb53-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</details>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>ggplot2</category>
  <category>dataviz</category>
  <category>plotting</category>
  <category>tidyverse</category>
  <guid>https://osu-codeclub.github.io/posts/S11E01_ggplot_01/</guid>
  <pubDate>Mon, 02 Feb 2026 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S11E01_ggplot_01/img/ggplot2_masterpiece.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Continuing with Git and GitHub 🐱</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E12_git_02/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>We are continuing one of this semester’s Code Club series on reproducible research. We are closing out the last session with some more about using git and GitHub.</p>
<p>Let’s quickly remind ourselves of the git workflow:</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/github-cartoon.png" class="img-fluid figure-img" style="width:70.0%" alt="A basic git workflow represented as two islands, one with local repo and working directory, and another with remote repo. Bunnies move file boxes from the working directory to the staging area, then with Commit move them to the local repo. Bunnies in rowboats move changes from the local repo to the remote repo (labeled PUSH) and from the remote repo to the working directory (labeled PULL). Art by Allison Horst"></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p>Last <a href="https://osu-codeclub.github.io/posts/S10E11_git_01/">session</a>, we:</p>
<ul>
<li>Installed <a href="https://git-scm.com/downloads">Git</a></li>
<li>Created a <a href="https://github.com/join">GitHub account</a></li>
<li>Installed <a href="https://desktop.github.com/">GitHub desktop</a> and link it to your GitHub account</li>
<li>Made sure we were working on a RStudio project (<code>.Rproj</code>)</li>
<li>Rendered a GitHub flavored markdown document so we would have something to post</li>
<li>Added our local repository to GitHub desktop and add version control</li>
<li>Published our repository</li>
</ul>
<p>Today, we are going to go over:</p>
<ul>
<li>How to edit contents in your repository</li>
<li>How to push those changes to GitHub</li>
<li>How to set up a <code>.gitignore</code></li>
<li>How to collaborate and what a merge conflict looks like</li>
</ul>
<p>Here is what my repo looks like now:</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/repo-start.png" class="img-fluid"></p>
</section>
<section id="edit-contents-in-your-repo" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Edit contents in your repo</h1>
<p>You all should by now have a repo which contains our material from the last Code Club session. One thing that came up last time was making changes to your <code>README.md</code> so that the material on the front page of your repo is updated with some information about its contents.</p>
<p>Last time, we initialized our repository with a README. If we look into our directory that is being tracked by git, we will see a file called <code>README.md</code>. Go ahead and open it.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>If you didn’t initialize with a README, that is ok, we can make one.
</div>
</div>
<div class="callout-body-container callout-body">
<p>Go to <code>File</code> &gt; <code>New File</code> &gt; <code>Markdown file</code>. Save this file in your folder being tracked by git with the name <code>README.md</code>.</p>
</div>
</div>
<p>Since my README was pushed to GitHub, you can see it there:</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/readme-original-screenshot.png" class="img-fluid"></p>
<p>When I initialized my README, I gave it a description, so you can see that listed underneath the name of the repo on GitHub.</p>
<p>We can open up that <code>README.md</code> in RStudio to make edits to it.</p>
<p>My file looks like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"># test-20251117</span></span>
<span id="cb1-2">Autumn 2025's first lil repo for code club</span></code></pre></div></div>
<p>Let’s make some changes to our <code>README.md</code> and push them to GitHub.</p>
<p>Since this is a Markdown document (file extension <code>.md</code>), it needs to be written in the Markdown language.</p>
<ul>
<li>The “#” indicates a level 1 (i.e., top level) header. That is why this is rendered to be larger and bolded text.</li>
<li>Regular text can be typed just like you would in a text editor.</li>
</ul>
<p>I am going to change my file to look like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"># Country-level population growth from 2000 to 2015</span></span>
<span id="cb2-2"></span>
<span id="cb2-3">This repository contains code to calculate the percent rate of growth for each </span>
<span id="cb2-4">country from 2000 to 2015. The data comes from </span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">The World Factbook</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">](https://www.cia.gov/the-world-factbook/)</span> and can be </span>
<span id="cb2-6">downloaded on its own <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">here</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">](https://github.com/osu-codeclub/osu-codeclub.github.io/blob/main/posts/S08E01_wrangling_01/factbook_download.csv)</span>.</span></code></pre></div></div>
<p>Click <code>Preview</code> to render your <code>README.md</code> and see how it looks.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/readme-rendered.png" class="img-fluid"></p>
<section id="practice" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="practice"><span class="header-section-number">2.1</span> Practice</h2>
<p>Try making a change to your README and preview the change.</p>
</section>
<section id="push-your-changes-to-github" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="push-your-changes-to-github"><span class="header-section-number">2.2</span> Push your changes to GitHub</h2>
<p>Now that we’ve made a change to our <code>README.md</code>, let’s push that change to GitHub. Start by opening GitHub Desktop. Make sure your repo is selected in the top right corner.</p>
<p>All of the changes we made are now tracked and specified in GitHub Desktop. We can see that two files have changed (our <code>README.md</code> and <code>README.html</code>). If you click on <code>README.md</code> you can see the changes we just made. These changes look more chaotic because they’re rendered in html.</p>
<p>To send these changes to GitHub, we can enter a commit message, and click <code>Commit to main</code>.</p>
<p>Then we will click <code>Push origin</code> (either as the blue button or on the top right). Note that in the bottom right corner, and we can see what our commit was.</p>
<p>Once we have done that, we are now “working clean,” meaning we have made no new changes to our files being tracked by git, and what is present locally will be the same as what is on GitHub.</p>
<p>We can look online to see how our repo looks now, a few things to notice:</p>
<ul>
<li>Our README has changed - we can now see the changes we made reflected in the README that populates on the home page of our repo. If we click on <code>README.md</code>, we can also see those changes.</li>
<li>We have a new most recent commit message (here, “Updating to a more descriptive readme”), and we can also see when this commit was made</li>
</ul>
</section>
<section id="practice-1" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="practice-1"><span class="header-section-number">2.3</span> Practice</h2>
<p>Try pushing your changes to GitHub.</p>
</section>
</section>
<section id="editing-our-.gitignore" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Editing our <code>.gitignore</code></h1>
<p>If we look in our repo, we see some files are on GitHub that perhaps don’t need to be there.</p>
<p>Let’s look at our current <code>.gitignore</code> and see what’s in that file. Mine looks like this:</p>
<pre><code># History files
.Rhistory
.Rapp.history

# Session Data files
.RData
.RDataTmp

# User-specific files
.Ruserdata

# Example code in package build process
*-Ex.R

# Output files from R CMD build
/*.tar.gz

# Output files from R CMD check
/*.Rcheck/

# RStudio files
.Rproj.user/

# produced vignettes
vignettes/*.html
vignettes/*.pdf

# OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3
.httr-oauth

# knitr and R markdown default cache directories
*_cache/
/cache/

# Temporary files created by R markdown
*.utf8.md
*.knit.md

# R Environment Variables
.Renviron

# pkgdown site
docs/

# translation temp files
po/*~

# RStudio Connect folder
rsconnect/</code></pre>
<p>Our <code>gitignore</code> has poulated like this because when we set it up, we selected that we are working in R, and these are some files that people working in R often like to be ignored by version control.</p>
<p>This gives you some options to types of files you can put in your <code>.gitignore</code>. Today we are going to add:</p>
<ul>
<li><code>.DS_Store</code> - this is a Mac specific filetyle</li>
<li><code>.gitattributes</code></li>
</ul>
<p>I will add these files to my <code>.gitignore</code> and leave the rest of the text as it is.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode md code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"># Mac specific files</span></span>
<span id="cb4-2">.DS_Store</span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"># git attributes</span></span>
<span id="cb4-5">.gitattributes</span></code></pre></div></div>
<p>If I look in GitHub Desktop now, I can see that those changes have been tracked.</p>
<p>Now, we can make a commit, and then push to main.</p>
<p>But - when we look at our repo, those files are still there!</p>
<p>That is because we have told Git to stop tracking them, but we haven’t actually removed them from GitHub. We can do that now.</p>
<p>This is a good opportunity for us to try using the terminal a little, since I can’t figure out how to do this without doing so. In GitHub Desktop, go to <code>Repository</code> &gt; <code>Open in Terminal</code>. This will open up a terminal on your computer in the location of your tracked directory.</p>
<p>In our terminal, we can remove the files we do not want on GitHub. When your terminal is open you should see something like this:</p>
<pre><code>computer-username@name-of-computer repo-name %</code></pre>
<p>You will use the command <code>git rm --cached name-of-file</code> to remove the files you added to your <code>.gitignore</code> but are still on GitHub. Two examples are below:</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/rm_ds_store.png" class="img-fluid"></p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/rm_gitattributes.png" class="img-fluid"></p>
<section id="practice-2" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="practice-2"><span class="header-section-number">3.1</span> Practice</h2>
<p>Try removing files that you don’t want tracked with git.</p>
</section>
<section id="push-our-.gitignore-changes-to-github" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="push-our-.gitignore-changes-to-github"><span class="header-section-number">3.2</span> Push our <code>.gitignore</code> changes to GitHub</h2>
<p>Now that we’ve removed the files we want, we can push our changes to GitHub. If you look at your GitHub Desktop, you should now see something like this:</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/removed-files-github-desktop.png" class="img-fluid"></p>
<p>We can make another commit, and push that change to main.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/remove-files-commit.png" class="img-fluid"></p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/remove-files-push.png" class="img-fluid"></p>
<p>Now, if we look on GitHub, we can see those files are now gone.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/ignored-files-gone.png" class="img-fluid"></p>
<p>Do keep in mind that those files are not totally gone – if we go back to our original commit, we will see those files again. Keep this in mind, that once something is committed, it is really tracked. You can see this by clicking on “commits” in the top right corner of your repo. When you do this, you can see the whole commit history of this repo.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/commit-history.png" class="img-fluid"></p>
<p>And if we click on our original commit, we can see those files we added to our <code>.gitignore</code> and their contents.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E12_git_02/img/ignored-files-original-commit.png" class="img-fluid"></p>
</section>
<section id="practice-3" class="level2" data-number="3.3">
<h2 data-number="3.3" class="anchored" data-anchor-id="practice-3"><span class="header-section-number">3.3</span> Practice</h2>
<p>Push your <code>gitignore</code> changes to GitHub.</p>
</section>
</section>
<section id="collaborating-and-merge-conflicts" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Collaborating and merge conflicts</h1>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>git</category>
  <category>github</category>
  <category>version control</category>
  <category>quarto</category>
  <guid>https://osu-codeclub.github.io/posts/S10E12_git_02/</guid>
  <pubDate>Mon, 01 Dec 2025 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E12_git_02/img/github-cartoon.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Getting started with Git and GitHub 🐱</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E11_git_01/</link>
  <description><![CDATA[ 




<hr>
<p><br></p>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>We are continuing one of this semester’s Code Club series on reproducible research. Since we started this at the beginning of the semester and now its mid-November let’s recap. So far, we have gone through:</p>
<ol type="1">
<li><a href="https://osu-codeclub.github.io/posts/S10E01_reprod_01/">An intro to Quarto</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S10E02_reprod_02/">A little more about Quarto</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S10E03_reprod_03/">File organization and RStudio projects</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S10E04_reprod_04/">Code structuring</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S10E05_reprod_05/">Code styling</a></li>
</ol>
<p>Today, we are going to take some code we’ve written and push it to a repository on GitHub. To do this we will need to:</p>
<ul>
<li>Install <a href="https://git-scm.com/downloads">Git</a></li>
<li>Create a <a href="https://github.com/join">GitHub account</a></li>
<li>Install <a href="https://desktop.github.com/">GitHub desktop</a> and link it to your GitHub account</li>
<li>Render a GitHub flavored markdown document</li>
<li>Add our repository to GitHub desktop and add version control</li>
<li>Publish your repository</li>
</ul>
<p>The book <a href="https://happygitwithr.com/">Happy Git and GitHub for the useR</a> is a super good resource by Jenny Bryan that talks about all this in extreme detail. Some of my materials below are inspired by some of this material.</p>
<section id="what-is-git" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="what-is-git"><span class="header-section-number">1.1</span> What is Git?</h2>
<p><a href="https://git-scm.com/">Git</a> is a version control system that allows materials (mostly code) to be tracked in an organized way. Every time any changes to the files that are being monitored by Git are changed, that change is tracked. You can think of it as combining the tracked changes capabilities of Word, with the file sharing from OneDrive.</p>
<p>We are going to use Git (combined with GitHub, more on that in a second) to take the information embedded within our Quarto document and make it available on GitHub. You could opt use Git for just yourself because it would allow you to always revert back to a previous version of any of your files in case you make a big mistake.</p>
<p>Here is a nice illustration by <a href="https://allisonhorst.com/allison-horst">Allison Horst</a> that shows the basics of git (at least the basics of what we will be using).</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/github-cartoon.png" class="img-fluid figure-img" style="width:70.0%" alt="A basic git workflow represented as two islands, one with local repo and working directory, and another with remote repo. Bunnies move file boxes from the working directory to the staging area, then with Commit move them to the local repo. Bunnies in rowboats move changes from the local repo to the remote repo (labeled PUSH) and from the remote repo to the working directory (labeled PULL). Art by Allison Horst"></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
</section>
<section id="what-is-github" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="what-is-github"><span class="header-section-number">1.2</span> What is GitHub?</h2>
<p><a href="https://github.com/">GitHub</a> hosts Git-based projects. GitHub can be free (especially for academic projects), and is where we house all our <a href="https://github.com/osu-codeclub/osu-codeclub.github.io">Code Club content</a>. I additionally use GitHub in my lab for <a href="https://github.com/CooperstoneLab">creating code repositories that go along with our publications</a>.</p>
</section>
<section id="what-is-github-desktop" class="level2" data-number="1.3">
<h2 data-number="1.3" class="anchored" data-anchor-id="what-is-github-desktop"><span class="header-section-number">1.3</span> What is GitHub Desktop?</h2>
<p><a href="https://desktop.github.com/">GitHub Desktop</a> is a application that lets you use Git and GitHub in a less-scary-than-working-in-your-terminal way. <a href="https://www.simplilearn.com/how-to-use-github-desktop-tutorial-article">Here</a> is a longer tutorial on how to use GitHub desktop.</p>
</section>
</section>
<section id="install-git" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Install Git</h1>
<p>If you don’t already have Git, download and install it: <a href="https://git-scm.com/downloads" class="uri">https://git-scm.com/downloads</a>.</p>
</section>
<section id="create-a-github-account" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Create a GitHub account</h1>
<p>In order to connect your local files to GitHub, you need a GitHub account.</p>
<p>Create a GitHub account by going to <a href="https://github.com/join">github.com/join</a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/create-gh-account.png" class="img-fluid figure-img"></p>
<figcaption>a screenshot of getting a GitHub account</figcaption>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you’ve been here all semester, you will already have created a GitHub account to enable GitHub Copilot in RStudio.</p>
</div>
</div>
<p>You will have to pick a username - remember that other people will see this username so use one you’d feel comfortable with. <a href="https://happygitwithr.com/github-acct.html">Here</a> is some advice for picking a username. I use my OSU email address with GitHub but you can use a personal one too.</p>
<p>You will then have a profile on GitHub, here’s an example of <a href="https://github.com/jcooperstone">mine</a>.</p>
</section>
<section id="install-github-desktop-and-link-it-to-your-account" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Install GitHub Desktop and link it to your account</h1>
<p>We are going to interface with GitHub via Github Desktop because I think this is the easiest way to do so. It is definitely not the only way.</p>
<p>First download GitHub Desktop and install it: <a href="https://desktop.github.com/" class="uri">https://desktop.github.com/</a></p>
<p>Open GitHub Desktop and log into your GitHub account by going to <code>Preferences</code> &gt; <code>Accounts</code> &gt; <code>Sign in</code></p>
</section>
<section id="create-a-new-project-which-will-become-tracked-and-we-will-eventually-share" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Create a new Project which will become tracked and we will eventually share</h1>
<p>I would recommend making a new R Project here as you probably don’t want to share all the stuff in the Code Club project you’re currently using.</p>
<p>We can do that by going to <code>File</code> &gt; <code>New Project</code>. I am going to start one in a <code>New Directory</code>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can use an existing Project as the source for what you plan to put on GitHub too, in fact this would be the more common situation for real material.</p>
</div>
</div>
</section>
<section id="render-a-github-flavored-markdown-gfm-document-to-prepare-to-push" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Render a GitHub flavored markdown (GFM) document to prepare to push</h1>
<p>We need some material to put in our new repository. We can use the <a href="https://osu-codeclub.github.io/posts/S08E08_reprod_03/">some old code club code</a>, you can download a <code>.qmd</code> file to work from <a href="https://github.com/osu-codeclub/osu-codeclub.github.io/blob/main/posts/S10E11_git_01/factbook_20251117.qmd">here</a>.</p>
<div class="callout callout-style-default callout-important callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Important</span>If you download my <code>.qmd</code>
</div>
</div>
<div class="callout-body-container callout-body">
<p>I indicated in the YAML that <code>eval: false</code> so that the code doesn’t run on the website. You will want to remove this part of the YAML on lines 5 and 6.</p>
</div>
</div>
<p>Open up this <code>.qmd</code> and set your YAML such that it renders as the format GitHub flavored markdown.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">title</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"My first code on GitHub"</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">author</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jessica Cooperstone"</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"November 17, 2025"</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> gfm</span></span>
<span id="cb1-6"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
<p>Render this document. You should now see in the <code>Files</code> quadrant a new file called <code>name-of-quarto-doc.md</code> which contains your rendered document in GitHub flavored markdown (markdown is what the <code>.md</code> is).</p>
<p>Your rendered document should look something like this:</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/md-rendered-screenshot.png" class="img-fluid"></p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Why use GitHub flavored markdown? What’s nice about it is that your code and output will all be immediately viewable on GitHub. You could push a Quarto or RMarkdown document, or one of these files that has been rendered (so now its a <code>.html</code>) and those files will be downloadable from GitHub but not viewable in your browser online.</p>
</div>
</div>
<p>In the folder of my repo, I have the following content:</p>
<ul>
<li>an .<code>Rproj</code> file</li>
<li>a <code>.qmd</code> file</li>
<li>a <code>.md</code> file that rendered from the <code>.qmd</code></li>
<li>a data folder where I store the data I read in</li>
</ul>
<p>We can upload all, or only some of this material to GitHub. I am going to show you how to send it all today, and in the next lesson on Git, we will go over gitignore.</p>
</section>
<section id="add-a-local-repository-to-github-desktop-and-add-version-control" class="level1" data-number="7">
<h1 data-number="7"><span class="header-section-number">7</span> Add a local repository to GitHub desktop and add version control</h1>
<p>Now we are going to add the folder containing our <code>.Rproj</code> to be tracked with GitHub Desktop.</p>
<p>In GitHub Desktop, click <code>File</code> &gt; <code>Add Local Repository</code>, select the directory with your <code>.Rproj</code>’s top-level folder, and click <code>Add Repository</code>.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/add-local-repo.png" class="img-fluid"></p>
<p>When we go to do this, we see that GitHub desktop is telling us that this directory does not appear to be a Git repository. We can click “create a repository” to initiate version control on this folder.</p>
<p>Here, you want to:</p>
<ul>
<li>give your repo a name (mine is first-repo)</li>
<li>provide a description</li>
<li>provide a local path to your folder (this should have already been done in the previous step)</li>
<li>check if you want to initialize with a README (we do, check yes)</li>
<li>select if you want a git ignore (we do, set the type as R, and we will talk about it more next week)</li>
<li>indicate if you want a license on your repo (I am not going to set one up)</li>
</ul>
<p>Then you can click the blue button <code>Create Repository</code>.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/create-repo-github-desktop.png" class="img-fluid"></p>
<p>You will notice that now in our <code>.Rproj</code> folder, we have some new files, including:</p>
<ul>
<li><code>.gitattributes</code> (which gives attributes for pathnames)</li>
<li><code>.gitignore</code> (which tells git which files to ignore, and we are going to go over this in the next sessions)</li>
<li><code>README.md</code> (contains the information that will be a part of your readme file)</li>
</ul>
</section>
<section id="publish-our-repo" class="level1" data-number="8">
<h1 data-number="8"><span class="header-section-number">8</span> Publish our repo</h1>
<p>Now, we can publish our repo on GitHub. This will create the repo on GitHub, and it will contain all of our content in it.</p>
<p>What we can see that GitHub desktop has done for us, since we are setting our repo up for the first time, is automatically made an Initial Commit (see bottom left). We are going to click Undo next to this so we can practice doing this ourselves. You won’t have to do this step in the future.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/undo-initial-commit.png" class="img-fluid"></p>
<p>Now we can see in GitHub desktop all of the files that are going to be sent to our repository on the lefthand side of the screen.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/before-first-commit.png" class="img-fluid"></p>
<p>Remember, this is the process of interacting with GitHub.</p>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/github-cartoon.png" class="img-fluid figure-img" style="width:70.0%" alt="A basic git workflow represented as two islands, one with local repo and working directory, and another with remote repo. Bunnies move file boxes from the working directory to the staging area, then with Commit move them to the local repo. Bunnies in rowboats move changes from the local repo to the remote repo (labeled PUSH) and from the remote repo to the working directory (labeled PULL). Art by Allison Horst"></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p>We can then add a message about our first commit and click <code>Commit to main</code>.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/first-commit.png" class="img-fluid"></p>
<p>Now we need to send our repository to GitHub. Since we are doing this for the first time, we see a button that says <code>Publish repository</code>. In the future, this button will say <code>Push to origin</code>.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/publish-repository-post-commit.png" class="img-fluid"></p>
<p>We are getting one last popup asking us to confirm our repo info, which can do with the <code>Publish Repository</code> button.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/publish-repo-final-conf.png" class="img-fluid"></p>
<p>Our repo is now on GitHub, and we can see it online if we go to the repositories tab of your personal GitHub page. You can see how this looks on my page - this is probably your first repo, and although I’ve called mine <code>first-repo</code> it is not for me.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E11_git_01/img/git-repo-screenshot.png" class="img-fluid"></p>
<p>We can click on our repo and see what it contains. If we click on our file that ends in <code>.md</code>, we can see our GitHub flavored markdown rendered document. Here is <a href="https://github.com/jcooperstone/first-repo/blob/main/first-push.md">mine</a>.</p>
</section>
<section id="next-time" class="level1" data-number="9">
<h1 data-number="9"><span class="header-section-number">9</span> Next time</h1>
<p>Next session we are going to go over:</p>
<ul>
<li>Making changes and pushing them to GitHub</li>
<li>Setting our gitignore</li>
<li>Maybe some other stuff</li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>git</category>
  <category>github</category>
  <category>version control</category>
  <category>quarto</category>
  <guid>https://osu-codeclub.github.io/posts/S10E11_git_01/</guid>
  <pubDate>Mon, 17 Nov 2025 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E11_git_01/img/github-cartoon.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Using Copilot, chattr, and ellmer</title>
  <dc:creator>Jessica Cooperstone and Horacio Lopez-Nicora</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E10_ai_03/</link>
  <description><![CDATA[ 




<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E10_ai_03/img/chattr-ellmer-hex.png" class="img-fluid figure-img"></p>
<figcaption><code>chattr</code> and <code>ellmer</code> hexes</figcaption>
</figure>
</div>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>We have spent the last handful of sessions using GitHub Copilot embedded in the RStudio IDE. Today we are going to try using:</p>
<ul>
<li>Microsoft Copilot in our browser</li>
<li>The package <a href="https://mlverse.github.io/chattr/"><code>chattr</code></a> and <a href="https://ellmer.tidyverse.org/"><code>ellmer</code></a></li>
</ul>
<section id="loading-data" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="loading-data"><span class="header-section-number">1.1</span> Loading data</h2>
<p>Let’s use the same data as last week to play around. If you have it from last week, no need to download, but if you need to get it, you can download the file using the code below.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Remember that files with by default be downloaded into your working directory.</p>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># download the happiness data</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/jcooperstone/dataviz-site/raw/refs/heads/master/2_04_wrangling/data/hapiscore_whr.csv"</span>,</span>
<span id="cb1-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"happiness.csv"</span>)</span></code></pre></div></div>
</div>
<p>And some data on life expectancy around the world from 1800 to predicted values from 2022-2100.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># download the life expectancy data</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/jcooperstone/dataviz-site/raw/refs/heads/master/2_04_wrangling/data/life_expectancy.csv"</span>,</span>
<span id="cb2-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life-expectancy.csv"</span>)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="get-setup-with-microsoft-copilot" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Get setup with Microsoft Copilot</h1>
<p>Open a browser and navigate to <a href="https://copilot.microsoft.com/">Microsoft Copilot</a>. Log in with your OSU credentials and indicate you are using your Work account</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E10_ai_03/img/copilot-work.png" class="img-fluid figure-img"></p>
<figcaption>Screenshot of selecting the Work Copilot experience</figcaption>
</figure>
</div>
<p>This would be the way to use generative AI that is compliant with with OSU’s data policies. You can learn more about this in OSU”s administrative resource center page on <a href="https://admin.resources.osu.edu/microsoft-365/copilot-chat?check_logged_in=1">Copilot chat</a>.</p>
</section>
<section id="get-setup-with-chattr-and-ellmer" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Get setup with <code>chattr</code> and <code>ellmer</code></h1>
<p>The package <code>chattr</code> is a chat interface to using a large language model (LLM) with R. <code>chattr</code> lets you “chat” with your LLM via your script or in a Shiny Gadget.</p>
<p>The package <code>ellmer</code> helps make it easier to use LLMs with R.</p>
<section id="install" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="install"><span class="header-section-number">3.1</span> Install</h2>
<p>First we will install both packages</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chattr"</span>)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ellmer"</span>)</span></code></pre></div></div>
</div>
<p>Then we need to tell <code>chattr</code> which LLM we want to use. <code>ellmer</code> helps us do this, and at the time of writing this tutorial, it <a href="https://ellmer.tidyverse.org/#providers">supports lots of different models</a>:</p>
<ul>
<li>Anthropic’s Claude: <code>chat_anthropic()</code></li>
<li>AWS Bedrock: <code>chat_aws_bedrock()</code></li>
<li>Azure OpenAI: <code>chat_azure_openai()</code></li>
<li>Cloudflare: <code>chat_cloudflare()</code></li>
<li>Databricks: <code>chat_databricks()</code></li>
<li>DeepSeek: <code>chat_deepseek()</code></li>
<li>GitHub model marketplace: <code>chat_github()</code>.</li>
<li>Google Gemini/Vertex AI: <code>chat_google_gemini()</code>, <code>chat_google_vertex().</code></li>
<li>Groq: <code>chat_groq()</code></li>
<li>Hugging Face: <code>chat_huggingface()</code></li>
<li>Mistral: <code>chat_mistral()</code></li>
<li>Ollama: <code>chat_ollama()</code></li>
<li>OpenAI: <code>chat_openai()</code></li>
<li>OpenRouter: <code>chat_openrouter()</code></li>
<li>perplexity.ai: <code>chat_perplexity()</code></li>
<li>Snowflake Cortex: <code>chat_snowflake()</code> and <code>c</code>hat_cortex_analyst()`</li>
<li>VLLM: <code>chat_vllm()</code></li>
</ul>
<p>You can pick which one you’d like to use, for the rest of this session I am going to use Google Gemini since I have a google/gmail account (as assume many of you do too), and their free tier is generous. I am including instructions on how to get your Google Gemini API key, but if you want to use a different model you can search how to find that API key.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Remember, your API key is personal so do not share it in repositories or elsewhere.</p>
</div>
</div>
</section>
<section id="set-up-your-api-key" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="set-up-your-api-key"><span class="header-section-number">3.2</span> Set up your API key</h2>
<p>To get your Google API key, go to <a href="https://aistudio.google.com/app/api-keys">https://aistudio.google.com/app/api-keys</a> and sign in with your Google credentials.</p>
<p>I’m showing a screenshot below of what my Google AI studio looks like, yours may be slightly different and you want to create an API key if you don’t have one.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E10_ai_03/img/google-api.png" class="img-fluid figure-img"></p>
<figcaption>Google AI studio for finding your API key</figcaption>
</figure>
</div>
<p>Then, you can click on the little double box to copy your API code. Your key code will be in your clipboard.</p>
<p><img src="https://osu-codeclub.github.io/posts/S10E10_ai_03/img/copy-api.png" class="img-fluid"></p>
<p>Now let’s set our API key. We can do that in R like this:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for Gemini</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.setenv</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GOOGLE_API_KEY =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"YOUR_GOOGLE_API_KEY"</span>)</span></code></pre></div></div>
</div>
<p>Again - if you have this in your scripts be sure then not to share them with anyone.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>There is a more elegant way to do this by setting up our API key in our R environment <code>.Renviron</code> file but I’m not going to set that up until we decide we like this :)</p>
</div>
</div>
</section>
<section id="use-chattr" class="level2" data-number="3.3">
<h2 data-number="3.3" class="anchored" data-anchor-id="use-chattr"><span class="header-section-number">3.3</span> Use <code>chattr</code></h2>
<p>Now we are set up to launch our chat. First we need to load our packages.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(chattr)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ellmer)</span></code></pre></div></div>
</div>
<p>Then we will tell <code>chattr</code> which LLM we want to use, with <code>ellmer</code> helping us:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chattr_use</span>(ellmer<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chat_google_gemini</span>())</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Using model = "gemini-2.5-flash".


── chattr 

• Provider: Google/Gemini

• Model: gemini-2.5-flash

• Label: gemini-2.5-flash (Google/Gemini)</code></pre>
</div>
</div>
<p>Let’s see how well that worked:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check our settings</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chattr_defaults</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── chattr ──────────────────────────────────────────────────────────────────────</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Defaults for: Default ──</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Prompt: </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Model </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>• Provider: Google/Gemini</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>• Model: gemini-2.5-flash</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>• Label: gemini-2.5-flash (Google/Gemini)</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code></code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Context: </code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Max Data Files: 0</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Max Data Frames: 0</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>✖ Chat History</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>✖ Document contents</code></pre>
</div>
</div>
<p>Now we can launch our chat app.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chattr_app</span>()</span></code></pre></div></div>
</div>
</section>
</section>
<section id="lets-ideate-together-what-guidance-we-want-to-provide-our-llm-for-helping-us-with-code." class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Let’s ideate together what guidance we want to provide our LLM for helping us with code.</h1>
</section>
<section id="tasks-to-play-around-with" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> Tasks to play around with</h1>
<p>Let’s together work on writing prompts to get R to write to complete each tasks or that answer the following questions:</p>
<ol type="1">
<li><p>Get R to read in your happiness and life expectancy data</p></li>
<li><p>Understand what your data contains</p></li>
<li><p>Which is the country with the highest life expectancy in 2025?</p></li>
<li><p>Which country increased life expectancy the most from 2000 to 2025?</p></li>
<li><p>Create a plot that shows the line expectancy in the United States over the time period for which we have data</p></li>
<li><p>Create a plot that shows the relationship between life expectancy and happiness score in 2022.</p></li>
</ol>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-basics</category>
  <category>tidyverse</category>
  <category>github-copilot</category>
  <category>chattr</category>
  <category>ellmer</category>
  <category>copilot</category>
  <category>gen-ai</category>
  <category>ggplot2</category>
  <guid>https://osu-codeclub.github.io/posts/S10E10_ai_03/</guid>
  <pubDate>Tue, 11 Nov 2025 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E10_ai_03/img/chattr-ellmer-hex.png" medium="image" type="image/png" height="88" width="144"/>
</item>
<item>
  <title>Wrangling and plotting with Copilot</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E09_ai_ggplot_02/</link>
  <description><![CDATA[ 




<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E09_ai_ggplot_02/img/github-copilot.jpeg" class="img-fluid figure-img"></p>
<figcaption>GitHub Copilot Logo</figcaption>
</figure>
</div>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>We are going to continue playing around today with wrangling and plotting with the help of GitHub Copilot in RStudio.</p>
<section id="loading-data" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="loading-data"><span class="header-section-number">1.1</span> Loading data</h2>
<p>I want to pick some data that has a bunch of dimensions so we can ask some interesting questions. Let’s use a dataset we’ve used before, <code>gapminder</code> and I’ve created some files for you to download.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Remember that files with by default be downloaded into your working directory.</p>
</div>
</div>
<p>Here is some code to download a file that includes information about the happiness score for each country in the world from 2005-2022.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># download the happiness data</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/jcooperstone/dataviz-site/raw/refs/heads/master/2_04_wrangling/data/hapiscore_whr.csv"</span>,</span>
<span id="cb1-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"happiness.csv"</span>)</span></code></pre></div></div>
</div>
<p>And some data on life expectancy around the world from 1800 to predicted values from 2022-2100.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># download the life expectancy data</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://github.com/jcooperstone/dataviz-site/raw/refs/heads/master/2_04_wrangling/data/life_expectancy.csv"</span>,</span>
<span id="cb2-3">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"life-expectancy.csv"</span>)</span></code></pre></div></div>
</div>
</section>
</section>
<section id="tasks-to-play-around-with" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Tasks to play around with</h1>
<p>Let’s together work on writing prompts to get R to write to complete each tasks or that answer the following questions:</p>
<ol type="1">
<li><p>Get R to read in your happiness and life expectancy data</p></li>
<li><p>Understand what your data contains</p></li>
<li><p>Which is the country with the highest life expectancy in 2025?</p></li>
<li><p>Which country increased life expectancy the most from 2000 to 2025?</p></li>
<li><p>Create a plot that shows the line expectancy in the United States over the time period for which we have data</p></li>
<li><p>Create a plot that shows the relationship between life expectancy and happiness score in 2022.</p></li>
</ol>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-basics</category>
  <category>tidyverse</category>
  <category>github-copilot</category>
  <category>ggplot2</category>
  <category>gen-ai</category>
  <guid>https://osu-codeclub.github.io/posts/S10E09_ai_ggplot_02/</guid>
  <pubDate>Mon, 03 Nov 2025 05:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E09_ai_ggplot_02/img/github-copilot.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Plotting with GitHub Copilot</title>
  <dc:creator>Horacio Lopez-Nicora</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E08_ai_ggplot_01/</link>
  <description><![CDATA[ 




<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<p>If you’ve joined previous Code Club sessions on plotting with ggplot2, you’ve already seen how powerful and flexible this package can be for data visualization in R. Today, we’ll take a different approach: instead of writing every line of ggplot2 code ourselves, we’ll let GitHub Copilot help us get started—even if you’ve never used ggplot2 before.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://allisonhorst.com/allison-horst"><img src="https://osu-codeclub.github.io/posts/S10E08_ai_ggplot_01/img/ggplot2_Horst.png" class="img-fluid figure-img"></a></p>
<figcaption>Figure from Allison Horst</figcaption>
</figure>
</div>
<p>If you’d like to review or catch up on the earlier Code Club sessions that introduced ggplot2, check out these resources:</p>
<ul>
<li><a href="https://osu-codeclub.github.io/posts/S09E01_ggplot_01/">Introduction to ggplot2 - 01</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S09E02_ggplot_02/">Introduction to ggplot2 - 02</a></li>
<li><a href="https://osu-codeclub.github.io/posts/S09E03_ggplot_03/">Introduction to ggplot2 - 03 &amp; 04</a></li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load required packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(palmerpenguins)</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load datasets</span></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(mtcars)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(iris)</span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data</span>(penguins)</span></code></pre></div></div>
</div>
</section>
<section id="data-inspection" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="data-inspection"><span class="header-section-number">2</span> Data Inspection</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">mtcars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span>
<span id="cb2-2"></span>
<span id="cb2-3">iris <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span>
<span id="cb2-4"></span>
<span id="cb2-5">palmerpenguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span></code></pre></div></div>
</div>
</section>
<section id="session-overview" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="session-overview"><span class="header-section-number">3</span> Session Overview</h2>
<p>In this Code Club session, you will explore how to use GitHub Copilot inside RStudio to develop and customize data visualizations in R. Using a series of 15 progressively complex prompts, you will guide Copilot to generate code that produces bar plots, scatterplots, boxplots, violin plots, line charts, and faceted graphics. Starting with simple datasets like mtcars and iris and finishing with real-world examples from the palmerpenguins package, you’ll see how Copilot interprets natural-language instructions to suggest ggplot2 code. The goal is to understand how to collaborate with Copilot—refining prompts, comparing different visual outputs, and learning efficient ways to produce publication-quality figures directly in RStudio.</p>
<table class="caption-top table">
<colgroup>
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 12%">
<col style="width: 50%">
<col style="width: 12%">
</colgroup>
<thead>
<tr class="header">
<th>#</th>
<th>Dataset</th>
<th>Graph Type</th>
<th>Copilot Prompt</th>
<th>Key Concept</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>mtcars</td>
<td>Barplot</td>
<td>“Create a barplot showing the number of cars for each cylinder in mtcars.”</td>
<td>Basic categorical plot</td>
</tr>
<tr class="even">
<td>2</td>
<td>iris</td>
<td>Scatterplot</td>
<td>“Plot Sepal.Length vs Sepal.Width as points from the iris dataset.”</td>
<td>Continuous vs continuous</td>
</tr>
<tr class="odd">
<td>3</td>
<td>mtcars</td>
<td>Barplot with color</td>
<td>“Make a bar plot showing cars by cylinder, colored by gears, with minimal theme.”</td>
<td>Grouped bars + theme</td>
</tr>
<tr class="even">
<td>4</td>
<td>iris</td>
<td>Scatterplot with groups</td>
<td>“Create a scatterplot of Sepal.Length vs Sepal.Width colored by Species, with linear regression lines.”</td>
<td>Color + regression</td>
</tr>
<tr class="odd">
<td>5</td>
<td>iris</td>
<td>Boxplot</td>
<td>“Draw a boxplot of Sepal.Length for each iris species with a white background theme.”</td>
<td>Distribution + theme</td>
</tr>
<tr class="even">
<td>6</td>
<td>iris</td>
<td>Violin + jitter</td>
<td>“Create a violin plot of Sepal.Length by Species with individual points overlaid.”</td>
<td>Combined geoms</td>
</tr>
<tr class="odd">
<td>7</td>
<td>mtcars</td>
<td>Line graph</td>
<td>“Plot average mpg by cylinder as a line chart with points.”</td>
<td>Summarization + line</td>
</tr>
<tr class="even">
<td>8</td>
<td>mtcars</td>
<td>Scatterplot with labels</td>
<td>“Scatterplot of mpg vs weight, color by cylinders, label each point with car names.”</td>
<td>Labeling data points</td>
</tr>
<tr class="odd">
<td>9</td>
<td>iris</td>
<td>Facet plot</td>
<td>“Scatterplot of Sepal.Length vs Sepal.Width with separate panels for each Species.”</td>
<td>Faceting</td>
</tr>
<tr class="even">
<td>10</td>
<td>iris</td>
<td>Multi-feature plot</td>
<td>“Make a scatterplot of Sepal.Length vs Sepal.Width, color by Species, add regression line, facet by Species, apply minimal theme.”</td>
<td>Layered plot</td>
</tr>
<tr class="odd">
<td>11</td>
<td>iris</td>
<td>Custom colors &amp; theme</td>
<td>“Create a boxplot of Sepal.Length by Species, use custom fill colors, and theme_classic().”</td>
<td>Manual color scales</td>
</tr>
<tr class="even">
<td>12</td>
<td>mtcars/iris</td>
<td>Challenge</td>
<td>“Make a publication-ready plot: color by group, add labels, add regression or summary line, apply a nice theme.”</td>
<td>Integration + polish</td>
</tr>
<tr class="odd">
<td>13</td>
<td>penguins</td>
<td>Scatterplot with multiple aesthetics</td>
<td>“Plot flipper_length_mm vs body_mass_g, color by species, shape by island, size by bill_length_mm.”</td>
<td>Multiple aesthetics</td>
</tr>
<tr class="even">
<td>14</td>
<td>penguins</td>
<td>Faceted scatterplot</td>
<td>“Scatterplot of flipper_length_mm vs body_mass_g, color by species, facet by island.”</td>
<td>Facets + grouping</td>
</tr>
<tr class="odd">
<td>15</td>
<td>penguins</td>
<td>Advanced composite</td>
<td>“Create a scatterplot of flipper_length_mm vs body_mass_g, color by species, add regression lines, facet by island, and apply a clean theme.”</td>
<td>Complex layering</td>
</tr>
</tbody>
</table>
</section>
<section id="notes-for-participants" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="notes-for-participants"><span class="header-section-number">4</span> Notes for Participants</h2>
<ul>
<li>Copy each prompt into RStudio and let <strong>Copilot generate the code</strong>.</li>
<li>Experiment with <strong>themes, colors, point sizes, and labels</strong>.</li>
<li>Work sequentially: <code>mtcars</code> → <code>iris</code> → <code>penguins</code>.</li>
<li>Advanced plots: combine <strong>color, shape, size, regression lines, and facets</strong>.</li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>tidyverse</category>
  <category>github-copilot</category>
  <category>gen-ai</category>
  <guid>https://osu-codeclub.github.io/posts/S10E08_ai_ggplot_01/</guid>
  <pubDate>Mon, 27 Oct 2025 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E08_ai_ggplot_01/img/ggplot2_Horst.png" medium="image" type="image/png" height="86" width="144"/>
</item>
<item>
  <title>Data wrangling with GitHub Copilot - I</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E07_ai_filter-select_02/</link>
  <description><![CDATA[ 




<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E07_ai_filter-select_02/img/github-copilot.jpeg" class="img-fluid figure-img"></p>
<figcaption>GitHub Copilot Logo</figcaption>
</figure>
</div>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>We are going to spend the next handful of Code Club sessions learning how to code with the help of generative AI. We have decided to structure these lessons by providing you a little bit of context about some functions that can be used to achieve a task, and then will spend the next session playing around with these functions using <a href="https://docs.posit.co/ide/user/ide/guide/tools/copilot.html">GitHub Copilot which can be integrated into RStudio</a>.</p>
<p>Existing and soon-to-come generative AI tools are powerful but when you don’t need to generate the code, its easy to get results that are not intended. We want to help provide you some tools to enable you to use these new tools but not accidentally lose a finger.</p>
</section>
<section id="ways-to-use-generative-ai-for-coding-in-r" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Ways to use generative AI for coding in R</h1>
<p>Here are some common ways to use generative AI for coding assistance in R (this list is not exhaustive):</p>
<ul>
<li>GitHub Copilot through the RStudio IDE</li>
<li>ChatGPT through an package like <code>chattr</code> - a package developed and maintained by Posit (the company that makes RStudio). You will need your OpenAI API key to do this.</li>
<li>Using a generative AI agent in your browser while you code - this method could be paired with Microsoft Copilot using your OSU credentials to be compliance with OSU data handling.</li>
</ul>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Warning</span>Know what type of data you are using before enabling Copilot in RStudio
</div>
</div>
<div class="callout-body-container callout-body">
<p>Using a generative AI agent can have broad implications for you. There may be instances where this is strictly unallowable. I encourage you to have a conversation with your supervisor/instructor/responsibile party about what implications there might be for use of AI with your work.</p>
</div>
</div>
<p>We are going to get started by using GitHub Copilot which is integrated into the RStudio IDE.</p>
</section>
<section id="getting-started-with-github-copilot" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Getting started with GitHub Copilot</h1>
<section id="create-a-github-account" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="create-a-github-account"><span class="header-section-number">3.1</span> Create a GitHub account</h2>
<p>In order to use GitHub Copilot, you must have a GitHub account.</p>
<p>Create a GitHub account by going to <a href="www.github.com/join">github.com/join</a>.</p>
<ul>
<li>You will have to link to an email address. You can use your OSU email or a personal one, it doesn’t matter.</li>
<li>You will have to pick a username. Some <a href="https://happygitwithr.com/github-acct.html">advice for picking a username</a>:
<ul>
<li>Incorporate your actual name - it’s useful for seeing who you are</li>
<li>Pick a username you’d be comfortable with an employer seeing</li>
<li>Shorter is better</li>
<li>Recommend to use all lower case letters</li>
<li>Your username can be changed but its annoying so try and get it right the first time</li>
</ul></li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>You can sign up for GitHub education with your OSU email by confirming your status as a student <a href="https://github.com/education">GitHub Education page</a>.</p>
</div>
</div>
<p>You have access to Copilot free with your GitHub account.</p>
</section>
<section id="enable-github-copilot-in-rstudio" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="enable-github-copilot-in-rstudio"><span class="header-section-number">3.2</span> Enable GitHub Copilot in RStudio</h2>
<p>Now we will enable GitHub Copilot in RStudio. We can do that by navigating to <code>Tools</code> &gt; <code>Global Options</code> &gt; <code>Copilot</code>. Click “Enable Copilot”, sign into GitHub, and use the verification code provided.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>RStudio Desktop 2023.09.0 or higher is required. You will need internet to access the API.</p>
</div>
</div>
<p><img src="https://osu-codeclub.github.io/posts/S10E07_ai_filter-select_02/img/enable-copilot.png" class="img-fluid"></p>
<p>You can see above I also have not “Index project files with GitHub Copilot”. I have done this because I don’t want all my files in this Rproject to be read by Copilot. For code club, it would probably be ok to index, but <strong>I would not check this for my research</strong>.</p>
<section id="autocomplete-vs.-copilot" class="level3" data-number="3.2.1">
<h3 data-number="3.2.1" class="anchored" data-anchor-id="autocomplete-vs.-copilot"><span class="header-section-number">3.2.1</span> Autocomplete vs.&nbsp;Copilot</h3>
</section>
</section>
<section id="try-but-verify" class="level2" data-number="3.3">
<h2 data-number="3.3" class="anchored" data-anchor-id="try-but-verify"><span class="header-section-number">3.3</span> Try but verify</h2>
<p>Posit (the company that makes RStudio) recommends a <a href="https://docs.posit.co/ide/user/ide/guide/tools/copilot.html#using-copilot">“trust but verify”</a> approach. They say specifically:</p>
<blockquote class="blockquote">
<p>“While Copilot often generates useful and functional code, it is important to note that the suggestions are not always valid code examples or completely solve the intended problem. Copilot suggestions are non-deterministic and Posit does not guarantee the quality, accuracy, or security of the outputs. It is important to review the suggestions and ensure that they are both accurate and appropriate for the intended use case. Copilot may generate code that contains insecure coding patterns, bugs, or outdated practices. You should always use best practices in code review, testing, and adherence to security standards when using Copilot. For more information, see the <a href="https://github.com/features/copilot#faq">FAQs on GitHub Copilot</a> and <a href="https://docs.github.com/en/copilot/get-started/what-is-github-copilot">GitHub Copilot for Business</a>.”</p>
</blockquote>
<p>I might recommend instead a “try but verify” approach - where you utilize Copilot but do your due diligence to check each thing it does to ensure you are getting your intended outcome. In the end, if you are using something to complete <strong>your</strong> work, you need to be able to stand behind it.</p>
</section>
<section id="asking-questions" class="level2" data-number="3.4">
<h2 data-number="3.4" class="anchored" data-anchor-id="asking-questions"><span class="header-section-number">3.4</span> Asking questions</h2>
<p>By using comments in a chunk, you can ask questions to Copilot.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># q: how do i calculate the mean of a column in a data frame?</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a: mean(mtcars$mpg) </span></span></code></pre></div></div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Remember that if you have Copilot set to give you suggestions manually, you’ll need to click <code>Ctrl</code> or <code>Cmd</code> + <code>\</code> to get a response.</p>
</div>
</div>
</section>
<section id="comments" class="level2" data-number="3.5">
<h2 data-number="3.5" class="anchored" data-anchor-id="comments"><span class="header-section-number">3.5</span> Comments</h2>
<p>You can also prompt Copilot for help by describing in comments what you want to do.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># select only the numeric variables from my data frame</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a: mtcars_numeric &lt;- dplyr::select_if(mtcars, is.numeric)</span></span></code></pre></div></div>
</div>
</section>
<section id="other-things-to-discuss" class="level2" data-number="3.6">
<h2 data-number="3.6" class="anchored" data-anchor-id="other-things-to-discuss"><span class="header-section-number">3.6</span> Other things to discuss</h2>
<p>AI and destroying the environment - my recommendation is to turn Copilot on only when you want to use it</p>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-basics</category>
  <category>tidyverse</category>
  <category>github-copilot</category>
  <category>gen-ai</category>
  <guid>https://osu-codeclub.github.io/posts/S10E07_ai_filter-select_02/</guid>
  <pubDate>Mon, 20 Oct 2025 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E07_ai_filter-select_02/img/github-copilot.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>GenAI prep: data wrangling with filter, select, and arrange</title>
  <dc:creator>Jessica Cooperstone</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E06_ai_filter-select-basics_01/</link>
  <description><![CDATA[ 




<hr>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E06_ai_filter-select-basics_01/img/go-wrangling.png" class="img-fluid figure-img"></p>
<figcaption>Artwork by <a href="https://twitter.com/allison_horst">@allison_horst</a></figcaption>
</figure>
</div>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>We are going to spend the next handful of Code Club sessions learning how to code with the help of generative AI. We have decided to structure these lessons by providing you a little bit of context about some functions that can be used to achieve a task, and then will spend the next session playing around with these functions using <a href="https://docs.posit.co/ide/user/ide/guide/tools/copilot.html">GitHub Copilot which can be integrated into RStudio</a>.</p>
<p>We are taking this approach so we are not starting using generative AI without any knowledge of our task or functions, so we can better see how its working and how we can make it work for us.</p>
<p>Existing and soon-to-come generative AI tools are powerful but when you don’t need to generate the code, its easy to get results that are not intended. We want to help provide you some tools to enable you to use these new tools but not accidentally lose a finger.</p>
<p>We won’t be using Github Copilot today but we will use it next week.</p>
<section id="why-do-we-wrangle-our-data" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="why-do-we-wrangle-our-data"><span class="header-section-number">1.1</span> Why do we wrangle our data?</h2>
<blockquote class="blockquote">
<p>“Yet far too much handcrafted work — what data scientists call “data wrangling,” “data munging” and “data janitor work” — is still required. Data scientists, according to interviews and expert estimates, spend from 50 percent to 80 percent of their time mired in this more mundane labor of collecting and preparing unruly digital data, before it can be explored for useful nuggets.” - <a href="https://www.nytimes.com/2014/08/18/technology/for-big-data-scientists-hurdle-to-insights-is-janitor-work.html">For Big-Data Scientists, ‘Janitor Work’ Is Key Hurdle to Insights, NY Times</a></p>
</blockquote>
<section id="poll---who-is-new-to-r" class="level3" data-number="1.1.1">
<h3 data-number="1.1.1" class="anchored" data-anchor-id="poll---who-is-new-to-r"><span class="header-section-number">1.1.1</span> Poll - who is new to R?</h3>
<p>If you don’t have R and RStudio on your computer, you can find instructions for installation <a href="https://osu-codeclub.github.io/pages/setup.html">here</a>. You can also find some additional introductory material on getting set up in RStudio <a href="https://osu-codeclub.github.io/posts/S07E01_basics_01/">here</a>.</p>
</section>
</section>
<section id="load-packages" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="load-packages"><span class="header-section-number">1.2</span> Load packages</h2>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("tidyverse") # i have version 2.0.0</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("janitor") # i have version 2.2.1</span></span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for wrangling</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (&lt;http://conflicted.r-lib.org/&gt;) to force all conflicts to become errors</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(janitor) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for cleaning up column names</span></span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test</code></pre>
</div>
</div>
</section>
<section id="data" class="level2" data-number="1.3">
<h2 data-number="1.3" class="anchored" data-anchor-id="data"><span class="header-section-number">1.3</span> Data</h2>
<p>We are going to use data from <a href="https://www.cia.gov/the-world-factbook/">The World Factbook</a>, put together by the CIA to “provides basic intelligence on the history, people, government, economy, energy, geography, environment, communications, transportation, military, terrorism, and transnational issues for 265 world entities.” I thought this data would give us some opportunities to flex our R skills, and learn a bit about the world.</p>
<p>The data we are going to download can be found <a href="https://www.cia.gov/the-world-factbook/field/population/country-comparison/">here</a>, though I have saved the file, added it to our Code Club Github, and included some code below for you to download it.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">download.file</span>(</span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">url =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://raw.githubusercontent.com/osu-codeclub/osu-codeclub.github.io/main/posts/S08E01_wrangling_01/data/factbook-2015.csv"</span>,</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">destfile =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factbook_download_2015.csv"</span></span>
<span id="cb5-4">)</span></code></pre></div></div>
</div>
<p>You should now see the file “factbook_download_2015.csv” in your working directory.</p>
<p>We can read it in using the tidyverse function from the <a href="https://readr.tidyverse.org/index.html"><code>readr</code></a> package called <a href="https://readr.tidyverse.org/reference/read_delim.html"><code>read_csv()</code></a>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">factbook_2015 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"factbook_download_2015.csv"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stderr">
<pre><code>Rows: 217 Columns: 53
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (2): Country Name, Country Code
dbl (51): Population, total, Population growth (annual %), Surface area (sq....

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.</code></pre>
</div>
</div>
<p>Let’s get a better handle on this data. We can use the function <a href="https://dplyr.tidyverse.org/reference/glimpse.html"><code>glimpse()</code></a> to get a “glimpse” at our data.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(factbook_2015)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code>Rows: 217
Columns: 53
$ `Country Name`                                                                                     &lt;chr&gt; …
$ `Country Code`                                                                                     &lt;chr&gt; …
$ `Population, total`                                                                                &lt;dbl&gt; …
$ `Population growth (annual %)`                                                                     &lt;dbl&gt; …
$ `Surface area (sq. km)`                                                                            &lt;dbl&gt; …
$ `Poverty headcount ratio at national poverty lines (% of population)`                              &lt;dbl&gt; …
$ `GNI, Atlas method (current US$)`                                                                  &lt;dbl&gt; …
$ `GNI per capita, Atlas method (current US$)`                                                       &lt;dbl&gt; …
$ `GNI, PPP (current international $)`                                                               &lt;dbl&gt; …
$ `GNI per capita, PPP (current international $)`                                                    &lt;dbl&gt; …
$ `Income share held by lowest 20%`                                                                  &lt;dbl&gt; …
$ `Life expectancy at birth, total (years)`                                                          &lt;dbl&gt; …
$ `Fertility rate, total (births per woman)`                                                         &lt;dbl&gt; …
$ `Adolescent fertility rate (births per 1,000 women ages 15-19)`                                    &lt;dbl&gt; …
$ `Contraceptive prevalence, any method (% of married women ages 15-49)`                             &lt;dbl&gt; …
$ `Births attended by skilled health staff (% of total)`                                             &lt;dbl&gt; …
$ `Mortality rate, under-5 (per 1,000 live births)`                                                  &lt;dbl&gt; …
$ `Prevalence of underweight, weight for age (% of children under 5)`                                &lt;dbl&gt; …
$ `Immunization, measles (% of children ages 12-23 months)`                                          &lt;dbl&gt; …
$ `Primary completion rate, total (% of relevant age group)`                                         &lt;dbl&gt; …
$ `School enrollment, secondary (% gross)`                                                           &lt;dbl&gt; …
$ `School enrollment, primary and secondary (gross), gender parity index (GPI)`                      &lt;dbl&gt; …
$ `Prevalence of HIV, total (% of population ages 15-49)`                                            &lt;dbl&gt; …
$ `Forest area (sq. km)`                                                                             &lt;dbl&gt; …
$ `Water productivity, total (constant 2015 US$ GDP per cubic meter of total freshwater withdrawal)` &lt;dbl&gt; …
$ `Energy use (kg of oil equivalent per capita)`                                                     &lt;dbl&gt; …
$ `CO2 emissions (metric tons per capita)`                                                           &lt;dbl&gt; …
$ `Electric power consumption (kWh per capita)`                                                      &lt;dbl&gt; …
$ `GDP (current US$)`                                                                                &lt;dbl&gt; …
$ `GDP growth (annual %)`                                                                            &lt;dbl&gt; …
$ `Inflation, GDP deflator (annual %)`                                                               &lt;dbl&gt; …
$ `Agriculture, forestry, and fishing, value added (% of GDP)`                                       &lt;dbl&gt; …
$ `Industry (including construction), value added (% of GDP)`                                        &lt;dbl&gt; …
$ `Exports of goods and services (% of GDP)`                                                         &lt;dbl&gt; …
$ `Imports of goods and services (% of GDP)`                                                         &lt;dbl&gt; …
$ `Gross capital formation (% of GDP)`                                                               &lt;dbl&gt; …
$ `Revenue, excluding grants (% of GDP)`                                                             &lt;dbl&gt; …
$ `Start-up procedures to register a business (number)`                                              &lt;dbl&gt; …
$ `Market capitalization of listed domestic companies (% of GDP)`                                    &lt;dbl&gt; …
$ `Military expenditure (% of GDP)`                                                                  &lt;dbl&gt; …
$ `Mobile cellular subscriptions (per 100 people)`                                                   &lt;dbl&gt; …
$ `High-technology exports (% of manufactured exports)`                                              &lt;dbl&gt; …
$ `Merchandise trade (% of GDP)`                                                                     &lt;dbl&gt; …
$ `Net barter terms of trade index (2015 = 100)`                                                     &lt;dbl&gt; …
$ `External debt stocks, total (DOD, current US$)`                                                   &lt;dbl&gt; …
$ `Total debt service (% of GNI)`                                                                    &lt;dbl&gt; …
$ `Net migration`                                                                                    &lt;dbl&gt; …
$ `Personal remittances, paid (current US$)`                                                         &lt;dbl&gt; …
$ `Foreign direct investment, net inflows (BoP, current US$)`                                        &lt;dbl&gt; …
$ `Net ODA received per capita (current US$)`                                                        &lt;dbl&gt; …
$ `GDP per capita (current US$)`                                                                     &lt;dbl&gt; …
$ `Foreign direct investment, net (BoP, current US$)`                                                &lt;dbl&gt; …
$ `Inflation, consumer prices (annual %)`                                                            &lt;dbl&gt; …</code></pre>
</div>
</div>
<p>We see that <code>Country Name</code> and <code>Country Code</code> are character columns while the others are numeric (i.e., dbl).</p>
<p>We can look at this data another way, using the function <code>head()</code> to look at the first six rows, and every column.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(factbook_2015)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 6 × 53
  `Country Name` `Country Code` `Population, total` Population growth (annual …¹
  &lt;chr&gt;          &lt;chr&gt;                        &lt;dbl&gt;                        &lt;dbl&gt;
1 Afghanistan    AFG                       33753499                        3.12 
2 Albania        ALB                        2880703                       -0.291
3 Algeria        DZA                       39543154                        2.00 
4 American Samoa ASM                          51368                       -1.64 
5 Andorra        AND                          71746                        0.174
6 Angola         AGO                       28127721                        3.62 
# ℹ abbreviated name: ¹​`Population growth (annual %)`
# ℹ 49 more variables: `Surface area (sq. km)` &lt;dbl&gt;,
#   `Poverty headcount ratio at national poverty lines (% of population)` &lt;dbl&gt;,
#   `GNI, Atlas method (current US$)` &lt;dbl&gt;,
#   `GNI per capita, Atlas method (current US$)` &lt;dbl&gt;,
#   `GNI, PPP (current international $)` &lt;dbl&gt;,
#   `GNI per capita, PPP (current international $)` &lt;dbl&gt;, …</code></pre>
</div>
</div>
<section id="cleaning-column-names" class="level3" data-number="1.3.1">
<h3 data-number="1.3.1" class="anchored" data-anchor-id="cleaning-column-names"><span class="header-section-number">1.3.1</span> Cleaning column names</h3>
<p>It looks like we have some column names that don’t use standard R practices (i.e., they have spaces, start with numbers). This isn’t a critical problem (meaning you can use column names like this - we know that because we have them here!) but it does make things slightly more difficult. The main difficulty is that you will have to refer to column names surrounded in back ticks and this can be annoying.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Note that you can access a single column in a dataframe by using the <code>$</code> (dollar sign operator). This will give you your column as a vector.</p>
</div>
</div>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look how annoying it is to refer to a column that has unconventional naming</span></span>
<span id="cb12-2">factbook_2015<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Country Name</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span></code></pre></div></div>
</div>
<p>Let’s use the function <a href=""><code>clean_names()</code></a> from the <code>janitor</code> package to clean those names up.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># old column names</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(factbook_2015)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code> [1] "Country Name"                                                                                    
 [2] "Country Code"                                                                                    
 [3] "Population, total"                                                                               
 [4] "Population growth (annual %)"                                                                    
 [5] "Surface area (sq. km)"                                                                           
 [6] "Poverty headcount ratio at national poverty lines (% of population)"                             
 [7] "GNI, Atlas method (current US$)"                                                                 
 [8] "GNI per capita, Atlas method (current US$)"                                                      
 [9] "GNI, PPP (current international $)"                                                              
[10] "GNI per capita, PPP (current international $)"                                                   
[11] "Income share held by lowest 20%"                                                                 
[12] "Life expectancy at birth, total (years)"                                                         
[13] "Fertility rate, total (births per woman)"                                                        
[14] "Adolescent fertility rate (births per 1,000 women ages 15-19)"                                   
[15] "Contraceptive prevalence, any method (% of married women ages 15-49)"                            
[16] "Births attended by skilled health staff (% of total)"                                            
[17] "Mortality rate, under-5 (per 1,000 live births)"                                                 
[18] "Prevalence of underweight, weight for age (% of children under 5)"                               
[19] "Immunization, measles (% of children ages 12-23 months)"                                         
[20] "Primary completion rate, total (% of relevant age group)"                                        
[21] "School enrollment, secondary (% gross)"                                                          
[22] "School enrollment, primary and secondary (gross), gender parity index (GPI)"                     
[23] "Prevalence of HIV, total (% of population ages 15-49)"                                           
[24] "Forest area (sq. km)"                                                                            
[25] "Water productivity, total (constant 2015 US$ GDP per cubic meter of total freshwater withdrawal)"
[26] "Energy use (kg of oil equivalent per capita)"                                                    
[27] "CO2 emissions (metric tons per capita)"                                                          
[28] "Electric power consumption (kWh per capita)"                                                     
[29] "GDP (current US$)"                                                                               
[30] "GDP growth (annual %)"                                                                           
[31] "Inflation, GDP deflator (annual %)"                                                              
[32] "Agriculture, forestry, and fishing, value added (% of GDP)"                                      
[33] "Industry (including construction), value added (% of GDP)"                                       
[34] "Exports of goods and services (% of GDP)"                                                        
[35] "Imports of goods and services (% of GDP)"                                                        
[36] "Gross capital formation (% of GDP)"                                                              
[37] "Revenue, excluding grants (% of GDP)"                                                            
[38] "Start-up procedures to register a business (number)"                                             
[39] "Market capitalization of listed domestic companies (% of GDP)"                                   
[40] "Military expenditure (% of GDP)"                                                                 
[41] "Mobile cellular subscriptions (per 100 people)"                                                  
[42] "High-technology exports (% of manufactured exports)"                                             
[43] "Merchandise trade (% of GDP)"                                                                    
[44] "Net barter terms of trade index (2015 = 100)"                                                    
[45] "External debt stocks, total (DOD, current US$)"                                                  
[46] "Total debt service (% of GNI)"                                                                   
[47] "Net migration"                                                                                   
[48] "Personal remittances, paid (current US$)"                                                        
[49] "Foreign direct investment, net inflows (BoP, current US$)"                                       
[50] "Net ODA received per capita (current US$)"                                                       
[51] "GDP per capita (current US$)"                                                                    
[52] "Foreign direct investment, net (BoP, current US$)"                                               
[53] "Inflation, consumer prices (annual %)"                                                           </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># use clean_names and save over the current df factbook_2015</span></span>
<span id="cb15-2">factbook_2015 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean_names</span>(factbook_2015)</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># new column names</span></span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(factbook_2015)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code> [1] "country_name"                                                                                
 [2] "country_code"                                                                                
 [3] "population_total"                                                                            
 [4] "population_growth_annual_percent"                                                            
 [5] "surface_area_sq_km"                                                                          
 [6] "poverty_headcount_ratio_at_national_poverty_lines_percent_of_population"                     
 [7] "gni_atlas_method_current_us"                                                                 
 [8] "gni_per_capita_atlas_method_current_us"                                                      
 [9] "gni_ppp_current_international"                                                               
[10] "gni_per_capita_ppp_current_international"                                                    
[11] "income_share_held_by_lowest_20_percent"                                                      
[12] "life_expectancy_at_birth_total_years"                                                        
[13] "fertility_rate_total_births_per_woman"                                                       
[14] "adolescent_fertility_rate_births_per_1_000_women_ages_15_19"                                 
[15] "contraceptive_prevalence_any_method_percent_of_married_women_ages_15_49"                     
[16] "births_attended_by_skilled_health_staff_percent_of_total"                                    
[17] "mortality_rate_under_5_per_1_000_live_births"                                                
[18] "prevalence_of_underweight_weight_for_age_percent_of_children_under_5"                        
[19] "immunization_measles_percent_of_children_ages_12_23_months"                                  
[20] "primary_completion_rate_total_percent_of_relevant_age_group"                                 
[21] "school_enrollment_secondary_percent_gross"                                                   
[22] "school_enrollment_primary_and_secondary_gross_gender_parity_index_gpi"                       
[23] "prevalence_of_hiv_total_percent_of_population_ages_15_49"                                    
[24] "forest_area_sq_km"                                                                           
[25] "water_productivity_total_constant_2015_us_gdp_per_cubic_meter_of_total_freshwater_withdrawal"
[26] "energy_use_kg_of_oil_equivalent_per_capita"                                                  
[27] "co2_emissions_metric_tons_per_capita"                                                        
[28] "electric_power_consumption_k_wh_per_capita"                                                  
[29] "gdp_current_us"                                                                              
[30] "gdp_growth_annual_percent"                                                                   
[31] "inflation_gdp_deflator_annual_percent"                                                       
[32] "agriculture_forestry_and_fishing_value_added_percent_of_gdp"                                 
[33] "industry_including_construction_value_added_percent_of_gdp"                                  
[34] "exports_of_goods_and_services_percent_of_gdp"                                                
[35] "imports_of_goods_and_services_percent_of_gdp"                                                
[36] "gross_capital_formation_percent_of_gdp"                                                      
[37] "revenue_excluding_grants_percent_of_gdp"                                                     
[38] "start_up_procedures_to_register_a_business_number"                                           
[39] "market_capitalization_of_listed_domestic_companies_percent_of_gdp"                           
[40] "military_expenditure_percent_of_gdp"                                                         
[41] "mobile_cellular_subscriptions_per_100_people"                                                
[42] "high_technology_exports_percent_of_manufactured_exports"                                     
[43] "merchandise_trade_percent_of_gdp"                                                            
[44] "net_barter_terms_of_trade_index_2015_100"                                                    
[45] "external_debt_stocks_total_dod_current_us"                                                   
[46] "total_debt_service_percent_of_gni"                                                           
[47] "net_migration"                                                                               
[48] "personal_remittances_paid_current_us"                                                        
[49] "foreign_direct_investment_net_inflows_bo_p_current_us"                                       
[50] "net_oda_received_per_capita_current_us"                                                      
[51] "gdp_per_capita_current_us"                                                                   
[52] "foreign_direct_investment_net_bo_p_current_us"                                               
[53] "inflation_consumer_prices_annual_percent"                                                    </code></pre>
</div>
</div>
<p>By default, <code>clean_names()</code> converts to “snake_case” (where words are separated by an underscore instead of a space).</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E06_ai_filter-select-basics_01/img/cases.png" class="img-fluid figure-img"></p>
<figcaption><a href="https://kiranvajrapu.medium.com/exploring-programming-naming-styles-85aefd54c188">Exploring Programming Naming Styles by Kiran Vajrapu</a></figcaption>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Having spaces, starting with numbers, or have unallowable characters (e.g., %, ?, !) is only a problem for column <strong>names</strong> not for cell <strong>contents</strong>.</p>
</div>
</div>
<p>What if we want to see a complete list of our countries? We could run the following code to see that:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">factbook_2015<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>country_name</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code>  [1] "Afghanistan"                    "Albania"                       
  [3] "Algeria"                        "American Samoa"                
  [5] "Andorra"                        "Angola"                        
  [7] "Antigua and Barbuda"            "Argentina"                     
  [9] "Armenia"                        "Aruba"                         
 [11] "Australia"                      "Austria"                       
 [13] "Azerbaijan"                     "Bahamas, The"                  
 [15] "Bahrain"                        "Bangladesh"                    
 [17] "Barbados"                       "Belarus"                       
 [19] "Belgium"                        "Belize"                        
 [21] "Benin"                          "Bermuda"                       
 [23] "Bhutan"                         "Bolivia"                       
 [25] "Bosnia and Herzegovina"         "Botswana"                      
 [27] "Brazil"                         "British Virgin Islands"        
 [29] "Brunei Darussalam"              "Bulgaria"                      
 [31] "Burkina Faso"                   "Burundi"                       
 [33] "Cabo Verde"                     "Cambodia"                      
 [35] "Cameroon"                       "Canada"                        
 [37] "Cayman Islands"                 "Central African Republic"      
 [39] "Chad"                           "Channel Islands"               
 [41] "Chile"                          "China"                         
 [43] "Colombia"                       "Comoros"                       
 [45] "Congo, Dem. Rep."               "Congo, Rep."                   
 [47] "Costa Rica"                     "Cote d'Ivoire"                 
 [49] "Croatia"                        "Cuba"                          
 [51] "Curacao"                        "Cyprus"                        
 [53] "Czechia"                        "Denmark"                       
 [55] "Djibouti"                       "Dominica"                      
 [57] "Dominican Republic"             "Ecuador"                       
 [59] "Egypt, Arab Rep."               "El Salvador"                   
 [61] "Equatorial Guinea"              "Eritrea"                       
 [63] "Estonia"                        "Ethiopia"                      
 [65] "Faroe Islands"                  "Fiji"                          
 [67] "Finland"                        "France"                        
 [69] "French Polynesia"               "Gabon"                         
 [71] "Gambia, The"                    "Georgia"                       
 [73] "Germany"                        "Ghana"                         
 [75] "Gibraltar"                      "Greece"                        
 [77] "Greenland"                      "Grenada"                       
 [79] "Guam"                           "Guatemala"                     
 [81] "Guinea"                         "Guinea-Bissau"                 
 [83] "Guyana"                         "Haiti"                         
 [85] "Honduras"                       "Hong Kong SAR, China"          
 [87] "Hungary"                        "Iceland"                       
 [89] "India"                          "Indonesia"                     
 [91] "Iran, Islamic Rep."             "Iraq"                          
 [93] "Ireland"                        "Isle of Man"                   
 [95] "Israel"                         "Italy"                         
 [97] "Jamaica"                        "Japan"                         
 [99] "Jordan"                         "Kazakhstan"                    
[101] "Kenya"                          "Kiribati"                      
[103] "Korea, Dem. People's Rep."      "Korea, Rep."                   
[105] "Kosovo"                         "Kuwait"                        
[107] "Kyrgyz Republic"                "Lao PDR"                       
[109] "Latvia"                         "Lebanon"                       
[111] "Lesotho"                        "Liberia"                       
[113] "Libya"                          "Liechtenstein"                 
[115] "Lithuania"                      "Luxembourg"                    
[117] "Macao SAR, China"               "North Macedonia"               
[119] "Madagascar"                     "Malawi"                        
[121] "Malaysia"                       "Maldives"                      
[123] "Mali"                           "Malta"                         
[125] "Marshall Islands"               "Mauritania"                    
[127] "Mauritius"                      "Mexico"                        
[129] "Micronesia, Fed. Sts."          "Moldova"                       
[131] "Monaco"                         "Mongolia"                      
[133] "Montenegro"                     "Morocco"                       
[135] "Mozambique"                     "Myanmar"                       
[137] "Namibia"                        "Nauru"                         
[139] "Nepal"                          "Netherlands"                   
[141] "New Caledonia"                  "New Zealand"                   
[143] "Nicaragua"                      "Niger"                         
[145] "Nigeria"                        "Northern Mariana Islands"      
[147] "Norway"                         "Oman"                          
[149] "Pakistan"                       "Palau"                         
[151] "Panama"                         "Papua New Guinea"              
[153] "Paraguay"                       "Peru"                          
[155] "Philippines"                    "Poland"                        
[157] "Portugal"                       "Puerto Rico"                   
[159] "Qatar"                          "Romania"                       
[161] "Russian Federation"             "Rwanda"                        
[163] "Samoa"                          "San Marino"                    
[165] "Sao Tome and Principe"          "Saudi Arabia"                  
[167] "Senegal"                        "Serbia"                        
[169] "Seychelles"                     "Sierra Leone"                  
[171] "Singapore"                      "Sint Maarten (Dutch part)"     
[173] "Slovak Republic"                "Slovenia"                      
[175] "Solomon Islands"                "Somalia"                       
[177] "South Africa"                   "South Sudan"                   
[179] "Spain"                          "Sri Lanka"                     
[181] "St. Kitts and Nevis"            "St. Lucia"                     
[183] "St. Martin (French part)"       "St. Vincent and the Grenadines"
[185] "Sudan"                          "Suriname"                      
[187] "Eswatini"                       "Sweden"                        
[189] "Switzerland"                    "Syrian Arab Republic"          
[191] "Tajikistan"                     "Tanzania"                      
[193] "Thailand"                       "Timor-Leste"                   
[195] "Togo"                           "Tonga"                         
[197] "Trinidad and Tobago"            "Tunisia"                       
[199] "Turkiye"                        "Turkmenistan"                  
[201] "Turks and Caicos Islands"       "Tuvalu"                        
[203] "Uganda"                         "Ukraine"                       
[205] "United Arab Emirates"           "United Kingdom"                
[207] "United States"                  "Uruguay"                       
[209] "Uzbekistan"                     "Vanuatu"                       
[211] "Venezuela, RB"                  "Viet Nam"                      
[213] "Virgin Islands (U.S.)"          "West Bank and Gaza"            
[215] "Yemen, Rep."                    "Zambia"                        
[217] "Zimbabwe"                      </code></pre>
</div>
</div>
</section>
</section>
</section>
<section id="pick-observations-with-filter" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Pick observations with <code>filter()</code></h1>
<div class="cell" data-layout-align="center">
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://osu-codeclub.github.io/posts/S10E06_ai_filter-select-basics_01/img/filter.png" class="img-fluid figure-img" style="width:70.0%" alt="Cartoon showing three fuzzy monsters either selecting or crossing out rows of a data table. If the type of animal in the table is “otter” and the site is “bay”, a monster is drawing a purple rectangle around the row. If those conditions are not met, another monster is putting a line through the column indicating it will be excluded. Stylized text reads “dplyr::filter() - keep rows that satisfy your conditions.” Learn more about dplyr::filter."></p>
<figcaption>Figure from <a href="https://github.com/allisonhorst/stats-illustrations">Allison Horst</a></figcaption>
</figure>
</div>
</div>
</div>
<p>Sometimes you want to select observations (rows) based on values. To do this you use <a href="https://dplyr.tidyverse.org/reference/filter.html"><code>filter()</code></a>. Try not to confuse this with <code>select()</code>.</p>
<div class="callout callout-style-default callout-note callout-empty-content callout-titled" title="`select()` picks columns, while `filter()` picks rows.">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span><code>select()</code> picks columns, while <code>filter()</code> picks rows.
</div>
</div>
<div class="callout-body-container callout-body">

</div>
</div>
<p>The function <code>filter()</code> will keep only observations that meet your filtering criteria.</p>
<p>Let’s filter for countries that have a population more than Ohio (11.76M).</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb19-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(population_total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11760000</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 73 × 53
   country_name country_code population_total population_growth_annual_percent
   &lt;chr&gt;        &lt;chr&gt;                   &lt;dbl&gt;                            &lt;dbl&gt;
 1 Afghanistan  AFG                  33753499                            3.12 
 2 Algeria      DZA                  39543154                            2.00 
 3 Angola       AGO                  28127721                            3.62 
 4 Argentina    ARG                  43131966                            1.08 
 5 Australia    AUS                  23815995                            1.44 
 6 Bangladesh   BGD                 157830000                            1.19 
 7 Brazil       BRA                 205188205                            0.846
 8 Burkina Faso BFA                  18718019                            2.97 
 9 Cambodia     KHM                  15417523                            1.35 
10 Cameroon     CMR                  23012646                            3.15 
# ℹ 63 more rows
# ℹ 49 more variables: surface_area_sq_km &lt;dbl&gt;,
#   poverty_headcount_ratio_at_national_poverty_lines_percent_of_population &lt;dbl&gt;,
#   gni_atlas_method_current_us &lt;dbl&gt;,
#   gni_per_capita_atlas_method_current_us &lt;dbl&gt;,
#   gni_ppp_current_international &lt;dbl&gt;,
#   gni_per_capita_ppp_current_international &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>If we want to see the countries that also have an area that is less than the size of Ohio (116,098 km^2), we can also add that to our filter statement using the <code>&amp;</code> operator.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb21-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(population_total <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11760000</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> surface_area_sq_km <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">116098</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 4 × 53
  country_name country_code population_total population_growth_annual_percent
  &lt;chr&gt;        &lt;chr&gt;                   &lt;dbl&gt;                            &lt;dbl&gt;
1 Guatemala    GTM                  15567419                            1.69 
2 Korea, Rep.  KOR                  51014947                            0.527
3 Netherlands  NLD                  16939923                            0.443
4 Sri Lanka    LKA                  21336697                            0.457
# ℹ 49 more variables: surface_area_sq_km &lt;dbl&gt;,
#   poverty_headcount_ratio_at_national_poverty_lines_percent_of_population &lt;dbl&gt;,
#   gni_atlas_method_current_us &lt;dbl&gt;,
#   gni_per_capita_atlas_method_current_us &lt;dbl&gt;,
#   gni_ppp_current_international &lt;dbl&gt;,
#   gni_per_capita_ppp_current_international &lt;dbl&gt;,
#   income_share_held_by_lowest_20_percent &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>There are four countries that have more people than Ohio in a space that is less than Ohio.</p>
<section id="practice" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="practice"><span class="header-section-number">2.1</span> Practice</h2>
<p>How many countries have negative annual population growth? This variable is called <code>population_growth_annual_percent</code>.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Try to <code>filter</code> for <code>population_growth_annual_percent</code> &lt; 0.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb23-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(population_growth_annual_percent <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 34 × 53
   country_name           country_code population_total population_growth_annu…¹
   &lt;chr&gt;                  &lt;chr&gt;                   &lt;dbl&gt;                    &lt;dbl&gt;
 1 Albania                ALB                   2880703                   -0.291
 2 American Samoa         ASM                     51368                   -1.64 
 3 Armenia                ARM                   2878595                   -0.393
 4 Bosnia and Herzegovina BIH                   3524324                   -1.32 
 5 Bulgaria               BGR                   7177991                   -0.638
 6 Croatia                HRV                   4203604                   -0.824
 7 Greece                 GRC                  10820883                   -0.659
 8 Greenland              GRL                     56114                   -0.322
 9 Hungary                HUN                   9843028                   -0.238
10 Isle of Man            IMN                     83593                   -0.362
# ℹ 24 more rows
# ℹ abbreviated name: ¹​population_growth_annual_percent
# ℹ 49 more variables: surface_area_sq_km &lt;dbl&gt;,
#   poverty_headcount_ratio_at_national_poverty_lines_percent_of_population &lt;dbl&gt;,
#   gni_atlas_method_current_us &lt;dbl&gt;,
#   gni_per_capita_atlas_method_current_us &lt;dbl&gt;,
#   gni_ppp_current_international &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>There are 34 countries that have a negative annual growth rate in 2015.</p>
</details>
</section>
</section>
<section id="choose-columns-with-select" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Choose columns with <code>select()</code></h1>
<p>Often you will want to pick only certain columns in your dataframe, and you can do this with the function <a href="https://dplyr.tidyverse.org/reference/select.html"><code>select()</code></a>. You can pick columns generally by:</p>
<ul>
<li>their names</li>
<li>their position</li>
<li>characteristics of that column</li>
</ul>
<p>If we want to know how the arguments to <code>select()</code> work, we can access the documentation material about the function.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">?<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>()</span></code></pre></div></div>
</div>
<p>Let’s first select columns by their names. Let’s pick just the <code>country_name</code>, <code>population_total</code>, and <code>surface_area_sq_km</code>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb26-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name, population_total, surface_area_sq_km)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 3
   country_name        population_total surface_area_sq_km
   &lt;chr&gt;                          &lt;dbl&gt;              &lt;dbl&gt;
 1 Afghanistan                 33753499             652860
 2 Albania                      2880703              28750
 3 Algeria                     39543154            2381741
 4 American Samoa                 51368                200
 5 Andorra                        71746                470
 6 Angola                      28127721            1246700
 7 Antigua and Barbuda            89941                440
 8 Argentina                   43131966            2780400
 9 Armenia                      2878595              29740
10 Aruba                         104257                180
# ℹ 207 more rows</code></pre>
</div>
</div>
<p>Those columns are also the 1st, 3rd, and 5th columns in our data frame, do we can select them by their indices, or by their location.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb28-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 3
   country_name        population_total surface_area_sq_km
   &lt;chr&gt;                          &lt;dbl&gt;              &lt;dbl&gt;
 1 Afghanistan                 33753499             652860
 2 Albania                      2880703              28750
 3 Algeria                     39543154            2381741
 4 American Samoa                 51368                200
 5 Andorra                        71746                470
 6 Angola                      28127721            1246700
 7 Antigua and Barbuda            89941                440
 8 Argentina                   43131966            2780400
 9 Armenia                      2878595              29740
10 Aruba                         104257                180
# ℹ 207 more rows</code></pre>
</div>
</div>
<p>In general I would recommend against this because its really hard to remember which column indices are which variables today, nevermind returning back to old code 1 year from now.</p>
<p>We can also select columns that are consecutive, using the <code>:</code> operator. Below I’m selecting the columns <code>country_name</code> through <code>population_growth_annual_percent</code>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb30-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>population_growth_annual_percent)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 4
   country_name        country_code population_total population_growth_annual_…¹
   &lt;chr&gt;               &lt;chr&gt;                   &lt;dbl&gt;                       &lt;dbl&gt;
 1 Afghanistan         AFG                  33753499                       3.12 
 2 Albania             ALB                   2880703                      -0.291
 3 Algeria             DZA                  39543154                       2.00 
 4 American Samoa      ASM                     51368                      -1.64 
 5 Andorra             AND                     71746                       0.174
 6 Angola              AGO                  28127721                       3.62 
 7 Antigua and Barbuda ATG                     89941                       0.787
 8 Argentina           ARG                  43131966                       1.08 
 9 Armenia             ARM                   2878595                      -0.393
10 Aruba               ABW                    104257                       0.638
# ℹ 207 more rows
# ℹ abbreviated name: ¹​population_growth_annual_percent</code></pre>
</div>
</div>
<p>We can remove columns using the <code>!</code> operator: you can read the <code>!</code> as “not”.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb32-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>country_code)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 52
   country_name       population_total population_growth_an…¹ surface_area_sq_km
   &lt;chr&gt;                         &lt;dbl&gt;                  &lt;dbl&gt;              &lt;dbl&gt;
 1 Afghanistan                33753499                  3.12              652860
 2 Albania                     2880703                 -0.291              28750
 3 Algeria                    39543154                  2.00             2381741
 4 American Samoa                51368                 -1.64                 200
 5 Andorra                       71746                  0.174                470
 6 Angola                     28127721                  3.62             1246700
 7 Antigua and Barbu…            89941                  0.787                440
 8 Argentina                  43131966                  1.08             2780400
 9 Armenia                     2878595                 -0.393              29740
10 Aruba                        104257                  0.638                180
# ℹ 207 more rows
# ℹ abbreviated name: ¹​population_growth_annual_percent
# ℹ 48 more variables:
#   poverty_headcount_ratio_at_national_poverty_lines_percent_of_population &lt;dbl&gt;,
#   gni_atlas_method_current_us &lt;dbl&gt;,
#   gni_per_capita_atlas_method_current_us &lt;dbl&gt;,
#   gni_ppp_current_international &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>We can also select data based on its characteristics. We can select using <a href="https://dplyr.tidyverse.org/reference/select.html">selection helpers</a> like:</p>
<ul>
<li><a href="https://tidyselect.r-lib.org/reference/everything.html"><code>everything()</code></a>: picks all variables</li>
<li><a href="https://tidyselect.r-lib.org/reference/starts_with.html"><code>starts_with()</code></a>: starts with some prefix</li>
<li><a href=""><code>ends_with()</code></a>: ends with some suffix</li>
<li><a href="https://tidyselect.r-lib.org/reference/starts_with.html"><code>contains()</code></a>: contains a specific string</li>
<li><a href="https://tidyselect.r-lib.org/reference/starts_with.html"><code>matches()</code></a>: matches a regular expression</li>
<li><a href="https://tidyselect.r-lib.org/reference/starts_with.html"><code>num_range()</code></a>: matches a numeric range</li>
<li><a href="https://tidyselect.r-lib.org/reference/where.html"><code>where()</code></a>: selects columns where the statement given in the argument is TRUE</li>
<li><a href="https://tidyselect.r-lib.org/reference/all_of.html"><code>all_of()</code></a>: matches all of the variable names in a character vector</li>
<li><a href="https://tidyselect.r-lib.org/reference/all_of.html"><code>any_of()</code></a>: matches any of the names in a character vector</li>
</ul>
<p>For example, we might want each column that has anything to do with gross domestic product, or gdp. We can select all of the columns which contain the string “gdp” in their name. I’m also going to add <code>country_name</code> so we know what we’re working with.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb34-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gdp"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 15
   country_name     water_productivity_t…¹ gdp_current_us gdp_growth_annual_pe…²
   &lt;chr&gt;                             &lt;dbl&gt;          &lt;dbl&gt;                  &lt;dbl&gt;
 1 Afghanistan                       0.943    19134221645                  1.45 
 2 Albania                          12.4      11386853113                  2.22 
 3 Algeria                          20.9     187494000000                  3.2  
 4 American Samoa                   NA          673000000                  3.15 
 5 Andorra                          NA         2789881259                  1.43 
 6 Angola                          128.       90496420507                  0.944
 7 Antigua and Bar…                327.        1437755556                  1.45 
 8 Argentina                        15.8     594749000000                  2.73 
 9 Armenia                           3.23     10553337518                  3.20 
10 Aruba                            NA         2962907263                 -0.624
# ℹ 207 more rows
# ℹ abbreviated names:
#   ¹​water_productivity_total_constant_2015_us_gdp_per_cubic_meter_of_total_freshwater_withdrawal,
#   ²​gdp_growth_annual_percent
# ℹ 11 more variables: inflation_gdp_deflator_annual_percent &lt;dbl&gt;,
#   agriculture_forestry_and_fishing_value_added_percent_of_gdp &lt;dbl&gt;,
#   industry_including_construction_value_added_percent_of_gdp &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>We can also select all column that meet a certain predicate. For example, we can pick all of the column that are of the type character.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb36-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">where</span>(is.character))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 2
   country_name        country_code
   &lt;chr&gt;               &lt;chr&gt;       
 1 Afghanistan         AFG         
 2 Albania             ALB         
 3 Algeria             DZA         
 4 American Samoa      ASM         
 5 Andorra             AND         
 6 Angola              AGO         
 7 Antigua and Barbuda ATG         
 8 Argentina           ARG         
 9 Armenia             ARM         
10 Aruba               ABW         
# ℹ 207 more rows</code></pre>
</div>
</div>
<p>We can also combine selections using the <code>&amp;</code> (and), <code>|</code> (or), and <code>!</code> (not) operators. For example, if I want the columns about GNI but only the international ones I can:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb38-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">starts_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gni"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ends_with</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"international"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 2
   gni_ppp_current_international gni_per_capita_ppp_current_international
                           &lt;dbl&gt;                                    &lt;dbl&gt;
 1                   77739869000                                     2300
 2                   33981401261                                    11800
 3                  541234000000                                    13690
 4                            NA                                       NA
 5                            NA                                       NA
 6                  190283000000                                     6760
 7                    1766297473                                    19640
 8                  848960000000                                    19680
 9                   30505448341                                    10600
10                    3718236361                                    35660
# ℹ 207 more rows</code></pre>
</div>
</div>
<p>We can also use <code>select()</code> to order our columns, as the order we select them in dictates the order they will exist in our dataframe.</p>
<section id="practice-1" class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="practice-1"><span class="header-section-number">3.1</span> Practice</h2>
<p>Come up with 3 different ways to select the columns about children, and make sure you also include a country column so you know what you’re looking at.</p>
<details>
<summary>
Need a hint?
</summary>
<p>Here are the columns that I’m considering to be about children:</p>
<p>[1] “country_name”<br>
[17] “mortality_rate_under_5_per_1_000_live_births”<br>
[18] “prevalence_of_underweight_weight_for_age_percent_of_children_under_5”<br>
[19] “immunization_measles_percent_of_children_ages_12_23_months”<br>
[20] “primary_completion_rate_total_percent_of_relevant_age_group”<br>
[21] “school_enrollment_secondary_percent_gross”<br>
[22] “school_enrollment_primary_and_secondary_gross_gender_parity_index_gpi”</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<p>By name and for columns in a row, using the <code>:</code>.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb40-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name, </span>
<span id="cb40-3">         mortality_rate_under_5_per_1_000_live_births<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb40-4">         school_enrollment_primary_and_secondary_gross_gender_parity_index_gpi)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 7
   country_name        mortality_rate_under_5_per_1_000…¹ prevalence_of_underw…²
   &lt;chr&gt;                                            &lt;dbl&gt;                  &lt;dbl&gt;
 1 Afghanistan                                       72.7                     NA
 2 Albania                                            9.6                     NA
 3 Algeria                                           25.3                     NA
 4 American Samoa                                    NA                       NA
 5 Andorra                                            3.6                     NA
 6 Angola                                            87.9                     19
 7 Antigua and Barbuda                               10.9                     NA
 8 Argentina                                         11.8                     NA
 9 Armenia                                           14.5                     NA
10 Aruba                                             NA                       NA
# ℹ 207 more rows
# ℹ abbreviated names: ¹​mortality_rate_under_5_per_1_000_live_births,
#   ²​prevalence_of_underweight_weight_for_age_percent_of_children_under_5
# ℹ 4 more variables:
#   immunization_measles_percent_of_children_ages_12_23_months &lt;dbl&gt;,
#   primary_completion_rate_total_percent_of_relevant_age_group &lt;dbl&gt;,
#   school_enrollment_secondary_percent_gross &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>By index:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb42-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>) </span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 7
   country_name        mortality_rate_under_5_per_1_000…¹ prevalence_of_underw…²
   &lt;chr&gt;                                            &lt;dbl&gt;                  &lt;dbl&gt;
 1 Afghanistan                                       72.7                     NA
 2 Albania                                            9.6                     NA
 3 Algeria                                           25.3                     NA
 4 American Samoa                                    NA                       NA
 5 Andorra                                            3.6                     NA
 6 Angola                                            87.9                     19
 7 Antigua and Barbuda                               10.9                     NA
 8 Argentina                                         11.8                     NA
 9 Armenia                                           14.5                     NA
10 Aruba                                             NA                       NA
# ℹ 207 more rows
# ℹ abbreviated names: ¹​mortality_rate_under_5_per_1_000_live_births,
#   ²​prevalence_of_underweight_weight_for_age_percent_of_children_under_5
# ℹ 4 more variables:
#   immunization_measles_percent_of_children_ages_12_23_months &lt;dbl&gt;,
#   primary_completion_rate_total_percent_of_relevant_age_group &lt;dbl&gt;,
#   school_enrollment_secondary_percent_gross &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>By name characteristics:</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb44-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"under_5"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"children"</span>),</span>
<span id="cb44-3">         <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"primary"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">contains</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"school"</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 7
   country_name        mortality_rate_under_5_per_1_000…¹ prevalence_of_underw…²
   &lt;chr&gt;                                            &lt;dbl&gt;                  &lt;dbl&gt;
 1 Afghanistan                                       72.7                     NA
 2 Albania                                            9.6                     NA
 3 Algeria                                           25.3                     NA
 4 American Samoa                                    NA                       NA
 5 Andorra                                            3.6                     NA
 6 Angola                                            87.9                     19
 7 Antigua and Barbuda                               10.9                     NA
 8 Argentina                                         11.8                     NA
 9 Armenia                                           14.5                     NA
10 Aruba                                             NA                       NA
# ℹ 207 more rows
# ℹ abbreviated names: ¹​mortality_rate_under_5_per_1_000_live_births,
#   ²​prevalence_of_underweight_weight_for_age_percent_of_children_under_5
# ℹ 4 more variables:
#   immunization_measles_percent_of_children_ages_12_23_months &lt;dbl&gt;,
#   primary_completion_rate_total_percent_of_relevant_age_group &lt;dbl&gt;,
#   school_enrollment_primary_and_secondary_gross_gender_parity_index_gpi &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>These are just some ways!</p>
</details>
</section>
</section>
<section id="sorting-data-with-arrange" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Sorting data with <code>arrange()</code></h1>
<p>A nice helper function for looking at your data is <a href="https://dplyr.tidyverse.org/reference/arrange.html"><code>arrange()</code></a> which sorts your data.</p>
<p>We can sort our data based on how much forest (<code>forest_area_sq_km</code>) each country has.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb46-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(forest_area_sq_km)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 53
   country_name             country_code population_total population_growth_an…¹
   &lt;chr&gt;                    &lt;chr&gt;                   &lt;dbl&gt;                  &lt;dbl&gt;
 1 Gibraltar                GIB                     32520                  0.209
 2 Monaco                   MCO                     36760                  1.78 
 3 Nauru                    NRU                     11185                  2.21 
 4 Qatar                    QAT                   2414573                  8.65 
 5 Curacao                  CUW                    157980                  1.32 
 6 Faroe Islands            FRO                     48816                  0.722
 7 Greenland                GRL                     56114                 -0.322
 8 Malta                    MLT                    445053                  2.39 
 9 Sint Maarten (Dutch par… SXM                     38825                  2.98 
10 Aruba                    ABW                    104257                  0.638
# ℹ 207 more rows
# ℹ abbreviated name: ¹​population_growth_annual_percent
# ℹ 49 more variables: surface_area_sq_km &lt;dbl&gt;,
#   poverty_headcount_ratio_at_national_poverty_lines_percent_of_population &lt;dbl&gt;,
#   gni_atlas_method_current_us &lt;dbl&gt;,
#   gni_per_capita_atlas_method_current_us &lt;dbl&gt;,
#   gni_ppp_current_international &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>By default, <code>arrange()</code> sorts small to big, if we want to go from big to small we can set <code>arrange(desc())</code> to sort by descending.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb48-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(forest_area_sq_km))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 53
   country_name       country_code population_total population_growth_annual_p…¹
   &lt;chr&gt;              &lt;chr&gt;                   &lt;dbl&gt;                        &lt;dbl&gt;
 1 Russian Federation RUS                 144640716                        0.279
 2 Brazil             BRA                 205188205                        0.846
 3 Canada             CAN                  35704498                        0.760
 4 United States      USA                 320738994                        0.736
 5 China              CHN                1379860000                        0.581
 6 Australia          AUS                  23815995                        1.44 
 7 Congo, Dem. Rep.   COD                  78656904                        3.39 
 8 Indonesia          IDN                 259091970                        1.11 
 9 Peru               PER                  30711863                        1.17 
10 India              IND                1322866505                        1.19 
# ℹ 207 more rows
# ℹ abbreviated name: ¹​population_growth_annual_percent
# ℹ 49 more variables: surface_area_sq_km &lt;dbl&gt;,
#   poverty_headcount_ratio_at_national_poverty_lines_percent_of_population &lt;dbl&gt;,
#   gni_atlas_method_current_us &lt;dbl&gt;,
#   gni_per_capita_atlas_method_current_us &lt;dbl&gt;,
#   gni_ppp_current_international &lt;dbl&gt;, …</code></pre>
</div>
</div>
<p>It’s not too surprising that the biggest countries in forest-feasible latitudes have the most forest.</p>
<p>We can also add <code>select()</code> into our pipe, so that we don’t have to scroll so far to see what the actual amount of forest is.</p>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb50-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name, surface_area_sq_km, forest_area_sq_km) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb50-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(forest_area_sq_km))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 3
   country_name       surface_area_sq_km forest_area_sq_km
   &lt;chr&gt;                           &lt;dbl&gt;             &lt;dbl&gt;
 1 Russian Federation           17098250          8149305.
 2 Brazil                        8515770          5038848 
 3 Canada                        9879750          3471157.
 4 United States                 9831510          3100950 
 5 China                         9562911          2102942.
 6 Australia                     7741220          1330945 
 7 Congo, Dem. Rep.              2344860          1316621.
 8 Indonesia                     1913580           950279 
 9 Peru                          1285220           731945.
10 India                         3287260           708280 
# ℹ 207 more rows</code></pre>
</div>
</div>
<p>Wow almost 50% of Russia is forested.</p>
<section id="practice-2" class="level2" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="practice-2"><span class="header-section-number">4.1</span> Practice</h2>
<p>Which countries have the lowest cell phone subscriptions? <code>mobile_cellular_subscriptions_per_100_people</code></p>
<details>
<summary>
Need a hint?
</summary>
<p>You can use the function <code>arrange()</code> to sort your columns. The default arranging is from low to high, so if you want to go from high to low, you can set <code>arrange(desc())</code>.</p>
</details>
<details>
<summary>
Click for the solution
</summary>
<div class="cell" data-layout-align="center">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1">factbook_2015 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb52-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(country_name, mobile_cellular_subscriptions_per_100_people) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb52-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(mobile_cellular_subscriptions_per_100_people)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre class="styled-output"><code># A tibble: 217 × 2
   country_name              mobile_cellular_subscriptions_per_100_people
   &lt;chr&gt;                                                            &lt;dbl&gt;
 1 Korea, Dem. People's Rep.                                         12.8
 2 Eritrea                                                           14.2
 3 Micronesia, Fed. Sts.                                             20.6
 4 Central African Republic                                          25.8
 5 South Sudan                                                       25.9
 6 Cuba                                                              29.4
 7 Djibouti                                                          31.2
 8 Marshall Islands                                                  31.4
 9 Kiribati                                                          35.1
10 Chad                                                              38.7
# ℹ 207 more rows</code></pre>
</div>
</div>
</details>


</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>r-basics</category>
  <category>tidyverse</category>
  <category>github-copilot</category>
  <category>gen-ai</category>
  <guid>https://osu-codeclub.github.io/posts/S10E06_ai_filter-select-basics_01/</guid>
  <pubDate>Mon, 13 Oct 2025 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E06_ai_filter-select-basics_01/img/go-wrangling.png" medium="image" type="image/png" height="115" width="144"/>
</item>
<item>
  <title>Reproducibility recommendations: Code styling</title>
  <dc:creator>Horacio Lopez-Nicora</dc:creator>
  <link>https://osu-codeclub.github.io/posts/S10E05_reprod_05/</link>
  <description><![CDATA[ 




<p><br></p>
<section id="introduction" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="introduction"><span class="header-section-number">1</span> Introduction</h2>
<blockquote class="blockquote">
<p>Good coding style is like correct punctuation: you can manage without it, butitsuremakesthingseasiertoread. [<a href="https://r4ds.hadley.nz">R for Data Science (2e)</a>]</p>
</blockquote>
<p>Welcome to this Code Club OSU session on Code Styling in R. Whether you’re just starting out with R or looking to improve the readability and consistency of your code, this session is designed to give you practical, beginner-friendly guidance on writing clean and professional R code. Code styling is more than just aesthetics, it’s about making your code easier to understand, debug, and share with others. In collaborative environments like research labs, classrooms, or open-source projects, consistent style helps everyone stay on the same page.</p>
<p>Today, we’ll explore key principles from the tidyverse style guide and the R for Data Science workflow/style chapter, and apply them using real datasets like <a href="https://allisonhorst.github.io/palmerpenguins/articles/intro.html"><code>palmerpenguins</code></a>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://allisonhorst.github.io/palmerpenguins/reference/figures/lter_penguins.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="435"></p>
</figure>
</div>
<p>By the end of this session, you’ll be able to recognize good style, apply it to your own code, and understand why it matters. Let’s dive in!</p>
<p>Before we start, let’s quickly build on what we covered last session about organizing our code. Now, go ahead and load the <strong>tidyverse</strong> and <strong>janitor</strong> packages. If you haven’t installed them yet, install them first.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LOAD PACKAGES</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># install.packages("janitor")</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(janitor) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># To fix column names with clean_names() (version 2.2.1)</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("janitor")</span></span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data summarizing, plotting, and writing (version 2.0.0)</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="why-code-style-matters" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="why-code-style-matters"><span class="header-section-number">2</span> Why Code Style Matters</h2>
<p>When writing code, especially in a collaborative or academic setting, style is not just a matter of personal preference, it’s a matter of clarity and professionalism.</p>
<p><strong>Well-styled code</strong> is easier to <em>read</em>, <em>debug</em>, and <em>maintain</em>. It helps others understand your logic without needing extensive explanations. <strong>Inconsistent</strong> or <strong>messy code</strong> can <em>slow down</em> projects, introduce <em>errors</em>, and make collaboration <em>frustrating</em>. By following a consistent style guide, like the <a href="https://style.tidyverse.org">tidyverse style guide</a>, you ensure that your code communicates clearly and efficiently. Today, we’ll learn how to make our R code clean, readable, and consistent; skills that will serve you well in research, coursework, and data science projects.</p>
</section>
<section id="naming-conventions-human-readable-machine-readable" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="naming-conventions-human-readable-machine-readable"><span class="header-section-number">3</span> <strong>Naming Conventions:</strong> Human-Readable &amp; Machine-Readable</h2>
<p>One of the first steps toward clean code is choosing good names for your variables and functions. In R, we recommend using <strong>snake_case</strong>—lowercase letters with underscores separating words. For example, <code>penguin_summary</code> is much clearer than something like <code>df1</code> or <code>temp</code>.</p>
<p>Good names describe what the object contains or does, making your code self-documenting. Avoid abbreviations unless they’re widely understood, and don’t be afraid to use longer names if they improve clarity. Think of naming as labeling your thoughts, make it easy for someone else (or future you) to understand what each part of your code is doing.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Messy Example</span></span>
<span id="cb2-2">df1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins</span>
<span id="cb2-3">temp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(df1, species)</span>
<span id="cb2-4">result <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(temp, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Styled Example</span></span>
<span id="cb2-7">penguin_summary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Style notes:</span></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Use descriptive names: 'penguin_summary' instead of 'df1' or 'temp'</span></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Use snake_case consistently</span></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Avoid vague or temporary names</span></span></code></pre></div></div>
<p>Good names serve <strong>two audiences</strong>:</p>
<ol type="1">
<li><p><strong>Humans</strong>: other programmers (or your future self) need to understand what the code does quickly.</p></li>
<li><p><strong>Machines</strong>: R and other tools need names that are valid, unambiguous, and free of spaces or special characters.</p></li>
</ol>
<section id="human-readable" class="level3" data-number="3.1">
<h3 data-number="3.1" class="anchored" data-anchor-id="human-readable"><span class="header-section-number">3.1</span> Human-Readable</h3>
<ul>
<li><p>Names should <strong>clearly describe</strong> the content or purpose of the object.</p></li>
<li><p>Avoid abbreviations that aren’t widely understood.</p></li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># We immediately know what it represents:</span></span>
<span id="cb3-2">avg_flipper_length <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(penguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>flipper_len, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Avoid vague or cryptic names:</span></span>
<span id="cb3-5">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(penguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>flipper_len)</span></code></pre></div></div>
</section>
<section id="machine-readable" class="level3" data-number="3.2">
<h3 data-number="3.2" class="anchored" data-anchor-id="machine-readable"><span class="header-section-number">3.2</span> Machine-Readable</h3>
<ul>
<li><p>Avoid spaces, special characters, or punctuation in names.</p></li>
<li><p>Column names imported from CSVs are often&nbsp;<strong>not machine-friendly</strong>:</p></li>
</ul>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#"Flipper Length (mm)" → contains spaces and parentheses </span></span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use snake_case and letters/numbers/underscores only:</span></span>
<span id="cb4-4">flipper_length_mm</span></code></pre></div></div>
</section>
<section id="balancing-both" class="level3" data-number="3.3">
<h3 data-number="3.3" class="anchored" data-anchor-id="balancing-both"><span class="header-section-number">3.3</span> <strong>Balancing Both</strong></h3>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Aspect</th>
<th>Recommendation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Human readability</td>
<td>Use descriptive, clear names, readable words, meaningful abbreviations</td>
</tr>
<tr class="even">
<td>Machine readability</td>
<td>Use snake_case, no spaces/special characters, consistent casing</td>
</tr>
<tr class="odd">
<td>Example</td>
<td><code>avg_bill_length</code>&nbsp;✅ vs&nbsp;<code>Avg.Bill.Length</code>&nbsp;❌</td>
</tr>
</tbody>
</table>
<p>Tools like&nbsp;<strong><code>janitor::clean_names()</code></strong>&nbsp;make column names both human- and machine-readable:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><a href="https://cran.r-project.org/web/packages/janitor/vignettes/janitor.html"><img src="https://osu-codeclub.github.io/posts/S10E05_reprod_05/img/janitor_clean_names.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></a></p>
</figure>
</div>
<figcaption>Artwork by @allison_horst</figcaption>
</figure>
</div>
<p>Using&nbsp;<code>janitor::clean_names()</code>&nbsp;at the start of a data analysis project is a simple step that prevents a lot of frustration later. Real-world datasets often come with inconsistent, messy column names—things like spaces, capital letters, punctuation, or mixed naming styles. These names can slow you down when writing code, force you to use backticks, and break the flow of tidyverse functions.</p>
<p>By cleaning names immediately, you convert everything to consistent, machine-friendly, <strong>snake_case</strong> column names that are easy to type, reference, and style. It also supports cleaner pipelines, improves readability, and aligns with best practices for reproducible and collaborative code.</p>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># MESSY Code</span></span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Examine dataset</span></span>
<span id="cb5-4">penguins_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select variables of interes</span></span>
<span id="cb5-7">penguins_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Body Mass (g)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, Sex, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Date Egg</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># STYLED Code</span></span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use janitor::clean_names and select same variables</span></span>
<span id="cb5-12">penguins_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean_names</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>()</span>
<span id="cb5-13">penguins_raw <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">clean_names</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(body_mass_g, sex, date_egg)</span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="pipes-and-indentation" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="pipes-and-indentation"><span class="header-section-number">4</span> <strong>Pipes and Indentation</strong></h2>
<p>The pipe operator (<code>|&gt;</code>) is one of the most powerful tools in the tidyverse. It allows you to write code that reads like a sequence of actions: take this data, do this, then do that. Each step in a pipeline should be indented on a new line, making the flow of logic easy to follow. For example, when summarizing data, you might start with <code>penguins |&gt;</code>, then indent <code>group_by(species)</code>, and follow with <code>summarise(...)</code>. This structure helps you and others quickly scan and understand the transformation. Indentation is not just about aesthetics—it’s about making your code readable and maintainable.</p>
<p>Lets’ look at some examples:</p>
<section id="example-1-filtering-and-summarizing" class="level4">
<h4 class="anchored" data-anchor-id="example-1-filtering-and-summarizing"><strong>Example 1: Filtering and Summarizing</strong></h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Cramped, no indentation, unclear naming</span></span>
<span id="cb6-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Clear naming, spacing, indentation, and comments</span></span>
<span id="cb6-5">adelie_summary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adelie"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filter to Adelie penguins</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(island) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Group by island</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate average body mass</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-2-mutate-and-arrange" class="level4">
<h4 class="anchored" data-anchor-id="example-2-mutate-and-arrange"><strong>Example 2: Mutate and Arrange</strong></h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Semicolon chaining, inconsistent naming</span></span>
<span id="cb7-2">penguins2<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(penguins,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bmi=</span>body_mass<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>bill_len);<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(penguins2,bmi)</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Pipe used throughout, clear naming, spacing, and indentation</span></span>
<span id="cb7-5">penguins_bmi <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bmi =</span> body_mass <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> bill_len) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create BMI variable</span></span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(bmi)                                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sort by BMI</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-3-summarise-misalignment" class="level4">
<h4 class="anchored" data-anchor-id="example-3-summarise-misalignment"><strong>Example 3: Summarise Misalignment</strong></h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Misaligned summarise arguments</span></span>
<span id="cb8-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), </span>
<span id="cb8-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())</span>
<span id="cb8-6"></span>
<span id="cb8-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Each summary on its own line, aligned</span></span>
<span id="cb8-8">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb8-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb8-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb8-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb8-13">  )</span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-4-broken-pipe-chain" class="level4">
<h4 class="anchored" data-anchor-id="example-4-broken-pipe-chain"><strong>Example 4: Broken Pipe Chain</strong></h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Pipe chain is broken</span></span>
<span id="cb9-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species)</span>
<span id="cb9-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Pipe chain is continuous</span></span>
<span id="cb9-7">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb9-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="spacing-and-alignment" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="spacing-and-alignment"><span class="header-section-number">5</span> <strong>Spacing and Alignment</strong></h2>
<p>Consistent spacing around operators and arguments improves readability. For example, write <code>x = 5</code> instead of <code>x=5</code>, and align similar lines when possible. This makes patterns in your code easier to spot and reduces cognitive load. When writing multiple lines of similar code, aligning them vertically can help highlight differences and similarities. <strong>Think of spacing as visual punctuation</strong>, it guides the reader’s eye and helps them parse your code more easily. Avoid cramming everything into one line; give your code room to breathe.</p>
<section id="example-1-assignment-and-function-calls" class="level4">
<h4 class="anchored" data-anchor-id="example-1-assignment-and-function-calls">Example 1: Assignment and Function Calls</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: No spacing, cramped code</span></span>
<span id="cb10-2">penguins_summary<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>penguins<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Proper spacing and alignment</span></span>
<span id="cb10-5">penguins_summary <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb10-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Style notes:</span></span>
<span id="cb10-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Use spaces around assignment (&lt;-), pipes (|&gt;), and function arguments (=)</span></span>
<span id="cb10-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Align each step of the pipeline on a new line</span></span>
<span id="cb10-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Use descriptive variable and column names</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-2-multiple-arguments-in-summarise" class="level4">
<h4 class="anchored" data-anchor-id="example-2-multiple-arguments-in-summarise">Example 2: Multiple Arguments in <code>summarise()</code></h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Arguments crammed together</span></span>
<span id="cb11-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm=</span><span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count=</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>())</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Each argument on its own line, aligned</span></span>
<span id="cb11-5">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(species) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb11-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb11-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">count =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>()</span>
<span id="cb11-10">  )</span>
<span id="cb11-11"></span>
<span id="cb11-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Style notes:</span></span>
<span id="cb11-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Each summary metric is on its own line</span></span>
<span id="cb11-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Arguments are aligned for readability</span></span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="inline-comments" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="inline-comments"><span class="header-section-number">6</span> <strong>Inline Comments</strong></h2>
<p>Comments are your opportunity to explain <strong><em>why</em></strong> your code does what it does. While code should be self-explanatory through good naming and structure, comments provide context that might not be obvious. For example, <code># remove NA values</code> tells the reader why <code>na.rm = TRUE</code> is used. Avoid redundant comments like <code># load data</code> when the code already says <code>library(palmerpenguins)</code>. Instead, focus on explaining decisions, assumptions, or non-obvious steps. Comments should be brief, relevant, and placed directly above or beside the code they refer to.</p>
<section id="example-1-no-comments-or-unhelpful-comments" class="level4">
<h4 class="anchored" data-anchor-id="example-1-no-comments-or-unhelpful-comments">Example 1: No Comments or Unhelpful Comments</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: No explanation or vague comment</span></span>
<span id="cb12-2">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flipper_len, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># summary</span></span>
<span id="cb12-5"></span>
<span id="cb12-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Clear, helpful inline comments</span></span>
<span id="cb12-7">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chinstrap"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Focus on Chinstrap penguins</span></span>
<span id="cb12-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_flipper =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(flipper_len, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate average flipper length</span></span>
<span id="cb12-10"></span>
<span id="cb12-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Style notes:</span></span>
<span id="cb12-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Comments explain *why* or *what* is being done</span></span>
<span id="cb12-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Avoid stating the obvious or repeating the code</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-2-misplaced-or-redundant-comments" class="level4">
<h4 class="anchored" data-anchor-id="example-2-misplaced-or-redundant-comments">Example 2: Misplaced or Redundant Comments</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Redundant and misplaced comments</span></span>
<span id="cb13-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is a filter</span></span>
<span id="cb13-3">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gentoo"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This is a summarise</span></span>
<span id="cb13-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb13-7"></span>
<span id="cb13-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Concise and well-placed comments</span></span>
<span id="cb13-9">penguins <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb13-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(species <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gentoo"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span>                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filter for Gentoo penguins</span></span>
<span id="cb13-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_mass =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(body_mass, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate average body mass</span></span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="plotting-with-style" class="level2" data-number="7">
<h2 data-number="7" class="anchored" data-anchor-id="plotting-with-style"><span class="header-section-number">7</span> <strong>Plotting with Style</strong></h2>
<p>Visualizations are a key part of data analysis, and styling your plots is just as important as styling your code. Use clear labels for axes and titles, choose readable color schemes, and <strong>structure your <code>ggplot</code> code with indentation for each layer</strong>. For example, start with <code>ggplot(...)</code>, then indent <code>geom_boxplot(...)</code>, <code>labs(...)</code>, and <code>theme_minimal()</code>. This makes it easy to see how the plot is constructed. Avoid cluttered plots, simplicity and clarity should guide your design choices. Well-styled plots communicate insights effectively and make your work look polished.</p>
<section id="example-1-cramped-and-unlabeled-plot" class="level4">
<h4 class="anchored" data-anchor-id="example-1-cramped-and-unlabeled-plot">Example 1: Cramped and Unlabeled Plot</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: No labels, no spacing</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(species, body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>()</span>
<span id="cb14-3"></span>
<span id="cb14-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Clear labels, spacing, and structure</span></span>
<span id="cb14-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> body_mass)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lightblue"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb14-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body Mass by Species"</span>,</span>
<span id="cb14-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>,</span>
<span id="cb14-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Body Mass (g)"</span></span>
<span id="cb14-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span>
<span id="cb14-13"></span>
<span id="cb14-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Style notes:</span></span>
<span id="cb14-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Use `labs()` to label axes and title</span></span>
<span id="cb14-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Indent each layer of the plot</span></span>
<span id="cb14-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Use readable color names and themes</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-2-overly-complex-one-liner" class="level4">
<h4 class="anchored" data-anchor-id="example-2-overly-complex-one-liner">Example 2: Overly Complex One-Liner</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ BAD STYLE: Everything in one line, hard to read</span></span>
<span id="cb15-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span>species,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>flipper_len,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill=</span>sex))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Length"</span>)</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ GOOD STYLE: Structured and readable</span></span>
<span id="cb15-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(penguins, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> species, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> flipper_len, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> sex)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_violin</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb15-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length by Species and Sex"</span>,</span>
<span id="cb15-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Species"</span>,</span>
<span id="cb15-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Flipper Length (mm)"</span>,</span>
<span id="cb15-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sex"</span></span>
<span id="cb15-12">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>()</span>
<span id="cb15-14"></span>
<span id="cb15-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Style notes:</span></span>
<span id="cb15-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Each ggplot layer is on its own line</span></span>
<span id="cb15-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Labels are clear and informative</span></span>
<span id="cb15-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Code is easy to scan and modify</span></span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="interactive-code-styling-with-lintr-and-styler" class="level2" data-number="8">
<h2 data-number="8" class="anchored" data-anchor-id="interactive-code-styling-with-lintr-and-styler"><span class="header-section-number">8</span> Interactive Code Styling with <code>lintr</code> and <code>styler</code></h2>
<p>As you begin writing more R code, it’s helpful to use tools that automatically check and improve your code style. Two popular packages for this are <code>lintr</code> and <code>styler</code>. These tools help you write cleaner code by identifying style violations (<code>lintr</code>) and automatically reformatting code (<code>styler</code>) to follow tidyverse conventions.</p>
<p>These tools are especially useful in teaching, collaborative projects, and when preparing code for publication or sharing. Let’s look at how each works with examples.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># INSTALL PACKAGES</span></span>
<span id="cb16-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lintr"</span>)</span>
<span id="cb16-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"styler"</span>)</span>
<span id="cb16-4"></span>
<span id="cb16-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># LOAD PACKAGES</span></span>
<span id="cb16-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lintr)</span>
<span id="cb16-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(styler)</span></code></pre></div></div>
</div>
<section id="using-lintr-to-check-code-style" class="level3" data-number="8.1">
<h3 data-number="8.1" class="anchored" data-anchor-id="using-lintr-to-check-code-style"><span class="header-section-number">8.1</span> Using <code>lintr</code> to Check Code Style</h3>
<p><strong><code>lintr</code></strong> is like a spell-checker for your code. It scans your scripts and flags issues such as inconsistent spacing, bad naming, or improper indentation.</p>
<section id="example-1-lint-a-messy-code-snippet" class="level4">
<h4 class="anchored" data-anchor-id="example-1-lint-a-messy-code-snippet">Example 1: Lint a messy code snippet</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">styler<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">style_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x &lt;- 1+1"</span>)</span>
<span id="cb17-2"></span>
<span id="cb17-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lint</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins |&gt;filter(species=='Adelie')|&gt;summarise(mean(body_mass,na.rm=TRUE))"</span>)</span>
<span id="cb17-4"></span>
<span id="cb17-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output (example):</span></span>
<span id="cb17-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Missing spaces around operators</span></span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Pipe not followed by a space</span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-2-lint-a-messy-code-for-data-viz" class="level4">
<h4 class="anchored" data-anchor-id="example-2-lint-a-messy-code-for-data-viz">Example 2: Lint a messy code for data viz</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lint</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ggplot(penguins,aes(species,body_mass))+geom_boxplot()"</span>)</span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output (example):</span></span>
<span id="cb18-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Missing spaces around operators</span></span>
<span id="cb18-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># - Layers not followed by a space</span></span></code></pre></div></div>
</details>
</div>
</section>
</section>
<section id="using-styler-to-check-code-style" class="level3" data-number="8.2">
<h3 data-number="8.2" class="anchored" data-anchor-id="using-styler-to-check-code-style"><span class="header-section-number">8.2</span> Using <code>styler</code> to Check Code Style</h3>
<p><strong><code>styler</code></strong> is like an auto-correct tool, it reformats your code to match a consistent style guide, such as the tidyverse style.</p>
<section id="example-1-style-a-messy-pipe-chain" class="level4">
<h4 class="anchored" data-anchor-id="example-1-style-a-messy-pipe-chain">Example 1: Style a messy pipe chain</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">style_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins |&gt;filter(species=='Adelie')|&gt;summarise(mean(body_mass,na.rm=TRUE))"</span>)</span>
<span id="cb19-2"></span>
<span id="cb19-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output:</span></span>
<span id="cb19-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># penguins |&gt; </span></span>
<span id="cb19-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   filter(species == "Adelie") |&gt; </span></span></code></pre></div></div>
</details>
</div>
</section>
<section id="example-2-style-a-cluttered-ggplot-call" class="level4">
<h4 class="anchored" data-anchor-id="example-2-style-a-cluttered-ggplot-call">Example 2: Style a cluttered ggplot call</h4>
<div class="cell">
<details class="code-fold">
<summary>Code</summary>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">style_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"penguins|&gt;ggplot(aes(species,body_mass))+geom_boxplot()"</span>)</span>
<span id="cb20-2"></span>
<span id="cb20-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output:</span></span>
<span id="cb20-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># penguins |&gt; ggplot(aes(species, body_mass_g)) +</span></span>
<span id="cb20-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   geom_boxplot()</span></span></code></pre></div></div>
</details>
</div>


</section>
</section>
</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>reproducibility</category>
  <guid>https://osu-codeclub.github.io/posts/S10E05_reprod_05/</guid>
  <pubDate>Mon, 06 Oct 2025 04:00:00 GMT</pubDate>
  <media:content url="https://osu-codeclub.github.io/posts/S10E05_reprod_05/img/lintr_logo.png" medium="image" type="image/png" height="166" width="144"/>
</item>
</channel>
</rss>
