<?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>The Tidy Trekker</title>
<link>https://www.thetidytrekker.com/blog.html</link>
<atom:link href="https://www.thetidytrekker.com/blog.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.7.32</generator>
<lastBuildDate>Mon, 22 Sep 2025 04:00:00 GMT</lastBuildDate>
<item>
  <title>My First Time Vibe-Conf’ing</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/vibe-conf-ing/posit_conf_2025.html</link>
  <description><![CDATA[ 






<section id="what-is-vibe-confing" class="level1">
<h1>What is Vibe Conf’ing?</h1>
<p>Before I recap my experience at posit::conf(2025), let’s talk about “vibe conf’ing” <em>(Vibe Conf- Ing)</em>. I was first introduced to this term by <a href="https://laderast.github.io">Ted Laderas</a>:</p>
<p><img src="https://www.thetidytrekker.com/post/vibe-conf-ing/images/origin.png" class="blog-img img-fluid" alt="Screenshot of social media conversation between Ted Laderas and Meghan Harris, MPH. Ted's post reads '100% support you for Vibe Conf'ing (not vibe coding)'. Meghan Harris replies 'Ted Laderas I am 100% committed to inserting the phrase 'Vibe Conf'ing' into the title of my conference recap blog post I hopefully will write after accrediting you. Thank you' with laughing and sparkle emojis."></p>
<figcaption>
I am a person of my word
</figcaption>
<p>This came after I posted that I was going to posit::conf(2025) without participating in workshops, being a TA, or presenting talks. I couldn’t understand why I struggled to just go “for the vibes” – attending without any expectation to “give something in return.”</p>
<p>After some introspection, it clicked: I’ve never had the privilege to just go to a conference. No matter where I worked, I always HAD to get a talk accepted to afford the conference ticket that comes with it.</p>
<p>While submitting talks is cost-efficient, there’s a hidden cost of stress and labor. Last year <a href="https://thetidytrekker.com/talks/please_let_me_merge/please_let_me_merge">I did a talk at posit::conf(2024)</a> and promptly got covid for the first time immediately afterwards. As I isolated in my room, missing conf events and Tidy Dev Day, I promised myself I’d take “a break” next year.</p>
<p>Fast forward to this week: I actually listened to myself. I attended with no talks submitted, no expectations, just good vibes. I don’t know if I’ll submit next year (I have a backlog of ideas) or just vibe again. For now, let me dive into what this experience was like.</p>
<hr>
</section>
<section id="what-was-positconf2025-like" class="level1">
<h1>What was posit::conf(2025) Like?</h1>
<p>posit::conf(2025) was my 5th RStudio/Posit conference and 4th in-person. What I love about it is how chill and friendly it is. You don’t have to dress up, worry about being socially awkward (I see you, fellow neurospicy peeps), or stress too much.</p>
<p><img src="https://www.thetidytrekker.com/post/vibe-conf-ing/images/dress.jpg" class="blog-img img-fluid" alt="Me with ombre blue-tinted braids taking a mirror selfie while wearing a colorful galaxy themed dress with designs in red, orange, yellow, and other vibrant colors."></p>
<figcaption>
Posit conf: The only conference I’d feel comfortable rocking blue hair and a space dress.
</figcaption>
<p><br></p>
<p>You can come and go as you please and choose how to consume keynotes and talks. Attend presentations live, watch from Posit’s lounge outside the ballroom if crowds aren’t your thing, or – though it seems counterintuitive – watch from your hotel room (I do this more than I care to admit). One of the best things about conf is the ability to tailor it to what you want and engage as much or as little as you’d like.</p>
<p>For first-timers or solo attendees, the organizers work hard to make everyone feel welcome. From Posit Discord invites to <a href="https://masto.machlis.com/@smach/115219969813836800">encouraging the pac-man rule</a>, the conference has earned its reputation for being newcomer-friendly.</p>
<p><img src="https://www.thetidytrekker.com/post/vibe-conf-ing/images/pacman.png" class="blog-img img-fluid" alt="An infographic titled 'The Pac-Man Rule' showing a yellow Pac-Man shape surrounded by people figures, with one person approaching from the right to enter pacman's mouth. Text reads 'Leave a space in your group for others to come join your conversation."></p>
<figcaption>
From what I could see, a lot of us were actively following this rule
</figcaption>
<hr>
</section>
<section id="my-personal-highlights" class="level1">
<h1>My Personal Highlights</h1>
<section id="talk-sessions" class="level2">
<h2 class="anchored" data-anchor-id="talk-sessions">Talk Sessions</h2>
<p>As I’ve mentioned, Posit conf is really what you make it. This year, with no obligations, I felt “free to learn” for the first time. Having this freedom solidified something I’d been noticing: I’m increasingly drawn to the development side of data work.</p>
<p>I’ve found myself on a path that may be familiar to some data scientists. There’s a subset of us who dip a toe into the “Development Pond” – maybe we need to build tools for data processing that dance on the edge of software engineering. Some pull their feet out once they’ve gotten what they needed, but others realize these skills could transform how we approach data problems entirely.</p>
<p>It’s an exciting challenge: wanting to deepen these development skills while applying them to the work I already love. This year’s conf was perfect for me because I was intentional about attending development-focused sessions that could enhance my current toolkit. Here are some talks that stood out:</p>
<p>(Links are provided to the slides if I can find them!) Check the <a href="https://github.com/rstudio/rstudio-conf/blob/e5a94beb569dee1cf55748e85020645ccbdbba57/2025/README.md#positconf2025">Posit conf github</a> to see if links are updated in the future:</p>
<ul>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745351601963001aBKi">Positron</a></p>
<details>
<summary>
<p>Which included:</p>
</summary>
<ul>
<li><a href="https://github.com/isabelizimm/talk-positconf2025-ide-choices/blob/main/identitycrisis.pdf">IDE-ntity Crisis: Choosing the Right Tool for Me</a> by Isabel Zimmerman</li>
<li>Tips &amp; tricks from the maintainers of Positron by Sharon Wang and Melissa Barca</li>
<li><a href="https://github.com/austin3dickey/ssh-demo/blob/main/slides.pdf">Outgrowing Your Laptop with Positron</a> by Austin Dickey</li>
<li>Exploring Datasets in Positron by Wes Mckinney</li>
</ul>
</details></li>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745351143742001aTfb">Lightning Talks</a></p>
<details>
<summary>
<p>Which included:</p>
</summary>
<ul>
<li><a href="https://github.com/smach/positconf_2025">Use Your Data Skills for Good: Ideas for Community Service</a> by Sharon Machalis</li>
<li>Make Big Geospatial Data Accessible with Arrow by Cari Gostic</li>
<li>Approaching Positron from RStudio by Mauro Lepore</li>
<li><a href="https://gordonwoodhull.github.io/brand-yml-lightning-talk/">Brand YML and Dark Mode in Quarto</a> by Gordon Woodhull</li>
<li>Automating Event Scheduling with Python in Positron by Becky Hodge</li>
<li>Putting an {ellmer} AI in production with the blessing of IT by Andrie de Vries</li>
<li>Enabling geospatial workflow management with targets: an R package origin story by Eric Scott</li>
<li>Plotgardener – Genomic Data Visualization Made Easy by Rishabh Sharma Vemuri and Abiye Berhanu</li>
<li>What we’re doing to make Quarto fast(er) by Carlos Scheidegger</li>
<li>Multiple Console Sessions in Positron by Dhruvi Sompura</li>
<li>It’s all fun and games til your analysis code is finished: the player package in R by Alex Rossell Hayes</li>
<li>Birthing the pregnancy package by Ella Kaye</li>
</ul>
</details></li>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745351602333001a8qL">Sparking Development Joy</a></p>
<details>
<summary>
<p>Which included:</p>
</summary>
<ul>
<li>Enemies to lovers: How non-programmers can make sparks fly when using testthat during package development by Libby McKenna</li>
<li>Air - A blazingly fast R code formatter by Davis Vaughan and Lionel Henry</li>
<li>Making Things Nice in Python by Rich Iannone</li>
<li>The Curse of Documentation by Michael Chow</li>
</ul>
</details></li>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745350850328001opY2">Keynote: The Psychology of Technologists</a> by Cat Hicks</p></li>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745351601442001aZMp">Strengthening the R Ecosystem</a></p>
<details>
<summary>
<p>Which included:</p>
</summary>
<ul>
<li><a href="https://shikokuchuo-posit2025.share.connect.posit.cloud/#/title-slide">Purrrfectly parallel, purrrfectly distributed</a> by Charlie Gao</li>
<li>R-multiverse: a new way to publish R packages by Will Landau</li>
<li><a href="https://github.com/shannonpileggi/practical-renv">Practical {renv}</a> by Shannon Pileggi</li>
<li>Extending the horizons of R with Rust by Andrés Quintero</li>
</ul>
</details></li>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745351601735001awFt">Facepalm-driven Development: Learning From AI and Human Errors</a></p>
<details>
<summary>
<p>Which included:</p>
</summary>
<ul>
<li><a href="https://github.com/juliasilge/get-unstuck-with-python">How I got unstuck with Python</a> by Julia Silge</li>
<li>Hacking Productivity with LLMs: What Works (and What Doesn’t) by Nic Crane</li>
<li>AI missteps as stepping stones: Opportunities gained when your LLM coding assistant gets it wrong by Ryan Timpe</li>
<li>Failure (and Mistakes) by Laura Gast</li>
</ul>
</details></li>
<li><p><a href="https://reg.conf.posit.co/flow/posit/positconf25/attendee-portal/page/sessioncatalog/session/1745351601804001amYw">Multilingual Data Science</a></p>
<details>
<summary>
<p>Which included:</p>
</summary>
<ul>
<li>Building Multilingual Data Science Teams by Michael Thomas</li>
<li>Polyglot Data Science: Why and How to Combine R and Python by Jeroen Janssens</li>
<li>When R Met Python: A Meet Cute on Posit Connect by Blake Abbenante</li>
<li>R &amp; Python playing nice, in production by Claudia Penaloza</li>
</ul>
</details></li>
</ul>
</section>
<section id="the-georgia-aquarium" class="level2">
<h2 class="anchored" data-anchor-id="the-georgia-aquarium">The Georgia Aquarium</h2>
<p>This was hands-down the BEST social event in conf history (for me). I’m biased as I love water, but this event was great – and the food was surprisingly good. Posit rented out the Georgia Aquarium for us at night, and I had way more fun than expected. It gave me a chance to talk to people I hadn’t run into at conf yet, and having the run of the place at night was awesome.</p>
<p><img src="https://www.thetidytrekker.com/post/vibe-conf-ing/images/aquarium.png" class="blog-img img-fluid" alt="Three polaroid-style photos arranged in a scattered layout showing marine life: a circular aquarium tank with fish and coral from above, sharks swimming in blue water, and more sharks underwater with light streaming down."></p>
<figcaption>
I had the BEST time at the aquarium
</figcaption>
</section>
<section id="tidy-dev-day-tdd" class="level2">
<h2 class="anchored" data-anchor-id="tidy-dev-day-tdd">Tidy Dev Day (TDD)</h2>
<p>Ok y’all. I have a confession: this was my favorite thing about conf HANDS DOWN, even though it technically happened after. As I mentioned, I’ve been discovering I love development work, and Tidy Dev Day (TDD) gave me the PERFECT opportunity to explore this further in a low-stress, supportive environment.</p>
<p>The day started with an ice-breaker bingo sheet, then a group photo, followed by the Posit team explaining the workflow (forking/cloning/PRs, etc.) before turning us loose to work.</p>
<p>We could jump right in because the Posit team does reconnaissance beforehand, looking through <a href="https://www.tidyverse.org">tidyverse</a>, <a href="https://r-lib.r-universe.dev/packages">r-lib</a>, and <a href="https://www.tidymodels.org">tidymodels</a> issues good for beginners. They print snippets on post-it notes for the “issue wall” where participants choose what to work on. Once you open a PR, you get to ring “The Gong” – and if you open multiple PRs, you ring it multiple times:</p>
<p><img src="https://www.thetidytrekker.com/post/vibe-conf-ing/images/gong.jpg" class="blog-img img-fluid" alt="Me smiling with ombre blue-tinted hair wearing a turquoise posit conf shirt and glasses, holding a mallet next to a large bronze gong mounted on a black metal stand. The setting is indoors with white curtains and wooden flooring in the background."></p>
<figcaption>
My first time ringing “The Gong”
</figcaption>
<p>Before everyone really dove in, Hadley reassured everyone by saying something akin to:</p>
<blockquote class="blockquote">
<p>“I have written a lot of shitty code. It’s OK if your code is shitty. We’re here to learn.”</p>
<p>— Hadley Wickham</p>
</blockquote>
<p>I have 7+ years of R programming under my belt, so while I’m confident tackling obscure problems, I admit when my code might be “shitty.” One example? I constantly struggle to write concisely (my coworkers can confirm I’m anything but brief). Going into TDD, I tried cutting through brain noise to make my annotations and documentation short and sweet… only to be advised to add MORE info in places 😅. What I loved was how feedback was delivered – positive or clever contributions were celebrated and corrections came with grace and compassion. This gave me confidence to contribute to tidyverse packages more often, which I’m making a goal moving forward.</p>
<p>As the day progressed, I continued vibing and working, stopping only for provided snacks and lunch. Looking back, I was nervous beforehand about what skills I’d need to be “useful” to the tidyverse team. I can confidently say that basic GitHub functionality is enough, and if you lack that, they provide virtual “office hours” before TDD to get you ready. For direct function work, knowing how to debug is super helpful – Shannon Pileggi has workshops on this <a href="https://www.pipinghotdata.com/workshops/2022-11-11-debugging">here</a>.</p>
<p>By day’s end, I opened 4 PRs with 2 accepted and merged so far for <a href="https://ggplot2.tidyverse.org">ggplot2</a>, <a href="https://dplyr.tidyverse.org">dplyr</a>, <a href="https://purrr.tidyverse.org">purrr</a>, and <a href="https://cli.r-lib.org/index.html">cli</a> – all packages I use frequently for my <a href="https://github.com/Meghansaha/artpack">artpack</a> development.</p>
<p><img src="https://www.thetidytrekker.com/post/vibe-conf-ing/images/tdd.png" class="blog-img img-fluid" alt="Two polaroid-style photos showing myself holding a metal hexagonal conference badge. The left photo shows one side of the badge that reads'DEV DAY 2025 ATLANTA GEORGIA SEPTEMBER 19 2025'. The right photo myself holding the other side of badge with colorful dots reading 'tidyverse' in gold text. Both photos show a filled out Tidy Dev Day bingo sheet in the background"></p>
<figcaption>
My Tidy Dev Day Badge
</figcaption>
<hr>
</section>
</section>
<section id="my-personal-takeaways" class="level1">
<h1>My Personal Takeaways</h1>
<p>Needless to say, I had an awesome time and left with new discoveries and motivation to dig deeper into:</p>
<ul>
<li>Becoming a programming polyglot, focusing on Python and Rust in my downtime</li>
<li>Using <a href="https://posit-dev.github.io/air">Air</a> in my workflow</li>
<li>Transitioning to Positron (this blog post was made in Positron!)</li>
<li>Getting over my fear of contributing to open source packages</li>
<li>Being less self-deprecating (TDD showed me that even if I struggle with brevity, I’m still intelligent. I may not know everything, but I know a lot, and that should be celebrated – even if just internally)</li>
</ul>
<p>As for next year, posit::conf(2026) was announced for Houston, Texas. Not sure how I feel about traveling to Texas, but we’ll see how things shake out. Until then, fellow nerds!</p>


</section>

 ]]></description>
  <category>r</category>
  <category>python</category>
  <category>dev work</category>
  <category>development</category>
  <category>updates</category>
  <category>generative art</category>
  <category>art</category>
  <category>artpack</category>
  <category>rust</category>
  <category>posit</category>
  <category>conf</category>
  <category>tidy dev day</category>
  <category>professional development</category>
  <category>vibe confing</category>
  <guid>https://www.thetidytrekker.com/post/vibe-conf-ing/posit_conf_2025.html</guid>
  <pubDate>Mon, 22 Sep 2025 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/vibe-conf-ing/images/vibeconfing.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Artpack 0.2.0</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0.html</link>
  <description><![CDATA[ 






<p><strong>In this post:</strong></p>
<ul>
<li><p>What is artpack?</p></li>
<li><p>Geospatial Tools</p></li>
<li><p>Group Tools</p></li>
<li><p>Sequencing Tools</p></li>
<li><p>Transformation Tools</p></li>
<li><p>Color Tools</p></li>
<li><p>More Vignettes</p></li>
<li><p>Reminder</p></li>
</ul>
<hr>
<section id="what-is-artpack" class="level1">
<h1>What is artpack?</h1>
<p>artpack is a “new-ish” R package created to help generative artists of all levels create generative art in R. The artpack package is intended for use with the <a href="https://www.tidyverse.org/">tidyverse</a> suite, more specifically with the <a href="https://ggplot2.tidyverse.org/">ggplot2</a> package. artpack aims to simplify the process of creating generative art while providing increased control over the actual data that’s used to create art on a ggplot. artpack does this by providing tools that create and transform data frames that can be mapped onto a ggplot to create art.</p>
<p>You can install artpack from CRAN with:</p>
<div class="cell">
<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;">"artpack"</span>)</span></code></pre></div>
</div>
<p>You can install the development version of artpack from GitHub with:</p>
<div class="cell">
<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;"># install.packages("devtools")</span></span>
<span id="cb2-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;">install_github</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Meghansaha/artpack"</span>)</span></code></pre></div>
</div>
<p>This post will cover the main new functions included in 0.2.0 that were not present in the original 0.1.0 CRAN release.</p>
<p>You can find all the changes implemented in the release notes <a href="https://meghansaha.github.io/artpack/news/index.html">here</a>.</p>
</section>
<section id="geospatial-tools" class="level1">
<h1>Geospatial Tools</h1>
<p>New to artpack is a geospatial function, <code>point_in_polygon()</code>, which analyzes points relative to a polygon. It was created in the spirit of <a href="https://edzer.github.io/sp/">{sp}</a>’s <code>point.in.polygon()</code> function. The sp package is currently being deprecated, so <code>point_in_polygon()</code> attempts to provide similar results by using the more stable <a href="https://r-spatial.github.io/sf/">{sf} package</a> instead. <code>point_in_polygon()</code> is a wrapper for various sf functions and returns a numeric vector of 0’s and 1’s where a value of 0 indicates that a point is outside of the polygon being tested and a value of 1 indicates that the point is either inside or on the border of the polygon being tested.</p>
<div class="cell">
<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>(dplyr)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(artpack)</span>
<span id="cb3-4"></span>
<span id="cb3-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create test points and polygon for visualization</span></span>
<span id="cb3-6">df_points_prep <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.0</span>),</span>
<span id="cb3-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.0</span>)</span>
<span id="cb3-10">  )</span>
<span id="cb3-11">df_polygon <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb3-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</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;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>),</span>
<span id="cb3-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-15">  )</span>
<span id="cb3-16"></span>
<span id="cb3-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test the points and add labels for the plot</span></span>
<span id="cb3-18">df_points <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb3-19">  df_points_prep <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb3-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">point_in_polygon</span>(x, y, df_polygon<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, df_polygon<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y),</span>
<span id="cb3-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position_string =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_match</span>(position,</span>
<span id="cb3-23">                                 <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <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;">"Outside"</span>,</span>
<span id="cb3-24">                                 <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Inside/On Boundary"</span></span>
<span id="cb3-25">                                 ) <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;">factor</span>(),</span>
<span id="cb3-26">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_match</span>(position,</span>
<span id="cb3-27">                       <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <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;">"#e31a1c"</span>,</span>
<span id="cb3-28">                       <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#33a02c"</span></span>
<span id="cb3-29">                       )</span>
<span id="cb3-30">  )</span>
<span id="cb3-31"></span>
<span id="cb3-32"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pull out the colors for the plot points</span></span>
<span id="cb3-33">unique_df <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>(df_points[<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;">"position_string"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span>)])</span>
<span id="cb3-34">vec_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;">setNames</span>(unique_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color, unique_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>position_string)</span>
<span id="cb3-35"></span>
<span id="cb3-36"></span>
<span id="cb3-37"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot it</span></span>
<span id="cb3-38"><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="cb3-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df_polygon, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y),</span>
<span id="cb3-40">               <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;">"#104d70"</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.3</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</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="cb3-41">  <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;">data =</span> df_points, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> position_string), <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;">4</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-42">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> vec_colors) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-43">  <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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Position:"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb3-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/pip-ex-1.png" class="img-fluid figure-img" alt="A scatter plot demonstrating point-in-polygon detection with a rectangular boundary. The plot shows a light blue rectangle with black borders spanning from x=0 to x=2 and y=0 to y=2. Four green points are plotted inside or on the boundary of the rectangle. Two red points are plotted outside the rectangle. A legend indicates green points represent Inside/On Boundary positions and red  points represent Outside positions." width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
</section>
<section id="group-tools" class="level1">
<h1>Group Tools</h1>
<p>If you’re a fan of using <code>geom_polygon()</code> in your generative art, chances are you know the importance of assigning grouping variables to your data where applicable. The <code>group_sample()</code> and <code>group_slice()</code> functions have been added to allow users to sample and slice their data frames by grouping variables. <code>group_sample()</code> and <code>group_slice()</code> were both created in the spirit of <a href="https://dplyr.tidyverse.org/reference/slice.html">dplyr’s <code>slice()</code></a> function family. Just like how <code>slice()</code> works on single rows, <code>group_sample()</code> and <code>group_slice()</code> will always keep the rows of a group intact. So if you decide to sample 2 groups out of 5, all of the rows labeled by the two groups will be returned while the other rows will be dropped. The same is true for <code>group_slice()</code>.</p>
<p><br></p>
<p>We can demonstrate <code>group_sample()</code> and <code>group_slice()</code> with a simple grid, where each square is its own group:</p>
<div class="cell">
<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;"># Create a grid</span></span>
<span id="cb4-2">df_grid <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grid_maker</span>(</span>
<span id="cb4-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>),</span>
<span id="cb4-6">    <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;">10</span>,</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill_pal =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">art_pals</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sunnyside"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb4-8">  )</span>
<span id="cb4-9"></span>
<span id="cb4-10">df_grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-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>(x,y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb4-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> df_grid<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fill,</span>
<span id="cb4-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;">"#000000"</span></span>
<span id="cb4-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-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="cb4-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/grid_group_ex-1.png" class="img-fluid figure-img" alt="A 10x10 grid of colored squares displaying a gradient from bright magenta at the top to golden yellow at the bottom. Each row contains 10 squares of the same color, with colors transitioning smoothly through pink, coral, orange-red, orange, and yellow tones. Black grid lines separate each square." width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Sampling 50% of the groups:</p>
<div class="cell">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">09132025</span>)</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a sampled df</span></span>
<span id="cb5-4">df_grid_sampled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb5-5">  df_grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb5-7"></span>
<span id="cb5-8">df_grid_sampled <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-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>(x,y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb5-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> df_grid_sampled<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fill,</span>
<span id="cb5-12">    <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;">"#000000"</span></span>
<span id="cb5-13">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-14">  <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="cb5-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/group_sample_ex-1-1.png" class="img-fluid figure-img" alt="A scattered arrangement of colored squares showing the same color gradient as the previous grid, but with an irregular, fragmented pattern. The squares are distributed across the canvas in various cluster sizes, maintaining the color progression from bright magenta at the top to golden yellow at the bottom, but with gaps and asymmetrical groupings creating a more organic, broken-up appearance." width="672"></p>
</figure>
</div>
</div>
</div>
<p>Sampling only 10 of the groups:</p>
<div class="cell">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">09132025</span>)</span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a sampled df</span></span>
<span id="cb6-4">df_grid_sampled <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb6-5">  df_grid <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;">group_sample</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group, <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;">10</span>)</span>
<span id="cb6-7"></span>
<span id="cb6-8">df_grid_sampled <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-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>(x,y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb6-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> df_grid_sampled<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fill,</span>
<span id="cb6-12">    <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;">"#000000"</span></span>
<span id="cb6-13">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-14">  <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="cb6-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/group_sample_ex-2-1.png" class="img-fluid figure-img" alt="A sparse arrangement of colored squares using the same gradient palette. The pattern shows only a few small clusters: bright magenta squares in the upper right forming a cross-like shape, two isolated orange-red squares in the middle area, and a small cluster of golden yellow squares at the bottom left. Most of the canvas is empty white space." width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Slicing the data so that the groups in the bottom half is removed:</p>
<div class="cell">
<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;"># Create a sliced df</span></span>
<span id="cb7-2">df_grid_slice <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb7-3">  df_grid <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_slice</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prop =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tail"</span>)</span>
<span id="cb7-5"></span>
<span id="cb7-6">df_grid_slice <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-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>(x,y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> df_grid_slice<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fill,</span>
<span id="cb7-10">    <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;">"#000000"</span></span>
<span id="cb7-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-12">  <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="cb7-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/group_slice_ex-1-1.png" class="img-fluid figure-img" alt="A partial grid showing only the top portion of the color gradient, displaying 5 rows of 10 squares each. Colors transition from bright magenta in the top rows through pink tones to coral-red in the bottom row. The rest of the canvas below is empty white space, creating a truncated version of the full gradient." width="672"></p>
</figure>
</div>
</div>
</div>
<p>Slicing the data so that 25 of the groups from the bottom remain:</p>
<div class="cell">
<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 sliced df</span></span>
<span id="cb8-2">df_grid_slice <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb8-3">  df_grid <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;">group_slice</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group, <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;">25</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"head"</span>)</span>
<span id="cb8-5"></span>
<span id="cb8-6">df_grid_slice <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-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>(x,y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(</span>
<span id="cb8-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> df_grid_slice<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>fill,</span>
<span id="cb8-10">    <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;">"#000000"</span></span>
<span id="cb8-11">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-12">  <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="cb8-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylim =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/group_slice_ex-2-1.png" class="img-fluid figure-img" alt="A partial grid showing only the bottom portion of the color gradient, displaying 3 rows of squares in orange and yellow tones. The top row shows 5 orange squares, while the bottom two rows show 10 squares each transitioning from orange to golden yellow. Most of the canvas above is empty white space.&quot;" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
</section>
<section id="sequencing-tools" class="level1">
<h1>Sequencing Tools</h1>
<p><code>seq_bounce()</code> generates a regular sequence of numeric values that “bounce” between a provided “start” and “end” number. It will always return a numeric vector of the length provided.</p>
<p>By default, <code>seq_bounce()</code> creates sequences by increments of 1:</p>
<div class="cell">
<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;">#The length argument accepts any positive integer</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_bounce</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_n =</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;">end_n =</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;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 1 2 3 4 5 4 3 2 1 2 3 4 5 4 3</code></pre>
</div>
</div>
<p>More precision can be obtained with the <code>by</code> argument:</p>
<div class="cell">
<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;">#The by argument accepts any positive numeric</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_bounce</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_n =</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;">end_n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</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;">247</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.000 0.247 0.494 0.741 0.988 1.235 1.482 1.729 1.976 2.223 2.470 2.717
[13] 2.964 3.211 3.458 3.705 3.952 4.199 4.446 4.693 4.940 5.187 5.434 5.681
[25] 5.928 6.175 6.422 6.669 6.916 7.163</code></pre>
</div>
</div>
<p><br></p>
</section>
<section id="transformation-tools" class="level1">
<h1>Transformation Tools</h1>
<p><code>resizer()</code> is artpack’s newest transformation tool. It can be used to “resize” or scale existing data points in a data frame based on either the first data point in the dataframe or a provided anchor point.</p>
<p>Resizing a square up by a factor of 6:</p>
<div class="cell">
<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;"># Make a data frame</span></span>
<span id="cb13-2">df_square <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">square_data</span>(</span>
<span id="cb13-4">    <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;">0</span>,</span>
<span id="cb13-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb13-6">    <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>
<span id="cb13-7">  )</span>
<span id="cb13-8"></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Resize it</span></span>
<span id="cb13-10">df_square_resized <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb13-11">  df_square <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;">resizer</span>(x, y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">factor =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>)</span>
<span id="cb13-13"></span>
<span id="cb13-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot them</span></span>
<span id="cb13-15">df_square <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb13-16">  <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>(x,y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-17">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># resized square - red dashed line</span></span>
<span id="cb13-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df_square_resized, <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;">"#a83246"</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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="cb13-19">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original square - black solid line</span></span>
<span id="cb13-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</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;">"#000000"</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;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb13-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/resizer-square-1.png" class="img-fluid figure-img" alt="A scatter plot with a rectangular boundary outlined in black, positioned in the lower-left corner from coordinates (0,0) to approximately (1,1). Dark red dashed lines form a grid pattern across the entire plot area, creating a regular pattern of horizontal and vertical lines extending from x=0 to x=7 and y=0 to y=6. The rectangular boundary appears as a small black-outlined box within this larger grid system." width="672"></p>
</figure>
</div>
</div>
</div>
<p>Resizing a circle down by a factor of 3 and manually setting an anchor point:</p>
<div class="cell">
<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;"># Make a dataframe</span></span>
<span id="cb14-2">df_circle <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">circle_data</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;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">radius =</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;">group_var =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set the anchor point as the middle of the circle c(5,5)</span></span>
<span id="cb14-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Although the point 5,5 is in the circle's bounds,</span></span>
<span id="cb14-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># it's not actually a row in `df_circle`</span></span>
<span id="cb14-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># A message will display in cases like these and is "fine" to ignore.</span></span>
<span id="cb14-9"></span>
<span id="cb14-10">df_circle_resized <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb14-11">  df_circle <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">resizer</span>(x,y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x_anchor =</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;">y_anchor =</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;">direction =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"down"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">factor =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>! The anchor point you've supplied (5, 5) is not found in your data.
ℹ The data will be scaled relative to this external point</code></pre>
</div>
</div>
<div class="cell">
<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;"># Plot it</span></span>
<span id="cb16-2">df_circle <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb16-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>(x,y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># resized circle - red dashed line</span></span>
<span id="cb16-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> df_circle_resized, <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;">"#a83246"</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</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="cb16-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># original circle - black solid line</span></span>
<span id="cb16-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</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;">"#000000"</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;">8</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb16-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/resizer-circle-2-1.png" class="img-fluid figure-img" alt="A plot showing two concentric circles centered at coordinates (5,5). The outer circle has a solid black boundary extending from approximately 0 to 10 on both axes. Inside is a smaller dashed circle made up of dark red dashed line segments, positioned in the center of the outer circle and roughly half its diameter." width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
</section>
<section id="color-tools" class="level1">
<h1>Color Tools</h1>
<p>Two new functions, <code>set_brightness()</code> and <code>set_saturation()</code>, have been added to artpack. Both can be used to adjust original colors by percentage of desired brightness and saturation. These functions work by transforming <a href="https://en.wikipedia.org/wiki/Web_colors">hexadecimal webcolor</a> values or any value from <code>colors()</code> into an RGB value, and then an HSL value to adjust the brightness or saturation. After the adjustment, the values are converted into a hexadecimal webcolor that is then returned to the environment for use.</p>
<p>These functions use a normalized scale of 0 to 1, meaning that 0% is the darkest/least saturated value in relation to the original color provided and 100% is the brightest/most saturated value in relation to the original color provided.</p>
<p>An example of adjusting the brightness of a color. Note how the “darker” color shown is not much darker than the original, because the original color already had a brightness of 35%:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create color values</span></span>
<span id="cb17-2">original_color <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;">"#94321c"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(original brightness == %35)</span></span>
<span id="cb17-3">darker_color <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;">set_brightness</span>(original_color, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(brightness == %30)</span></span>
<span id="cb17-4">lighter_color <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;">set_brightness</span>(original_color, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(brightness == %70)</span></span>
<span id="cb17-5"></span>
<span id="cb17-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make a data frame with the color values</span></span>
<span id="cb17-7">df_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb17-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb17-9">    <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;">0</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="cb17-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb17-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(darker_color, original_color, lighter_color)</span>
<span id="cb17-12">  )</span>
<span id="cb17-13"></span>
<span id="cb17-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a label for clarity</span></span>
<span id="cb17-15">df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">paste</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;">"Darker"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lighter"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color)</span>
<span id="cb17-16"></span>
<span id="cb17-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot to see the brightness changes</span></span>
<span id="cb17-18">df_colors <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-19">  <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>(x,y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <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;">label =</span> df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-21">  <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> df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color, <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;">15</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;">50</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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;">1</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;">ylim =</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="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="cb17-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/brightness-1.png" class="img-fluid figure-img" alt="Three color swatches demonstrating brightness variations of a reddish-brown color. From left to right: a darker version (#812B18), the original color (#94321c), and a lighter version (#E7917E). Each swatch is labeled with its corresponding hex color code above it, showing how the same base color appears when darkened and lightened.&quot;" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Saturation is adjusted similarly:</p>
<div class="cell">
<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;"># Create color values</span></span>
<span id="cb18-2">original_color <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;">"#748c3f"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(original saturation == %38)</span></span>
<span id="cb18-3">desaturated_color <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;">set_saturation</span>(original_color, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(saturation == %20)</span></span>
<span id="cb18-4">saturated_color <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;">set_saturation</span>(original_color, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(saturation == %90)</span></span>
<span id="cb18-5"></span>
<span id="cb18-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make a data frame with the color values</span></span>
<span id="cb18-7">df_colors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span></span>
<span id="cb18-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb18-9">    <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;">0</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="cb18-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb18-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(desaturated_color, original_color, saturated_color)</span>
<span id="cb18-12">  )</span>
<span id="cb18-13"></span>
<span id="cb18-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a label for clarity</span></span>
<span id="cb18-15">df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label <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;">paste</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;">"Desaturated"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Saturated"</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">":"</span>, df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color)</span>
<span id="cb18-16"></span>
<span id="cb18-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Plot to see the saturation changes</span></span>
<span id="cb18-18">df_colors <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb18-19">  <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>(x,y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</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> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <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;">label =</span> df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>label) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-21">  <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> df_colors<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>color, <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;">15</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;">50</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_cartesian</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlim =</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;">1</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;">ylim =</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="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="cb18-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>()</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0_files/figure-html/saturation-1.png" class="img-fluid figure-img" alt="Three color swatches demonstrating saturation variations of a green color. From left to right: a desaturated grayish-green (#6D7A51), the original olive-green color (#748c3f), and a highly saturated bright green (#88C10A). Each swatch is labeled with its corresponding hex color code above it, showing how the same base color appears with different saturation levels." width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>You might be asking how you could determine the original brightness or saturation of a color you provide. Currently, there is no function in artpack that does this, but it is planned for a future release.</p>
<p>In the meantime, if needed, you can absolutely use other methods to determine this. Take the color “#748c3f” for example. Going to Google.com and using their <a href="https://www.google.com/search?q=google+color+picker">“Color picker” tool</a> can let you easily see these values:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/artpack-0-2-0/images/google_picker.png" class="img-fluid figure-img"></p>
<figcaption>Google color picker tool showing color #748c3f with HSL values displayed in bottom right: H:82°, S:38%, L:40%</figcaption>
</figure>
</div>
<p>An “HSL” value is provided in the bottom right of the picker tool. The second value, “S”, is the saturation percentage of the color (38%), and the third value, “L”, is the “lightness” which is essentially the brightness (40%).</p>
</section>
<section id="more-vignettes" class="level1">
<h1>More Vignettes</h1>
<p>The last update you might want to check out are the new and improved vignettes:</p>
<ul>
<li><p><em><a href="https://meghansaha.github.io/artpack/articles/brief_examples.html">Brief Examples</a></em> has an updated example of some art that can be made with artpack.</p></li>
<li><p><em><a href="https://meghansaha.github.io/artpack/articles/geom_chart.html">Connecting artpack Assets to ggplot2 Geoms</a></em> is a new vignette that goes over the various artpack <a href="https://meghansaha.github.io/artpack/reference/index.html#asset-creation">asset creations</a> and their recommended ggplot2 <a href="https://ggplot2.tidyverse.org/reference/index.html#geoms">geoms</a> that can be used when creating art in R with artpack and ggplot2. It also gives a quick example of how to bring your newly created art data into a ggplot if you need that information.</p></li>
<li><p><em><a href="https://meghansaha.github.io/artpack/articles/see_artpack_in_action.html">See aRtpack in Action</a></em> is a new vignette that shows you how to make an art piece from start to finish, which may be helpful for anyone just starting or those who just need an example of an entire workflow that’s possible with artpack.</p></li>
</ul>
</section>
<section id="reminder" class="level1">
<h1>Reminder</h1>
<p>artpack is still in its infancy and is currently developed and maintained by one person. Some weird stuff may happen when using artpack. If you come across any bugs or have any ideas about new features you’d want to see implemented, or recommend improvements, please do so on artpack’s github repository by opening a new issue <a href="https://github.com/Meghansaha/artpack/issues/new">here</a>.</p>


</section>

 ]]></description>
  <category>r</category>
  <category>cran</category>
  <category>dev work</category>
  <category>development</category>
  <category>updates</category>
  <category>generative art</category>
  <category>art</category>
  <category>artpack</category>
  <guid>https://www.thetidytrekker.com/post/artpack-0-2-0/artpack-0-2-0.html</guid>
  <pubDate>Wed, 17 Sep 2025 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/artpack-0-2-0/images/artpack_logo.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Git Stash for Newbies.</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/git-stash-for-newbies/git-stash-for-newbies.html</link>
  <description><![CDATA[ 






<p><strong>In this post:</strong></p>
<ul>
<li><p>What is a Git Stash?</p></li>
<li><p>Why Would I Want to Use <code>git stash</code>?</p></li>
<li><p>How can I Use <code>git stash</code>?</p></li>
<li><p>A Rundown of <code>git stash</code> Commands</p></li>
<li><p>Stashes in the Wild</p></li>
<li><p>Some Additional Things You May Want to Know</p></li>
</ul>
<hr>
<section id="what-is-a-git-stash" class="level1">
<h1>What is a Git Stash?</h1>
<p>A <code>git stash</code> is exactly what it sounds like. It’s a command in Git that allows you to save (stash away) any changes that are uncomitted in your current directory. Stashes are unique and centralized to each repository. In comparison, <code>git push</code> is used to get staged changes that are committed onto the remote repository.</p>
<center>
<p><img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/pushvsstash.png" class="img-fluid" alt="a diagram showing that pushed commits go to a remote repository and stashed changes go to a local folder called .refs"></p>
<fig><code>git push</code> moves staged changes to your remote. <code>git stash</code> can move any changes to the local <code>refs</code> folder</fig>
</center>
<p><br> <br></p>
</section>
<section id="why-would-i-want-to-use-git-stash" class="level1">
<h1>Why Would I Want to Use <code>git stash</code>?</h1>
<p>Git stashes can help you maintain a clean git/working environment. You would usually want to use git stashes when you need to cleanse your current directory of whatever unstaged changes you have. One of the most common use cases for git stash is when you have changes that you don’t want to commit to your current branch but need to switch to another branch. As stated in my talk, <em><a href="https://meghansaha.github.io/please_let_me_merge/#/title-slide">Please Let Me Merge Before I Start Crying</a></em>, I believe that emergencies aren’t a thing. Still, sometimes, a situation can arise when you have to stop to pivot and work on something else within the same repository.</p>
<center>
<p><img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/lisa.png" class="img-fluid" alt="An image of Lisa Simpson rock and back and forth on the floor."></p>
<fig>POV of me when I’m asked to pivot to a different deliverable for the 5th time in a day</fig>
</center>
<p><br></p>
<p>You might also want to use git stash if you’ve started working on a branch but realized you never pulled down updates from the remote repository. Instead of committing your work in progress, you can keep your commit history clean (<a href="https://happygitwithr.com/repeated-amend">which is important!</a>) by stashing your current changes, and then bringing the changes in to resolve any potential conflicts.</p>
<p><br> <br></p>
</section>
<section id="how-can-i-use-git-stash" class="level1">
<h1>How Can I Use <code>git stash</code>?</h1>
<p><br></p>
<section id="stashing-with-a-terminal" class="level2">
<h2 class="anchored" data-anchor-id="stashing-with-a-terminal">Stashing with A Terminal</h2>
<p>If you’re an R user like myself, you may favor using the RStudio IDE (Integrated Development Environment). While great for many things, it unfortunately does not have a built-in graphical user interface (GUI) for git stashes at the time of writing this. This means there are no accessible buttons to click to create a git stash like you would for creating a commit, pushing and pulling to repositories, or other standard git functions. If you use RStudio, the most accessible way to create and use git stashes within the IDE is to use the embedded terminal:</p>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/rstudioterminal.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash -m 'My first stash'"> <fig>Making a stash in RStudio’s Git Terminal</fig>
</center>
<p><br></p>
<p>If you’re not using Rstudio, you can use any terminal/Command Line Interface (CLI) you’d like. This could be Powershell on Windows operating systems, Terminal on Macs, or even Bash on Linux systems. The potential benefit of using a terminal is increased functionality with the possibility of a steeper learning curve or decreased accessibility due to the difficulty of learning to script in CLIs for most users.</p>
<p><br></p>
</section>
<section id="stashing-with-third-party-gui-clients" class="level2">
<h2 class="anchored" data-anchor-id="stashing-with-third-party-gui-clients">Stashing with Third-party GUI Clients</h2>
<p>If you don’t feel like being bothered with terminals or just want to use a pretty GUI for your Git operations, you can always use third-party programs like&nbsp;<a href="https://desktop.github.com/download/">GitHub Desktop</a>, <a href="https://www.sourcetreeapp.com">SourceTree</a>, or <a href="https://www.gitkraken.com">GitKraken</a>. These GUI clients absolutely take some of the pain out of doing the most common git procedures, stashes included.</p>
<p>Different IDEs like Positron and VS Code can also be considered third-party GUI clients that allow you to use git stashes with git-flavored extensions from the <a href="https://open-vsx.org/">OpenVSX Marketplace</a> (for Positron) and the <a href="https://marketplace.visualstudio.com/vscode">VS Code Marketplace</a> (for VS Code).</p>
<p>Because third-party GUI clients can vary widely in their user interfaces and functionality, this post will only go over how to create and work with stashes via the Git terminal. Even if you are using a GUI, reading about the commands a git stash can take and what each of them do can help give a better understanding of stashes and what specific actions you’ll need for your work. If you’re not using the terminal, it’s always a good idea to explore what functionalty your GUI client has as it can vary. For most users, the functionality a GUI client can provide is usually efficient for most use cases. However, if you start running into any friction while using a GUI client, it may be time to consider using a terminal if you haven’t already.</p>
<p><br> <br></p>
</section>
</section>
<section id="a-rundown-of-git-stash-commands" class="level1">
<h1>A Rundown of <code>git stash</code> Commands</h1>
<p>If you use the Git terminal to do your stash work, you can find the most essential commands you’ll need to know in the table below. If you’re choosing to use a GUI, you won’t have to type the inputs into a terminal, but rather, the application you’re using will be interacting with Git on your behalf to perform some of these commands for you:</p>
<div class="cell">
<div class="cell-output-display">
<div id="qpfvwisfqc" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#qpfvwisfqc 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;
}

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

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

#qpfvwisfqc .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: 100%;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

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

#qpfvwisfqc .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;
}

#qpfvwisfqc .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;
}

#qpfvwisfqc .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;
}

#qpfvwisfqc .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

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

#qpfvwisfqc .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;
}

#qpfvwisfqc .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;
}

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

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

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

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

#qpfvwisfqc .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: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  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;
}

#qpfvwisfqc .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: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

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

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

#qpfvwisfqc .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  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;
  overflow-x: hidden;
}

#qpfvwisfqc .gt_stub {
  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;
}

#qpfvwisfqc .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;
}

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

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

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

#qpfvwisfqc .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

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

#qpfvwisfqc .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: #D3D3D3;
}

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

#qpfvwisfqc .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: #D3D3D3;
}

#qpfvwisfqc .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: #D3D3D3;
}

#qpfvwisfqc .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#qpfvwisfqc .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#qpfvwisfqc .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;
}

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

#qpfvwisfqc .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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#qpfvwisfqc 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-postprocess="true" data-quarto-disable-processing="false" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 5%">
<col style="width: 20%">
<col style="width: 50%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="command" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong><code>git stash</code><br>Command</strong></th>
<th id="description" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Description</strong></th>
<th id="input" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Type this into the Git Terminal</strong></th>
<th id="why" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>So you can…</strong></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="command"><code>push</code></td>
<td class="gt_row gt_left" headers="description">Saves your local changes to a stash and rolls your working tree back to <code>HEAD</code></td>
<td class="gt_row gt_center" headers="input"><code>git stash push</code></td>
<td class="gt_row gt_left" headers="why">Return your files to what they looked like at your last saved commit before you started making changes, <strong>while</strong> saving your changes into a stash</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="command"><code>list</code></td>
<td class="gt_row gt_left" headers="description">Lists all the stash entries that you have for your current project/repo</td>
<td class="gt_row gt_center" headers="input"><code>git stash list</code></td>
<td class="gt_row gt_left" headers="why">See all of the stashes you have for your repository</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="command"><code>show</code></td>
<td class="gt_row gt_left" headers="description">Show the changes in the stash as a diff between the stash and the most recent commit back when the stash was made</td>
<td class="gt_row gt_center" headers="input"><code>git stash show</code></td>
<td class="gt_row gt_left" headers="why">See a quick summary in the terminal about what you’ve changed in that stash from the last commit</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="command"><code>pop</code></td>
<td class="gt_row gt_left" headers="description">Remove a stash from the stash list and bring it’s changes back into your local directory</td>
<td class="gt_row gt_center" headers="input"><code>git stash pop</code></td>
<td class="gt_row gt_left" headers="why">Integrate your stash changes back into your current files, <strong>while removing</strong> the stash from the stash list because you don’t need it there anymore</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="command"><code>apply</code></td>
<td class="gt_row gt_left" headers="description">Bring changes from a stash back into your local directory while keeping the stash in the stash list</td>
<td class="gt_row gt_center" headers="input"><code>git stash apply</code></td>
<td class="gt_row gt_left" headers="why">Integrate your stash changes back into your current files, <strong>while keeping</strong> the stash in the stash list because you’re not ready to get rid of it</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="command"><code>branch</code></td>
<td class="gt_row gt_left" headers="description">Creates and checks out a new branch starting from the commit at which the stash was originally created while applying the changes of the stash and dropping the stash from the stash list</td>
<td class="gt_row gt_center" headers="input"><code>git stash branch &lt;new_branch_name&gt;</code></td>
<td class="gt_row gt_left" headers="why">Start working on your changes again in a version of the repository that has been rolled back to the last commit at the time the stash was made</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="command"><code>clear</code></td>
<td class="gt_row gt_left" headers="description">Removes all stashes from the stash list</td>
<td class="gt_row gt_center" headers="input"><code>git stash clear</code></td>
<td class="gt_row gt_left" headers="why">Purge your repository of <strong>all</strong> stashes because you’re confident you don’t need them anymore</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="command"><code>drop</code></td>
<td class="gt_row gt_left" headers="description">Removes one stash from the stash list</td>
<td class="gt_row gt_center" headers="input"><code>git stash drop</code></td>
<td class="gt_row gt_left" headers="why">Remove <strong>one</strong> of your repository’s stashes because you’re confident you don’t need it anymore</td>
</tr>
</tbody>
</table>

</div>
</div>
</div>
<p><br> <br></p>
</section>
<section id="stashes-in-the-wild" class="level1">
<h1>Stashes in the Wild</h1>
<p>So it’s one thing to list off the commands and tell you to go “figure it out” because there isn’t “much” to git stashes, but I feel like it would be more helpful to see some examples of using stashes in the wild. Let’s look at some scenarios in which you could use these commands.</p>
<section id="making-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="making-a-stash">Making a Stash</h2>
<p><strong><em>Scenario</em></strong>: You jumped into working on a branch that hasn’t been updated from the remote in a month and of course, you forgot to pull before touching anything. You want to make a stash to keep your current work safe so you can pull the updates in and deal with any conflicts later. You save the stash with a message to give a description of the work you’re saving.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"premature analysis changes before pull"</span></span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/makestash.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash -m 'premature analysis changes before pull'">
</center>
<p><br></p>
</section>
<section id="popping-the-last-stash-saved" class="level2">
<h2 class="anchored" data-anchor-id="popping-the-last-stash-saved">Popping the Last Stash Saved</h2>
<p><strong><em>Scenario</em></strong>: So it turns out you won’t have any conflicts from that last scenario, so you simply want to throw your changes from that last stash back into your environment.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash pop</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/popstash.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash pop">
</center>
<p><br></p>
</section>
<section id="listing-stashes" class="level2">
<h2 class="anchored" data-anchor-id="listing-stashes">Listing Stashes</h2>
<p><strong><em>Scenario</em></strong>: After doing more work, you realize that you’re missing some code you absolutely <em>did</em> write at some point… but can’t find anywhere. You suspect it’s in a different stash but don’t know which one. So you want to see all your available stashes for this project.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash list</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/liststash.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash list">
</center>
<p><br></p>
</section>
<section id="showing-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="showing-a-stash">Showing a Stash</h2>
<p><strong><em>Scenario</em></strong>: You think you’ve found the right stash. The one at index 2, but you’re not entirely sure. You want to see a quick summary to see what changes are on this particular stash.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash show 2</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/showstash.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash show 2">
</center>
<p><br></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">
Important
</div>
</div>
<div class="callout-body-container callout-body">
<p>Sometimes, when you start using some commands like <code>git stash show</code> or <code>git log</code> the terminal may expect you to input something and won’t allow you input anything else. If this happens, you simply need to “escape” to revert the terminal back to it’s original state. You can do so by inputting the letter <code>q</code>.</p>
</div>
</div>
<p><br></p>
</section>
<section id="popping-a-specific-stash" class="level2">
<h2 class="anchored" data-anchor-id="popping-a-specific-stash">Popping a Specific Stash</h2>
<p><strong><em>Scenario</em></strong>: You are sure the stash at index 2 is the correct choice, and you know that whatever work you do today will be good enough to commit to the branch, so you want to pop off that stash to continue your work while removing it from your stash list.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash pop 2</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/popstashspec.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash pop 2">
</center>
<p><br></p>
</section>
<section id="branching-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="branching-a-stash">Branching a Stash</h2>
<p><strong><em>Scenario</em></strong>: You’re starting to realize that the scope of your work has crept away a bit. It now makes sense to have a dedicated branch that’s separate from the current one to continue this work. You want to name the branch “misc-fixes.”</p>
<p>First, make the stash…</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"misc fixes port"</span></span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashbranchprep.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash -m 'misc fixes port'">
</center>
<p><br></p>
<p>… then make the branch.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash branch misc-fixes</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashbranch.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash branch misc-fixes">
</center>
<p><br></p>
</section>
<section id="applying-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="applying-a-stash">Applying a Stash</h2>
<p><strong><em>Scenario</em></strong>: You’re starting your work on the new “misc-fixes” branch and have made solid progress before taking a break from it. When you finally pick it up again, you realize that an ambitious function you wrote may need a tweak, but you’re unsure where or how to implement it. You apply the stash (instead of popping it) so you can freely experiment without losing the integrity of your code. If you mess up, your original code will be in the stash list.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash apply</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashapply.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash apply">
</center>
<p><br></p>
</section>
<section id="pushing-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="pushing-a-stash">Pushing a Stash</h2>
<p><strong><em>Scenario</em></strong>: You’ve switched to a different project someone else was working on because they asked you to help with a data-wrangling problem. Sadly, your brain is fried, and you’re just making it worse. Instead of committing wrong/incomplete code, you decide to push ALL of your changes (even untracked ones) to a stash so you can remove your embarrassment from the branch as if nothing ever happened.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash push <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-u</span></span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashpush.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash push -u">
</center>
<p><br></p>
</section>
<section id="dropping-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="dropping-a-stash">Dropping a Stash</h2>
<p><strong><em>Scenario</em></strong>: You don’t know what you were thinking, bothering to push those changes to a stash. Your code is broken and makes no sense. It’s not even relevant. You want to put the stash out of its misery and ban it to the shadow realm.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash drop</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashdrop.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash drop">
</center>
<p><br></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><span style="color:red"><strong>NOTE</strong></span>: You better be sure you want to do this. Git does not ask you if you’re sure… There are ways to recover stashes you’ve accidentally dropped, but if you’re a git newbie, it’s not pretty, and it’s out of the scope of this blog post.</p>
</div>
</div>
<p><br></p>
</section>
<section id="clearing-a-stash" class="level2">
<h2 class="anchored" data-anchor-id="clearing-a-stash">Clearing a Stash</h2>
<p><strong><em>Scenario</em></strong>: You’ve persevered. Whatever seemingly impossible problem you worked on is now complete and rolled into your main branch. All of the stashes on your project are no longer needed. You want to drop ALL of your stashes in one cathartic input.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb11-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash clear</span></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><span style="color:red"><strong>NOTE</strong></span>: You better be sure you want to do this. Git does not ask you if you’re sure… There are ways to recover stashes you’ve accidentally cleared, but if you’re a git newbie, it’s not pretty, and it’s out of the scope of this blog post.</p>
</div>
</div>
<p><br> <br></p>
</section>
</section>
<section id="some-additional-things-you-may-want-to-know" class="level1">
<h1>Some Additional Things You May Want to Know</h1>
<p><br></p>
<section id="git-stash-apply--vs--git-stash-pop" class="level2">
<h2 class="anchored" data-anchor-id="git-stash-apply--vs--git-stash-pop"><code>git stash apply</code> -vs- <code>git stash pop</code></h2>
<p>If you’re just starting to explore git stashes, it may be confusing to know when to apply or pop a stash. Just remember that both of these commands will bring your stashed changes back into your working directory but will be handled differently in your git stash list:</p>
<center>
<p><img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/gitapply.png" class="img-fluid" alt="a diagram showing that the command git stash apply copies and pastes changes back into the working directory"></p>
<p><fig>You can think of <code>git stash apply</code> as “copying and pasting” your stashed changes into your directory. An intact copy stays in the stash list.</fig></p>
</center>
<p><br></p>
<center>
<p><img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/gitpop.png" class="img-fluid" alt="a diagram showing that the command git stash pop moves changes back into the working directory, while not keeping a copy in the stash list"></p>
<p><fig>You can think of <code>git stash pop</code> as “moving” your stashed changes into your directory. The stash is removed from your stash list.</fig></p>
</center>
<p><br></p>
<p>While <code>git stash apply</code> could be good for the cautious newbie who doesn’t want to get rid of something they may need, know that <code>git stash pop</code> or <code>git stash drop</code> is also important as you don’t want to bloat your git stash list with stale work. You should make it a habit to periodically check your stashes to make sure they’re still needed. In other words, don’t hoard your stashes! <br></p>
</section>
<section id="stashing-staged-changes" class="level2">
<h2 class="anchored" data-anchor-id="stashing-staged-changes">Stashing Staged Changes</h2>
<p>You may have so many changes in your environment at the moment but only want to save/stash a few of them. You can stage those changes and stash them. By default, git does not stash staged changes. You can use this to your advantage to quickly group changes together for a stash:</p>
<p>Either stage them via a GUI, or in the terminal with the <code>-S</code> option:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> add file-to-keep-1.R</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> add file-to-keep-2.R</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two important updates"</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-S</span></span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashstaged.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with three commands: git add file-to-keep-1.R, git add file-to-keep-2.R, git stash -m 'two important updates' -S ">
</center>
<p><br></p>
</section>
<section id="porting-stashed-changes-to-another-branch" class="level2">
<h2 class="anchored" data-anchor-id="porting-stashed-changes-to-another-branch">Porting Stashed Changes to Another Branch</h2>
<p>If, for any reason, you want to bring stashed changes into another branch that is already created, you can do so by switching to (checking out) the branch you want to bring the changes to and either pop or apply the changes to the branch.</p>
<p>If you’re using a GUI, switch to the desired branch. If using the terminal, check the branch out:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> checkout the-other-branch</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashportprep.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git checkout the-other-branch">
</center>
<p><br></p>
<p>Then pop or apply the stash (In this case, the stash at index 3):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash pop 3</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashport.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash pop 3">
</center>
<p><br></p>
</section>
<section id="naming-and-organization" class="level2">
<h2 class="anchored" data-anchor-id="naming-and-organization">Naming and Organization</h2>
<p>The <code>-m</code> option lets you attach a message to a stash. This is a wonderful capability that can help you stay organized. Here’s just an example of some things you can insert into a stash message:</p>
<ul>
<li>The date you’re creating the stash</li>
<li>A brief description of the work</li>
<li>Action items/project statuses</li>
<li>Challenges/things or people you’re waiting on before work can continue</li>
<li>What branch you need to move the changes to if applicable</li>
</ul>
<p><br></p>
</section>
<section id="referencing-stashes" class="level2">
<h2 class="anchored" data-anchor-id="referencing-stashes">Referencing Stashes</h2>
<p>Whenever you use any stash command/option without referencing a specific stash, git will always choose the stash at index 0 in the stash list, which is whatever stash you saved last. The easiest way to reference your stashes is by index. You can easily see which stash is at which index with <code>git stash list</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash list</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/liststash.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash list">
</center>
<p><br></p>
<p>Although not as easy as just referencing the index, you <em>can</em> search for your git stashes via <a href="https://en.wikipedia.org/wiki/Grep#:~:text=grep%20is%20a%20command-line,which%20has%20the%20same%20effect.">grep</a> if you get in the habit of attaching messages to your stashes:</p>
<p>Let’s say you have a stash that you tagged as “IMPORTANT”. When using grep, it is case-sensitive. Meaning we’d input this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash list <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">|</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grep</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"IMPORTANT"</span></span></code></pre></div>
</div>
<p>To find the stash/index we’re looking for (Index 1 in this case):</p>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashgrep.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash list | grep 'IMPORTANT'">
</center>
<p><br></p>
</section>
<section id="looking-at-the-stash-dates" class="level2">
<h2 class="anchored" data-anchor-id="looking-at-the-stash-dates">Looking at the Stash Dates</h2>
<p>Although it’s an option for the <code>git log</code> command, you can use <code>--date</code> to display the date and time the stashes in the stash list were created:</p>
<p>Use <code>--date=local</code> to see the full day and time the stashes were created.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb17-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash list <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>local</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashdatelocal.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash list ==date=local">
</center>
<p><br></p>
<p>Use <code>--date=relative</code> to see a relative time calculation like “35 minutes ago”. This could come in handy if you’ve made a lot of stashes without labeling them well, as you can gauge the time on the stash you’re looking for.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> stash list <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--date</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>relative</span></code></pre></div>
</div>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/stashdaterel.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash list --date=relative">
</center>
<p><br></p>
</section>
<section id="abandoning-ship" class="level2">
<h2 class="anchored" data-anchor-id="abandoning-ship">Abandoning Ship</h2>
<p>Sometimes, popping or applying stashes into your environment may lead to some merge conflicts:</p>
<center>
<img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/abandonshipprep.png" class="img-fluid" alt="A screenshot of an RStudio Git Terminal with the command: git stash apply 2 that has resulted in a merge conflict.">
</center>
<p><br></p>
<p>If this happens, don’t forget that the first rule of resolving conflicts is to NOT panic. If for some reason you can’t help but panic, you can always tell git “nevermind” so you can deal with it later with the following command:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">git</span> reset <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--merge</span></span></code></pre></div>
</div>
<p><br></p>
</section>
<section id="perfection-can-halt-progress" class="level2">
<h2 class="anchored" data-anchor-id="perfection-can-halt-progress">Perfection Can Halt Progress</h2>
<p>One last bit of advice I want you to take from this is to not let fear or perfection stop you from making meaningful contributions to your project. Remember that the utility of git stashes lies in its ability to help you maintain a clean git/project environment. Don’t fall victim to hoarding your work.</p>
<center>
<p><img src="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/charlie.gif" class="img-fluid" alt="That one scene from it's always sunny in philadelphia where Charlie is stressed in front of his planner board."></p>
<fig>POV: Me explaining to someone why I can’t push my work up to the remote yet.</fig>
</center>
<p><br></p>
<p>I am absolutely guilty of waiting to make commits/push work to remotes because it’s not perfect. Things don’t have to be perfect, especially for larger projects. The best approach to tackling large projects that will take a while to complete is designating project milestones/checkpoints. These can be agreed upon “stopping points” in which you will determine whether your work is “good enough for now” so it can be committed instead of stashed away.</p>
<p><br> <br></p>
<p>Hopefully, I’ve given you enough information to start using some git stashes. If you’d like to read the official git stash documentation yourself, you can do so <a href="https://git-scm.com/docs/git-stash">here</a>. If you’d like more resources on git workflows in general, you can checkout the README for my 2024 Posit Conf talk “<em>Please let me Merge Before I Start Crying</em>,” which has <a href="https://github.com/Meghansaha/please_let_me_merge?tab=readme-ov-file#supplemental-resources">plenty of supplemental links</a> to explore.</p>


</section>
</section>

 ]]></description>
  <category>r</category>
  <category>git</category>
  <category>stash</category>
  <category>beginner</category>
  <category>terminal</category>
  <guid>https://www.thetidytrekker.com/post/git-stash-for-newbies/git-stash-for-newbies.html</guid>
  <pubDate>Mon, 17 Feb 2025 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/git-stash-for-newbies/images/gitstash.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Making Circular Maps in ggplot</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/making-circular-maps/making-circular-maps.html</link>
  <description><![CDATA[ <div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/final_irl.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="a picture of a wedding gift. A circular map in blue and yellow colors of the Mackey House in Savannah GA. It reads, The Mackey House, Jessica and Chris. June 10th 2022. Savannah Georgia. Latitude 32.0380. Longitude -81.1716"></p>
</figure>
</div>
<center>
<sub><em>The final result of an R script, printed onto a canvas and given as a wedding gift</em>💕🍑</sub><br>
</center>
<p><br></p>
<center>
<h2 class="anchored">
<strong>tldr:</strong> Don’t want to read and just want the code? View it <a href="https://github.com/Meghansaha/a_wedding_gift/blob/master/scripts/processing.R"><em>here on GitHub</em></a>.
</h2>
</center>
<p><br></p>
<section id="about" class="level1"><h1>About</h1>
<p>So I already <a href="../../post/lets-talk-about-2022/lets-talk-about-2022.html">talked about how insane 2022</a> was for me. A part of the insanity was that I traveled so much, like the most ever. A portion of my travels was to be a maid of honor for my best friend, that got married in Savannah, Georgia (US). It was my first time going to “The South”&nbsp;<em>(Because Disney doesn’t count)</em>, and it was a fun experience. Unfortunately, because I was in the middle of so many things, I didn’t get my friend her present until the end of the year because I’m terrible.</p>
<p>Because I’m the nerd I am, of course, I had to incorporate R somehow. So I got a circular map of where she got married printed on a canvas and shipped to her. I’ll explain how this can be done.</p>
<p><br></p>
<hr></section><section id="get-your-packages" class="level1"><h1>Get Your Packages</h1>
<p>First, you’ll need a few packages. What you’ll need will depend on what you want to do. At the bare minimum, you’ll need the <a href="https://r-spatial.github.io/sf/"><code>sf</code></a>, <a href="https://docs.ropensci.org/osmdata/"><code>osmdata</code></a>, and <a href="https://ggplot2.tidyverse.org/"><code>ggplot2</code></a> packages to make this happen. Despite this, I’ll simply show what I used for this particular example:</p>
<div class="cell">
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Library Load-In--------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/">sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#For getting "simple features" - It makes my maps go brrr in ggplot</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://tidyverse.tidyverse.org">tidyverse</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#For Everything data (dplyr and ggplot specifically)</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/">osmdata</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#For geodata and mapping</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://github.com/yixuan/showtext">showtext</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For using pretty Google fonts</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/">ggtext</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#For easier text aesthetics</span></span>
<span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;"><a href="https://glue.tidyverse.org/">glue</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Also for easier text aesthetics</span></span></code></pre></div>
</div>
<p><br></p>
<hr></section><section id="pick-a-point-of-interest-poi" class="level1"><h1>Pick a Point of Interest (POI)</h1>
<p>We first need to pick a “center” point of interest (POI) of our map. In this example, my friend’s wedding venue was The Mackey House in Savannah, Georgia (US). So I found out those latitude and longitude points and put them in an object, <code>POI</code>, with named values <code>lat</code> and <code>long</code>:</p>
<div class="cell">
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map Options-------------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map's Point of Interest (POI - Center)----------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  lat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">32.03825</span>,</span>
<span>  long <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">81.1716</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p><br></p>
<section id="latitude-vs.-longitude" class="level2"><h2 class="anchored" data-anchor-id="latitude-vs.-longitude">Latitude vs.&nbsp;Longitude</h2>
<p>As stated, you need to designate a POI that will act as the “center” of your circular map. Once you decide on a place, you’ll need to grab the latitude and longitude points and store them for use. One thing that ALWAYS trips me up without fail is discerning which value is latitude and which is longitude. It wasn’t until recently that I learned of the “ladder” memory trick:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/lat_v_long.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="An image of a ladder with purple rungs and red rails making the comparison to latitude (rungs/horizontal) and longitude (rails/vertical) lines on a globe. Latitude is horizontal (east/west) like rungs on a ladder. Longitude is vertical (north/south) like rails on a ladder." width="500"></p>
</figure>
</div>
<center>
<sub><em>I’m embarrassed about how long it took me to remember this</em>🤦🏽‍♀️</sub><br>
</center>
<p><br></p>
</section><section id="acquiring-the-latitude-and-longitude-points" class="level2"><h2 class="anchored" data-anchor-id="acquiring-the-latitude-and-longitude-points">Acquiring the Latitude and Longitude Points</h2>
<p>There are multiple ways to go about figuring out the latitude and longitude points of your POI. The following is <strong>not</strong> an exhaustive list, but just what I’ve used in the past:</p>
<p><br></p>
<section id="google-maps-search" class="level3"><h3 class="anchored" data-anchor-id="google-maps-search">Google Maps Search</h3>
<p>This is the “<em>Manual</em>” way and may be needed if your POI is relatively new to mapping/GPS systems or if it’s obscure. You can simply search for your POI in <a href="https://www.google.com/maps">Google Maps</a>, right-click on the exact point of the POI in the map, and click on the latitude and longitude points Google Maps provides. Clicking on this will automatically copy the values to your clipboard. Remember that these points are displayed in alphabetical order here. Meaning that latitude comes first.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/mackey_map.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="A screenshot from Google Maps showing how right-clicking on a point of the map will provide latitude and longitude point that can be copied to your clipboard. The map is centered on the Mackey House in Savannah, Georgia, in the United States."></p>
</figure>
</div>
<center>
<sub><em>This is absolutely the way I did it because that’s just where my brain was at that day.</em></sub><br>
</center>
<p><br></p>
</section><section id="the-ggmap-package" class="level3"><h3 class="anchored" data-anchor-id="the-ggmap-package">The <a href="https://github.com/dkahle/ggmap"><code>ggmap</code></a> Package</h3>
<p>Suppose you’ve already got a&nbsp;<a href="https://cloud.google.com/">Google Cloud</a> Account. The <code>ggmap</code> package can help grab latitude and longitude points via its <a href="https://www.rdocumentation.org/packages/ggmap/versions/3.0.1/topics/geocode"><code>geocode</code></a> function. This package is convenient but does come with a caveat. Google has recently changed its terms of service and now requires all Google Cloud Accounts to have an active credit card on file before using any generated APIs. As of February 2023, If you have a valid credit card to use and don’t have a lot of data that needs to be geocoded, you can take advantage of a&nbsp;<a href="https://cloud.google.com/free/docs/free-cloud-features">new-member free tria</a>l of $300 worth of free credits for Google’s API services. After that, users can use up to&nbsp;<a href="https://mapsplatform.google.com/pricing/">$200 worth of Google Maps API credits</a>. (Please check this for yourself as things can always change!) This is more than enough credits for standard tasks to use the API sparingly without ever having to pay. Just be mindful not to overuse it. For example, don’t constantly re-run your entire script while debugging and forget it’s calling the API each time. If you aren’t confident about <strong>not</strong> overusing the API, you may want to avoid this option.</p>
<p><br></p>
</section><section id="the-tidygeocoder-package" class="level3"><h3 class="anchored" data-anchor-id="the-tidygeocoder-package">The <a href="https://jessecambon.github.io/tidygeocoder/"><code>tidygeocoder</code></a> Package</h3>
<p>If <code>ggmap</code> is too much of a potential gamble, you can try using the&nbsp;<a href="https://jessecambon.github.io/tidygeocoder/"><code>tidygeocoder</code></a>&nbsp;package. I’m absolutely biased towards <code>tidygeocoder</code> because I used it frequently at the last job that required me to geocode thousands of addresses for my local government. It was really clutch. It also has a handful of different types of <a href="https://jessecambon.github.io/tidygeocoder/reference/index.html">geocoding functions</a> and <a href="https://jessecambon.github.io/tidygeocoder/articles/geocoder_services.html">geolocation services</a> to call from. A downside to this may be a lack of accuracy for some places (compared to Google’s service, but even Google isn’t perfect.) <code>tidygeocoder</code> is worth looking into if you’re interested in doing geospatial tasks like this in R.</p>
<p><br></p>
<hr></section></section></section><section id="make-the-maps-bounding-box" class="level1"><h1>Make the Map’s Bounding Box</h1>
<p>Once we have the POI coordinates, we can make our bounding box. You can think of this as setting the limits we want to see on our map. We’ll also set a “buffer” value to adjust all of the values in our bounding box. Note that you don’t&nbsp;<em>need</em>&nbsp;a buffer value if you don’t want it, but because I’m a GIS newbie, I don’t really understand&nbsp;<a href="https://en.wikipedia.org/wiki/Geographic_coordinate_system#Length_of_a_degree">degree measurements</a>; this was nice to include in the program for trial and error tweaking after everything was finished at the end.</p>
<div class="cell">
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map's bounding Box "buffer" size----------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Helps create the bounds for the entire map#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bb_buff</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.06000</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map's Bounding Box (mbb)------------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mbb</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  minlong <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bb_buff</span>, </span>
<span>  minlat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bb_buff</span>,</span>
<span>  maxlong <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bb_buff</span>, </span>
<span>  maxlat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">bb_buff</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/unname.html">unname</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p><br></p>
<hr></section><section id="pick-your-osm-features" class="level1"><h1>Pick Your OSM Features</h1>
<p>Next, we need to tell the OSM package what “features” we want on our map. These features will translate into relevant&nbsp;<a href="https://support.esri.com/en-us/gis-dictionary/geometry">geometry</a>&nbsp;data that will visually appear as structures (like bodies of water, buildings, or roads) in our image. The list of available features is MASSIVE, so I’d encourage you to view&nbsp;<a href="https://wiki.openstreetmap.org/wiki/Map_Features">this reference</a>&nbsp;and pick the relevant ones for your project. Because my computer is a potato (i.e., slow), I only included five features (buildings, landuse, leisure, natural, and highways). The more features you have and the more populated your POI is with features, the longer it will take for your computer to pull and process the data through the OSM APIs, so balance out what you include in these scripts as you see fit.</p>
<p>I’ve placed my features into objects (<code>feature_names</code> , <code>feature_keys</code>, <code>osm_features</code>) to optimize my code and pulled the data iteratively using&nbsp;<a href="https://purrr.tidyverse.org/reference/map.html">purrr’s map</a>&nbsp;function. From the osmdata package, we’ll need to use the following functions to grab this data from the servers:</p>
<ul>
<li><p><code><a href="https://docs.ropensci.org/osmdata/reference/opq.html">opq()</a></code> - Builds an <a href="https://wiki.openstreetmap.org/wiki/Overpass_API">Overpass</a> query to “talk to” the server to gain access to the Open Street Map data.</p></li>
<li><p><code><a href="https://docs.ropensci.org/osmdata/reference/add_osm_feature.html">add_osm_feature()</a></code> - Adds selected OSM features to an existing Overpass query.</p></li>
<li><p><code><a href="https://docs.ropensci.org/osmdata/reference/osmdata_sf.html">osmdata_sf()</a></code> - Converts the collected data from the API into Open Street Map Data that is formatted into sf format.</p></li>
</ul>
<div class="cell">
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Specific OSM features desired-------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"buildings"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"landuse"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"leisure"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_keys</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"building"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"landuse"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"leisure"</span>,</span>
<span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"natural"</span></span>
<span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Personally omitting water/rivers (won't map for me)#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#My bounding box data has inconsistencies#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Not sure if this is just because of where I'm mapping though#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#It is in a rural area, so not sure if that's why#</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#OSM feature grabbing and naming-----------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Native pipe (|&gt;) does not evaluate this properly like magrittr's#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_keys</span>, <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/reference/opq.html">opq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>bbox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mbb</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/reference/add_osm_feature.html">add_osm_feature</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>key <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/reference/osmdata_sf.html">osmdata_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>                      <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_polygons</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rlang.r-lib.org/reference/set_names.html">set_names</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span></code></pre></div>
</div>
<p><br></p>
<p>For my code below, I made a separate block of calls to collect this data for the highways/roads as I’d need that to be presented as lines and not polygons. I could have just put this into the above <code><a href="https://purrr.tidyverse.org/reference/map.html">map()</a></code> function, but I found this to be more readable (to me anyway). After we pull this data we <code><a href="https://purrr.tidyverse.org/reference/prepend.html">prepend()</a></code> it to our existing <code>osm_features</code> object:</p>
<div class="cell">
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Road features-----------------------------------------------------------------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">roads</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/reference/opq.html">opq</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>bbox <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">mbb</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/reference/add_osm_feature.html">add_osm_feature</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>key <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;">'highway'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://docs.ropensci.org/osmdata/reference/osmdata_sf.html">osmdata_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>  <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_lines</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Combining all OSM features#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_all</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/prepend.html">prepend</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/list.html">list</a></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;">"roads"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">roads</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Updating feature names to include "roads"#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_all</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p><br></p>
<p>If we were to take the data stored in the <code>osm_features_all</code> objects, we’d see that we’ve pulled down the map data from the API that exists within the values of our bounding box in the <code>mbb</code> object:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/full_geo.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="A simple map of Savannah, Georgia's (US) roads, landmarks, and other land use areas within a specified bounding box."></p>
</figure>
</div>
<center>
<sub><em>The data of the <code>osm_features_all</code> object plotted in ggplot with <code><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf()</a></code> layers.</em></sub><br>
</center>
<hr></section><section id="mess-with-the-crs-coordinate-reference-system" class="level1"><h1>Mess with The CRS (Coordinate Reference System)</h1>
<p>Now that the data is pulled from the API, we need to “filter” the data that only lies in a circle in the middle of our bounding box. We can make this happen with some additional functions from the <code>sf</code> package that we can use for this type of geo data-wrangling with our Coordinate Reference System (CRS):</p>
<ul>
<li><p><code><a href="https://r-spatial.github.io/sf/reference/st_crs.html">st_crs()</a></code> - Sets/Retrieves a CRS from an object.</p></li>
<li><p><code><a href="https://r-spatial.github.io/sf/reference/st_as_sf.html">st_as_sf()</a></code> - Converts and object to an <code>sf</code> object.</p></li>
<li><p><code><a href="https://r-spatial.github.io/sf/reference/geos_unary.html">st_buffer()</a></code> - Used to add or subtract values from <code>sf</code> geometries.</p></li>
<li>
<p><code><a href="https://r-spatial.github.io/sf/reference/st_transform.html">st_transform()</a></code> - Transforms/converts coordinates of a <code>sf</code> object.</p>
<p><br></p>
</li>
</ul>
<p>Other general purpose functions that we’re using here are:</p>
<ul>
<li><p><code><a href="https://rdrr.io/r/base/unname.html">unname()</a></code> - Removes the <code>names</code> attributes of an object.</p></li>
<li><p><code><a href="https://tibble.tidyverse.org/reference/enframe.html">enframe()</a></code> - Converts named vectors to columns in a data frame.</p></li>
<li><p><code><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider()</a></code> - Transforms data from long to wide via <a href="https://r4ds.had.co.nz/tidy-data.html#pivoting">pivoting</a>.</p></li>
</ul>
<p><br></p>
<p>Below, we are setting up our CRS, taking our POI coordinates and converting it into a <code>sf</code> object. Next we set a numeric <code>dist</code> value that controls how big our “circle” is. Finally, we manipulate all of this into an updated <code>circle</code> data frame that we will use to filter out the final image from the data we pulled from the API earlier.</p>
<p>Additionally, because I know what I’m making ahead of time, I’m adding another data frame, <code>center_POI</code> that will be used to mark our POI at the center of our map with a unicode heart symbol. We’ll talk about the specifics of that soon in the Set Your Colors and Text section of this post.</p>
<div class="cell">
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Circle/Center Calculations----------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Pulling the CRS---------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#(Coordinate Reference System) from the "roads" polygon object#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">crs_data</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/unname.html">unname</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/st_crs.html">st_crs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_all</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">roads</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">wkt</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Defining the circular "view" from POI-----------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Setting a distance from the center POI----------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Affects how "big" the circle is#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dist</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span>  <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Circle "data" used to crop/filter the original OSM data "down to size"--------</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">circle</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/enframe.html">enframe</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tidyr.tidyverse.org/reference/pivot_wider.html">pivot_wider</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/st_as_sf.html">st_as_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>coords <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"long"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, crs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">crs_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/geos_unary.html">st_buffer</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>dist <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">dist</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/st_transform.html">st_transform</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>crs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">crs_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#...and adding a label for the map to mark it#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://tibble.tidyverse.org/reference/tibble.html">tibble</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>long <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>                      lat <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, </span>
<span>                      label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/expression.html">expression</a></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;">"\U2764"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/st_as_sf.html">st_as_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>coords <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"long"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, crs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">crs_data</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/circle_boundary.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="A ggplot of a circular boundary on a basic map graph."></p>
</figure>
</div>
<center>
<sub><em>The <code>circle</code> data frame plotted to show the work so far. This will act as our boundary in the next step.</em></sub><br>
</center>
<hr></section><section id="carve-out-your-circle" class="level1"><h1>Carve Out Your Circle</h1>
<p>Now that we have all of the map data and the proper boundaries for the “circle” of our map, we can use the <code><a href="https://r-spatial.github.io/sf/reference/geos_binary_ops.html">st_intersection()</a></code> function to “filter” out original data to only include objects that lie in our “circle” boundaries. I’ve made a personal choice to use the <code><a href="https://purrr.tidyverse.org/reference/list_c.html">list_rbind()</a></code> function on this cropped data simply because all of my map layers will be the same gold color on a dark blue background (My best friend’s wedding colors) If you want different layers to have different colors (i.e.&nbsp;water = blue, roads = black, etc. I’d suggest either adding these aesthetics as data in this step with the use of the <code><a href="https://purrr.tidyverse.org/reference/map2.html">map2()</a></code> or <code><a href="https://purrr.tidyverse.org/reference/pmap.html">pmap()</a></code> functions, or omit the use of the <code><a href="https://purrr.tidyverse.org/reference/list_c.html">list_rbind()</a></code> function and set the individual layers in your ggplot instead. You can see these suggestions fleshed out in the Alternative Aesthetics section of this post.</p>
<div class="cell">
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Cropping OSM data to fit circle-----------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Calculating intersections-----------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Native pipe (|&gt;) does not evaluate this properly like magrittr's#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_cropped</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span>, </span>
<span>                            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/geos_binary_ops.html">st_intersection</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">circle</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_all</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/list_c.html">list_rbind</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/cropped_geo.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="A basic cropped circular map of The Mackey House and it's surrounding area in Savannah, Georgia (US). Mapped in ggplot2."></p>
</figure>
</div>
<center>
<sub><em>The <code>osm_features_cropped</code> data frame plotted to show the data that was filtered from the original <code>osm_features_all</code> object to fill in the boundaries present in the <code>circle</code> data frame.</em></sub><br>
</center>
<hr></section><section id="set-your-colors-and-text" class="level1"><h1>Set Your Colors and Text</h1>
<p>The hard part of grabbing the data is done. All that’s left to do is set some aesthetics. As mentioned, I kept this piece simple to mimic my friend’s wedding colors. I tend to store these colors/hex codes in separate objects. This is to make for faster editing if I want to create different pieces with different colors or options. I follow this same convention for the text options as well. In this project, I’m using the free Google fonts&nbsp;<a href="https://fonts.google.com/specimen/La+Belle+Aurore">La Belle Aurore</a>&nbsp;(For the title and annotated text) and&nbsp;<a href="https://fonts.google.com/specimen/Inter">Inter</a>&nbsp;(for the heart in the center, where the Mackey House, our POI, is.) I’m using the second font because it is Unicode-friendly. I want to place a Unicode heart ♥ on the Mackey House. This is just a hacky way to do it. I’m sure there are plenty more options, like exploring the <a href="https://emilhvitfeldt.github.io/emoji/"><code>emoji</code> package</a> that’s available on CRAN.</p>
<p>To get the font <em>exactly</em> how I want, I’m choosing to utilize the <a href="https://glue.tidyverse.org/"><code>glue</code></a>, <a href="https://wilkelab.org/ggtext/"><code>ggtext</code></a>, and <a href="https://github.com/yixuan/showtext"><code>showtext</code></a> packages as they will allow me to use the Google fonts with some HTML/CSS syntax to get my font/words/style just right.</p>
<p>You can replace the title and caption texts as you see fit or remove them for whatever you need.</p>
<div class="cell">
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map aesthetic options---------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Setting all colors#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e9d66b"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Gold/Yellow color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_fill</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e9d66b"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Gold/Yellow color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_background</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#022052"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Dark blue/Navy Blue</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outer_ring_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e9d66b"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Gold/Yellow color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inner_ring_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#022052"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Dark blue/Navy Blue</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#960503'</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Red color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#000000'</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Black</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Text Options------------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Adding a pretty font from Google#</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/sysfonts/man/font_add_google.html">font_add_google</a></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;">"La Belle Aurore"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/sysfonts/man/font_add_google.html">font_add_google</a></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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/pkg/showtext/man/showtext_auto.html">showtext_auto</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_text</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The Mackey House"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_text</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://glue.tidyverse.org/reference/glue.html">glue</a></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;">"Jessica and Chris&lt;br&gt;"</span>,</span>
<span>                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"06 • 10 • 22&lt;br&gt;"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Savannah, Ga&lt;br&gt;"</span>,</span>
<span>                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lat:&lt;span style='color:"</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'&gt;"</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;/span&gt; , Long:&lt;span style='color:"</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"'&gt;"</span>,</span>
<span>                     <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</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;">"&lt;/span&gt;&lt;/b&gt;"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"La Belle Aurore"</span></span></code></pre></div>
</div>
<p><br></p>
<hr></section><section id="build-the-ggplot" class="level1"><h1>Build the ggplot</h1>
<p>Finally, we can throw this bad boy together into a single ggplot. Just like we would with any other standard ggplot visual. The key here is to use the <code><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf()</a></code> layer for all our geometry data. All of your aesthetics are mapped on the same way you would for any other type of data visualization. The only thing special or unique is the geomanipulation needed for the <code><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf()</a></code> function to read and visualize the data properly within the ggplot.</p>
<p>You may be wondering about the border that is around the map. I’ve done some hacky stuff with <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code> to achieve this. My original plan was to manually make the circle/border visual with data points; however, when I attempted this months ago, I couldn’t figure out how to correctly manipulate the coordinate system to project the circle properly, so I opted to use standard <code><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point()</a></code> shapes to make it happen. Both layers were figured out with trial and error based on my specific print size. Be mindful that you may need some trial and error tweaking for your particular needs.</p>
<div class="cell">
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map Layering/Building---------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Adding OSM layers to the ggplot-----------------------------------------------</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_void</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/theme.html">theme</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>plot.background <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_rect</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>    fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_background</span>,</span>
<span>    color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/reference/element_textbox.html">element_textbox_simple</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>          halign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, </span>
<span>          color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_color</span>, </span>
<span>          family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span>, </span>
<span>          size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">240</span>, </span>
<span>          face <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;">'bold'</span>, </span>
<span>          fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span>          margin <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">margin</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'cm'</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_cropped</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geometry</span>,</span>
<span>          color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_color</span>,</span>
<span>          fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_color</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>,</span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_color</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>,</span>
<span>            family <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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>,</span>
<span>            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span>            family <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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>             color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outer_ring_color</span>, </span>
<span>             shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, </span>
<span>             size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">399</span>, </span>
<span>             stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>             color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inner_ring_color</span>, </span>
<span>             shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, </span>
<span>             size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">403</span>, </span>
<span>             stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/reference/geom_richtext.html">geom_richtext</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>,</span>
<span>                    y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.011</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_text</span>, </span>
<span>                text.color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_color</span>,</span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span>, </span>
<span>                lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span>                fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span>                hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>,</span>
<span>                label.color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">coord_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ylim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.008</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.012</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#ggsave("finished map.png",</span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#width = 16, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#height = 20, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#dpi = 300, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#units = "in")</span></span></code></pre></div>
</div>
<p>This results in this beauty after we use <code><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave()</a></code> with our set output size:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/finished map.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="The completed circular map of the Mackey House in Savannah, Georgia, (US) The background is a deep navy blue, all other map elements are a yellow gold color. The title reads The Mackey House. The bottom caption reads Jessica and Chris, June 10th 2022, Savannah Georgia with Latitude and Longitude degrees listed in red text. The Mackey House is marked in the center with a red heart."></p>
</figure>
</div>
<center>
<sub><em>The final result!</em></sub><br>
</center>
<p><br></p>
<section id="alternative-aesthetics" class="level2"><h2 class="anchored" data-anchor-id="alternative-aesthetics">Alternative Aesthetics</h2>
<p>So what if you don’t want to use a simple color palette? There are many ways to add variations to the aesthetics in the ggplot to introduce more complicated color schemes to your maps. For our example, though, let’s look at just two ways to do this:</p>
<section id="embedding-color-data-into-the-data-frame" class="level3"><h3 class="anchored" data-anchor-id="embedding-color-data-into-the-data-frame">Embedding Color Data Into the Data Frame</h3>
<p>To do it this way, we can add a vector of colors into the cropped data frame (the step where we use <code><a href="https://r-spatial.github.io/sf/reference/geos_binary_ops.html">st_intersection()</a></code> to get our final data set. We simply use <code><a href="https://purrr.tidyverse.org/reference/map2.html">map2()</a></code> and add a <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> line inside to add the <code>color</code> column that will store the hex color codes for each feature:</p>
<div class="cell">
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Cropping OSM data to fit circle-----------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Storing the new colors#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_colors</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"#43963C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#FCE8E6"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9e220d"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#9e6e0d"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffffff"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Calculating intersections-----------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Native pipe (|&gt;) does not evaluate this properly like magrittr's##</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_df</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map2.html">map2</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_colors</span>,  </span>
<span>                             <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/geos_binary_ops.html">st_intersection</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">circle</span>, </span>
<span>                                              <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_all</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>                                                <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.y</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/list_c.html">list_rbind</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Map aesthetic options---------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_background</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d9d1a0"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Tan Color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">text_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outer_ring_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Black</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inner_ring_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d9d1a0"</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Tan Color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#190d9e'</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Blue color</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_color</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'#000000'</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Black</span></span></code></pre></div>
</div>
<p><br></p>
<p>Now we can just tweak the ggplot code a bit by adding the <code><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html">scale_color_manual()</a></code> function and updating our <code><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf()</a></code> function to include the new data frame <code>osm_colors_df</code> with added aesthetics for <code>geometry</code>, <code>fill</code>, and <code>color</code>.</p>
<div class="cell">
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_void</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/theme.html">theme</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.background <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_rect</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_background</span>, </span>
<span>                                               color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/reference/element_textbox.html">element_textbox_simple</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>          halign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, </span>
<span>          color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">text_color</span>, </span>
<span>          family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span>, </span>
<span>          size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">240</span>, </span>
<span>          face <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;">'bold'</span>, </span>
<span>          fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span>          height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,</span>
<span>          width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        legend.position <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;">"none"</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/scale_manual.html">scale_color_manual</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>values <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/rev.html">rev</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_colors</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                     aesthetics <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></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;">"fill"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"color"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_df</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>geometry <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">geometry</span>,</span>
<span>                                    fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color</span>,</span>
<span>                                    color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">color</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_color</span>, </span>
<span>                        size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>,</span>
<span>                        family <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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span>            family <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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outer_ring_color</span>, </span>
<span>            shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">399</span>, </span>
<span>            stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inner_ring_color</span>, </span>
<span>            shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">403</span>, </span>
<span>            stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/reference/geom_richtext.html">geom_richtext</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.011</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_text</span>, </span>
<span>                text.color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">text_color</span>,</span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span>, </span>
<span>                lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span>                fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span>                hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>,</span>
<span>                label.color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">coord_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ylim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.008</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.012</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#ggsave("map_v2.png",</span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#width = 16, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#height = 20, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#dpi = 300, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#units = "in")</span></span></code></pre></div>
</div>
<p>Which changes our colors accordingly and gives us this after we use <code><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave()</a></code> with our set output size:</p>
<p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/map_v2.png" class="img-fluid" alt="The completed circular map of the Mackey House in Savannah, Georgia, (US) The background is a light tan color and all other map elements are various green, orange, and white colors. The title reads The Mackey House. The bottom caption reads Jessica and Chris, June 10th 2022, Savannah Georgia with Latitude and Longitude degrees listed in red text. The Mackey House is marked in the center with a blue heart."></p>
<center>
<sub><em>The data of the <code>osm_colors_df</code> object plotted in ggplot with a <code><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf()</a></code> layer.</em></sub><br>
</center>
<p><br></p>
</section><section id="leaving-data-in-a-list-and-changing-colors-in-geom_sf" class="level3"><h3 class="anchored" data-anchor-id="leaving-data-in-a-list-and-changing-colors-in-geom_sf">Leaving Data in a List and Changing Colors in <code>geom_sf()</code>
</h3>
<p>If you’re used to having multiple layers in your ggplot and would prefer to manually set each feature’s color individually without using <code>purrr</code>, you can leave your data in a list like so…</p>
<div class="cell">
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Calculating intersections-----------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Native pipe (|&gt;) does not evaluate this properly like magrittr's#</span></span>
<span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_list</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://purrr.tidyverse.org/reference/map.html">map</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span>, </span>
<span>                            <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://r-spatial.github.io/sf/reference/geos_binary_ops.html">st_intersection</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">circle</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_features_all</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[[</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.x</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"><a href="https://magrittr.tidyverse.org/reference/pipe.html">%&gt;%</a></span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rlang.r-lib.org/reference/set_names.html">set_names</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">.</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">feature_names</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> </span></code></pre></div>
</div>
<p><br></p>
<p>And create separate layers for each feature in the list in your ggplot:</p>
<div class="cell">
<div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggplot.html">ggplot</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggtheme.html">theme_void</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/theme.html">theme</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>        plot.background <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/element.html">element_rect</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>fill <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;">"#82a775"</span>, </span>
<span>                                               color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>        plot.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/reference/element_textbox.html">element_textbox_simple</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span></span>
<span>          halign <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>, </span>
<span>          color <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;">"#ffffff"</span>, </span>
<span>          family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span>, </span>
<span>          size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">240</span>, </span>
<span>          face <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;">'bold'</span>, </span>
<span>          fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span>          height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,</span>
<span>          width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span>        <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">roads</span>,</span>
<span>          color <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;">"#B05f66"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">buildings</span>,</span>
<span>          color <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;">"#64513b"</span>,</span>
<span>          fill <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;">"#A68662"</span>,</span>
<span>          linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">landuse</span>,</span>
<span>          color <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;">"#734B09"</span>,</span>
<span>          fill <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;">"#D78C10"</span>,</span>
<span>          linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">leisure</span>,</span>
<span>          color <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;">"#d1b39d"</span>,</span>
<span>          fill <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;">"#4F443B"</span>,</span>
<span>          linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">osm_colors_list</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">natural</span>,</span>
<span>          color <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;">"#3b727c"</span>,</span>
<span>          fill <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;">"#5CB2C2"</span>,</span>
<span>          linewidth <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span>          linetype <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>                        color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_color</span>, </span>
<span>                        size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>,</span>
<span>                        family <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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_text.html">geom_text</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">label</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI_fill</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>,</span>
<span>            family <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;">"Inter"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">outer_ring_color</span>, </span>
<span>            shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">399</span>, </span>
<span>            stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/geom_point.html">geom_point</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">center_POI</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>, </span>
<span>            color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">inner_ring_color</span>, </span>
<span>            shape <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>, </span>
<span>            size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">403</span>, </span>
<span>            stroke <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/labs.html">labs</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">title_text</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://wilkelab.org/ggtext/reference/geom_richtext.html">geom_richtext</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/aes.html">aes</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"long"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span>, y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.011</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span>,</span>
<span>                label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">caption_text</span>, </span>
<span>                text.color <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;">"#ffffff"</span>,</span>
<span>                family <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">map_font</span>, </span>
<span>                lineheight <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.1</span>,</span>
<span>                size <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span>                fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>,</span>
<span>                hjust <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.5</span>,</span>
<span>                label.color <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span>  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">coord_sf</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span>ylim <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">(</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.008</span>,<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">POI</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;">"lat"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">]</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">.012</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span>
<span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#ggsave("map_v2.png",</span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#width = 16, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#height = 20, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#dpi = 300, </span></span>
<span>       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#units = "in")</span></span></code></pre></div>
</div>
<p><br></p>
<p>Which results in this after we use <code>ggsave():</code></p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://www.thetidytrekker.com/post/making-circular-maps/images/map_v3.png" class="img-fluid quarto-figure quarto-figure-center figure-img" alt="The completed circular map of the Mackey House in Savannah, Georgia, (US) The background is a light green color and all other map elements are various blue, brown, and pink colors. The title reads The Mackey House. The bottom caption reads Jessica and Chris, June 10th 2022, Savannah Georgia with Latitude and Longitude degrees listed in red text. The Mackey House is marked in the center with a blue heart."></p>
</figure>
</div>
<center>
<sub><em>The data of the <code>osm_colors_list</code> object plotted in ggplot with multiple <code><a href="https://ggplot2.tidyverse.org/reference/ggsf.html">geom_sf()</a></code> layers.</em></sub><br>
</center>
<hr></section></section></section><section id="save-it-out-and-get-it-printed-if-you-want" class="level1"><h1>Save it Out and Get it Printed (If you Want)</h1>
<p>Because I wanted this to be a wedding gift for my friend, I had the idea to save the output with the <code><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave()</a></code> function but then send it to a printer to get it on a canvas. I ended up using <a href="https://www.canva.com">Canva</a>’s printing services for this. While I wouldn’t use Canva for any bulk printing because of the price (say for a shop for merchandise), I will use it for one-off special things like this. My friend received it on time and the quality was great!</p>
<div class="cell">
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="downlit sourceCode r code-with-copy"><code class="sourceCode R"><span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Final Image Export------------------------------------------------------------</span></span>
<span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#=============================================================================#</span></span>
<span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;"><a href="https://ggplot2.tidyverse.org/reference/ggsave.html">ggsave</a></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;">"finished map.png"</span>,</span>
<span>       width <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>, </span>
<span>       height <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, </span>
<span>       dpi <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>, </span>
<span>       units <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;">"in"</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">)</span></span></code></pre></div>
</div>
<p><br></p>
<p>That’s it! I want to note that I came across an existing <a href="https://taraskaduk.com/posts/2021-01-18-print-street-maps/">blog</a> by <a href="https://taraskaduk.com/about">Taras Kaduk</a> and a <a href="https://github.com/ivabrunec/30daymapchallenge/blob/main/scripts/2022/day8_OSM.R">GitHub repository</a> by <a href="https://ivabrunec.github.io/">Iva Brunec</a>. I used both of these together with a lot of tweaking to get the final result here. If something wasn’t clear here, or you want to see their takes on this type of visual, go check out their work!</p>
<p><em>If this helped you out or you made anything as a result of reading, I’d love for you to share what you’ve made! Find me on Twitter (<a href="https://twitter.com/meghansharris">@meghansharris</a>), Mastodon (<a href="https://fosstodon.org/@meghansharris">@meghansharris@fosstodon.org</a>), or LinkedIn (<a href="https://www.linkedin.com/in/meghan-harris/">meghan-harris</a>) to do so!</em></p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section> ]]></description>
  <category>printing</category>
  <category>data visualization</category>
  <category>life</category>
  <category>r</category>
  <category>maps</category>
  <category>ggplot</category>
  <category>personal</category>
  <guid>https://www.thetidytrekker.com/post/making-circular-maps/making-circular-maps.html</guid>
  <pubDate>Wed, 15 Feb 2023 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/making-circular-maps/images/makingmaps.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Let’s Talk About 2022</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/lets-talk-about-2022/lets-talk-about-2022.html</link>
  <description><![CDATA[ 






<p>Yes, hello. It’s been a while. I have been neglecting The Tidy Trekker as of late (and to be honest, I still might this year after this overhaul is done, but we’ll see.) I had all the intentions of being super active here in 2022, but life happened. The good news is, instead of the usual negative BS, I was inundated with EXCELLENT things in 2022. I was afraid to even step into 2023 for fear that all my good luck would “run out” this year. (<em>I’m joking, btw life is what you make it, and your enjoyment of it depends on a lot of things like perspective, if you need/go to therapy, circumstances, etc.)</em></p>
<p><br></p>
<p>But enough general rambling; let’s start with some targeted rambling. <strong>So what happened in 2022?</strong></p>
<p><br></p>
<p><strong>In this post:</strong></p>
<ul>
<li><p>Conferences and Talks</p></li>
<li><p>Job Change</p></li>
<li><p>Family and Home Changes</p></li>
<li><p>So, What About 2023?</p></li>
</ul>
<hr>
<section id="conferences-and-talks" class="level1">
<h1>Conferences and Talks</h1>
<p>So this one is probably the most known. I had A LOT of talks to prepare for and do. Some of you may have found out about me through&nbsp;<a href="https://thetidytrekker.com/talks.html">one of the various talks</a>&nbsp;I did in 2022. Most of the talks were about #rtistry (generative art in the R language), but I also gave more practical ones. The biggest one was obviously RStudio::Conf(2022). This was BY FAR the best conference experience I ever had despite being pregnant and in the middle of so many life-changing things all at once. I was literally onboarding at my new job and had jumped on a plane to D.C. less than a few days after unexpectedly closing on my first home. It was an absolute cluster of chaotic good.</p>
<p>At the conference, I assisted with the&nbsp;<a href="https://art-from-code.netlify.app/">Making Art From Code workshop</a>, was a diversity scholar, and gave my own talk, “<a href="https://thetidytrekker.com/talks/pipelines_in_r/pipeline_in_r.html">Making Data Pipelines in R.</a>”</p>
<center>
<em><img src="https://www.thetidytrekker.com/post/lets-talk-about-2022/images/conf_2.png" class="img-fluid" alt="Three pictures I took at RStudio conf 2022. An Orange entry sign of a Workshop title Making Art from Code, a 2022 conference schedule, and the title screen of my conference presentation, Making Data Pipelines in R."></em> <sub>Had to take pictures so I could know it was ✨real.✨</sub> <br>
</center>
<p><br></p>
<p>Side note. If you LOVE R and are on the fence about ever going to a (Now Posit) conference, you should go if you can! In the past, if you submit a presentation or even a lightning talk, Posit has provided traveling and boarding assistance. If you are from an underrepresented group in tech, you can also apply for a diversity scholarship. It’s a great way to gain any needed assistance as well. Just like how Twitter used to be great&nbsp;<em>(before the Twitter Exodus)</em>, imagine having that in real life for a few days. It was awesome. And I mean, come on, who doesn’t like stickers???</p>
<center>
<p><img src="https://www.thetidytrekker.com/post/lets-talk-about-2022/images/conf_1.png" class="img-fluid" alt="Three pictures I've taken at the 2022 RStudio conference. A pile of hex stickers on a table, a bathroom mirror selfie of me showing my baby bump before I gave my presentation, and a pile of souvenirs on a bed including generative art pictures, a R Studio cheat sheet, my badge, and some hex stickers."></p>
<p><sub><em>So…Many…STICKERS!!!!</em></sub></p>
</center>
</section>
<section id="job-change" class="level1">
<h1>Job Change</h1>
<p>One of the cool things about attending the conference was that I could meet some of my co-workers in real life! I recently changed jobs in 2022. I now work at the Prostate Cancer Clinical Trials Consortium (PCCTC) at Memorial Sloan Kettering Cancer Center. This has been a significant change so far, as it has allowed me to stop working so much. I went from having three jobs to one full-time one (and a Per Diem contract one if needed from time to time.)</p>
<p>At the time of writing this, I’m on parental leave now, but I’m excited to get to work when I get back. Some of my work will focus on streamlining internal stuff for reporting on prostate cancer studies, and I’m SUPER hyped about it. I’ve been using R for about four, going on five years now, and I think I’m finally starting to hone in on my “niche.” Besides making art with R, I get a lot of satisfaction from automating things. Building (hopefully) seamless pipelines that make work easier for people. I realized I’d rather spend months building pipelines than doing analyses, external client-facing, and the like. In other words, my current role as a data scientist in this capacity perfectly aligns with my pragmatic interests. Honestly, I’m super lucky there.</p>
</section>
<section id="family-and-home-changes" class="level1">
<h1>Family and Home Changes</h1>
<p>Working at the PCCTC also went perfectly with home life. For those of you that missed it, I was very open about my&nbsp;<a href="https://meghansharris.shinyapps.io/A_Fertility_Journey/?_ga=2.49472864.796738959.1675303563-1161739068.1675303563">infertility struggles</a>&nbsp;after I got pregnant. But as life would have it, after trying aggressively for three years, I got pregnant right before I found out about the open position at the PCCTC. I almost didn’t apply for it because it felt wrong to do so, knowing that I would have to leave for parental leave.</p>
<p>Looking back, switching jobs was the best thing I could have done. I could support myself financially, stop trying to run a one-person data science team (like I had to do at my old job), and really just focus on working while learning new things everyday. An added bonus is being able to spend time getting to know my son and just being grateful that all the years of feeling like I was working myself to death eventually paid off (yes, even during late nights while I was on the verge of tears trying to console a crying baby 😅)</p>
<p>I have a lot to be grateful for. I feel like I have changed so much as a person and that I am really coming into the best version of myself that I can be. Whether we’re talking professionally or personally.</p>
</section>
<section id="so-what-about-2023" class="level1">
<h1>So, What About 2023?</h1>
<p>So… what’s next? Honestly, this year I’d like to “lay low.” I want to actively stay home, study up on things, be a mother, and work. That’s all I want. So, professionally speaking, this is what I’ve got on my to-do list for 2023:</p>
<ul>
<li><p>Get projects done/foundations started at work <sub><em>(This has to be done as I have to work lol)</em></sub></p></li>
<li><p>Open up my online art store <sub><em>(This doesn’t have to be done, but I probably should since I already made the store and everything.)</em></sub></p></li>
<li><p>Re-launch the new version of my website <sub><em>(If you’re reading this, that means I did this! Go Me!!!)</em></sub></p></li>
<li><p>Contribute to the textbook <a href="https://twitter.com/meghansharris/status/1620256954433273859?s=20&amp;t=D2O6oV0SAKmRK4nmZwK_hg"><em>“Rtistry: Methods for Visual Generative Art Using R”</em></a> along with <a href="https://fosstodon.org/@paezha@mastodon.online">Antonio Paez</a>, <a href="https://twitter.com/aschinchon">Antonio Sanchez</a>, and <a href="https://fosstodon.org/@jacquietran@mastodon.social">Jacquie Tran</a>. <sub><em>(This has to be done as a contract was signed)</em></sub></p></li>
</ul>
<p>That’s it. If I have energy for a blog post here or there, or if my job wants to send me to Chicago for Posit Conf this year (Travis, pls.) Then I’ll do that. But the goal is to work <em>“passively”</em> this year (if that makes any sense at all.)</p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section>

 ]]></description>
  <category>personal</category>
  <category>intentions</category>
  <category>life</category>
  <category>r</category>
  <category>rtistry</category>
  <category>talks</category>
  <category>conferences</category>
  <guid>https://www.thetidytrekker.com/post/lets-talk-about-2022/lets-talk-about-2022.html</guid>
  <pubDate>Mon, 23 Jan 2023 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/lets-talk-about-2022/images/lta2022.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Making Waves in ggplot: An Rtistry Tutorial</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/making-waves/making-waves.html</link>
  <description><![CDATA[ 






<center>
<img src="https://www.thetidytrekker.com/post/making-waves/images/wave.PNG" width="100%">
<figcaption>
The Original “Wave Patch” Piece Made in R
</figcaption></center>
<p><br></p>
<p>A little while ago, I made an Rtistry piece that I named <em>“Wave Patch.”</em> (Shown above). There seemed to be some interest in providing a tutorial on making waves like I did in <em>Wave Patch</em>. I’ll use this post to discuss the basic concepts behind making one set of waves that will hopefully set the reader up to take that knowledge, expand upon it, and make their own Rtistry!</p>
<p>While things will always be explained when appropriate, this tutorial assumes that you have experience working in ggplot in the tidyverse, working with ggplot aesthetics, and creating functions.</p>
<p><strong><em>Don’t want to read and just want the code? The full code for this tutorial is on my GitHub Repo <a href="https://github.com/Meghansaha/Data_Art/blob/master/creations/Functions/Waves/Making_Waves.R">here</a>.</em></strong></p>
<hr>
<section id="lets-start-with-a-trig-refresher" class="level2">
<h2 class="anchored" data-anchor-id="lets-start-with-a-trig-refresher">Let’s Start with a Trig Refresher!</h2>
<p>So, confession time. I have never taken a trigonometry class in my life! The closest I got was taking geometry in high school and I got a C. BUT, I was still able to make this piece, so no matter your math background, you can too! These waves are made with <a href="https://en.wikipedia.org/wiki/Sine">sine waves</a>. You might be familiar with them already. We can code for basic sine waves in ggplot by setting up our data appropriately:</p>
<div class="cell" data-layout-align="center">
<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;"># Library Load-In====</span></span>
<span id="cb1-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 everything data#</span></span></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.0     ✔ stringr   1.5.2
✔ 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="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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Let's Start with a Trig Refresher#========================#</span></span>
<span id="cb3-2">theta <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;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-3">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</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>pi, </span>
<span id="cb3-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb3-5"></span>
<span id="cb3-6">sine <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> theta,</span>
<span id="cb3-7">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(theta),</span>
<span id="cb3-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(theta))</span></code></pre></div>
</div>
<p><br></p>
<p>… and plotting it like this:</p>
<div class="cell" data-layout-align="center">
<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;"># A basic sine curve======</span></span>
<span id="cb4-2">sine <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-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>x,<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></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_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;">"red"</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></code></pre></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-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/codesnip2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>All we are doing with this code is setting up a sequence of points in our theta variable. Using the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq"><code>seq()</code></a> function. We’re telling R “Create 100 numbers between and including the values <code>0</code> to <code>2pi</code> (6.283185). When we go to plot this data, we put theta on one axis (in this case, the <code>x</code> variable because I want my wave to be drawn horizontally on the plane), and then place the sine values of theta on the other axis. Note that these values don’t have to be <code>0</code> and <code>2pi</code>. In the next section, we will switch this up. But let’s just take one more look at this wave with some labels attached:</p>
<div class="cell" data-layout-align="center">
<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;"># A basic sine curve with more detail pointed out======</span></span>
<span id="cb6-2"></span>
<span id="cb6-3">sine <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-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>x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> label))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-5">  <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;">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="cb6-6">  <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;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi, <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="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_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;">"red"</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="cb6-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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-9">  ggrepel<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_text_repel</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max.overlaps =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</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="cb6-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</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> <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;">y =</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>
<span id="cb6-11">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u2190'</span>),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"theta's '0'"</span>),</span>
<span id="cb6-12">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,</span>
<span id="cb6-13">           <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="cb6-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_text</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> <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>pi,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb6-15">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</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;">"theta's '2*pi'"</span>,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'\u2192'</span>)),</span>
<span id="cb6-16">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nudge_x =</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="cb6-17">           <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></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in geom_text(aes(x = 0, y = -1), label = paste(sprintf("←"), "theta's '0'"), : All aesthetics have length 1, but the data has 100 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
  a single row.</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in geom_text(aes(x = 2 * pi, y = 1), label = paste("theta's '2*pi'", : All aesthetics have length 1, but the data has 100 rows.
ℹ Please consider using `annotate()` or provide this layer with data containing
  a single row.</code></pre>
</div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/codesnip3-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>If needed, take some time to review the more detailed code/plot of the labeled sine wave. Understanding how data is selected to create the visuals on the plot will set you up for understanding how you can manipulate these visuals in the future.</p>
<hr>
</section>
<section id="make-the-transition-from-basic-waves-to-pretty-waves" class="level1">
<h1>Make the Transition from Basic Waves to Pretty Waves</h1>
<p>OK. So we know how to make a basic sine wave. How do we turn that into a “pretty” wave? There’s multiple ways to achieve this, but we’ll just go through the way that the <em>wave patch</em> image is built from. In order to have more creative control over our wave’s aesthetics, we’ll turn our sine wave into a polygon object. First, we can start by setting our theta. Let’s change it up. This time instead of calling the <code>length.out</code> argument, we’ll call the <code>by</code> argument. The <code>seq()</code> function will automatically calculate all numbers between <code>-.4</code> and <code>-1.5*pi</code> (-4.712389) by a value of -.1:</p>
<div class="cell" data-layout-align="center">
<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;"># Starting the transition to a pretty wave====</span></span>
<span id="cb9-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Setting up our "range" on the x axis for horizontal waves=====</span></span>
<span id="cb9-3">wave_theta <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;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</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;">4</span>,</span>
<span id="cb9-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</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;">1.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi, </span>
<span id="cb9-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</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>
<p><br></p>
<p>We can now take our new theta and make a <a href="https://tibble.tidyverse.org/">tibble</a>, (data frame) for our waves. Say we don’t want these basic sine waves though. We can play around with our x variable when creating the data frame to get some interesting results. We need to also remember to <a href="https://dplyr.tidyverse.org/reference/arrange.html"><code>arrange()</code></a> our frame by the x variable. Or else we’ll run into plotting issues in the future. I’ll speak more on this later. Let’s call this new tibble, <code>curve_top</code>:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Creating the "top" of our wave polygon====</span></span>
<span id="cb10-2">curve_top <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> wave_theta,</span>
<span id="cb10-3">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</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;">cos</span>(wave_theta))<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;">exp</span>(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;">2</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(x)</span></code></pre></div>
</div>
<p><br></p>
<p>What we have now is a data frame that contains points for a single sine wave that’s been manipulated a bit by multiplying the sine of x with the cosine of our new theta and then added to the logarithmic value of x squared. If you’re wondering how I figured this out, the answer is by <strong>trial and error</strong>. While I have taken calculus and geometry classes, I have a <a href="https://www.mathsisfun.com/sets/functions-common.html">very basic understanding of what type of functions create what type of visuals</a>. This basic knowledge is just enough to create some great visuals!</p>
<p>So, let’s look at just this “top” of the wave we made:</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://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/wavetop-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Nicer! But still basic. We need some dimension. We want to make a polygon, so now we need to add some sides and a bottom to make a complete polygon. There are many ways to go about doing this, but let’s try to create these pieces one by one for clarity. Let’s add a side to the right of this wave. To do so, first we find the maximum point of the x variable with the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Extremes"><code>max()</code></a> function, and subset the <code>curve_top</code> data frame with just that point in it:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Grab the max X value in the wave from the top wave====</span></span>
<span id="cb11-2">max_x_point <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curve_top[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(curve_top<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">max</span>(curve_top<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x)),]</span></code></pre></div>
</div>
<p><br></p>
<p>Using this row we’ve pulled out, we can simply add a row that includes the “bottom” of our side (because the top position of the side is just the last (max x) point in our wave. Because we want our side to connect to a “bottom” piece, we’ll use the <a href="https://tibble.tidyverse.org/reference/add_row.html"><code>add_row()</code></a> function to increase the length of our side down the y axis by 0.5. Because of the way polygons are drawn, we should also set the option to have the “side” data rearranged in descending order of the y variable (since we want to move “down” on our plot):</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Create a subset of curve_top to create a "side" of the wave====</span></span>
<span id="cb12-2">curve_side_right <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> max_x_point <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;">add_row</span>(max_x_point <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;">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.5</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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(y))</span></code></pre></div>
</div>
<p><br></p>
<p>Let’s see our addition:</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://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/curveright-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Great! Next, we connect a bottom curve to the side we just created. For simplicity, we’ll use a copy of <code>curve_top</code> that has its y values decreased by 0.5 (the same length as our side we just made. Because of how <a href="https://ggplot2.tidyverse.org/reference/geom_polygon.html"><code>geom_polygon()</code></a> will draw onto the plot, we should arrange the x values in the decreasing order for this part of the wave as well:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Create a copy of the curve_top dataset with the y values decreased by .5====</span></span>
<span id="cb13-2">curve_bottom <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curve_top <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-3">  <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;">y =</span> y <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.5</span>) <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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(x))</span></code></pre></div>
</div>
<p><br></p>
<p>Let’s take a look:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Viewing the top curve, curve_side_right, and curve_bottom together====</span></span>
<span id="cb14-2">curve_top <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-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>x, <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></span>
<span id="cb14-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>()<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;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> curve_side_right, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</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>
<span id="cb14-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> curve_bottom, <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="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/curvebottom-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Aww yeah. Getting closer. Finally, we can close this up by adding the final left side. We can finish this by doing the exact same thing we did for the right side, only this time we’ll subset out the minimum value with the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Extremes"><code>min()</code></a> function. This time we want to arrange the <code>y</code> variable in ascending order because we want to move “up” the plot. Our code is essentially going to be set up to draw the polygon in a clockwise fashion starting with the top curve:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Grab the min X value in the wave from the top wave====</span></span>
<span id="cb15-2">min_x_point <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> curve_top[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which</span>(curve_top<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</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;">min</span>(curve_top<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x)),]</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Create a side that will connect curve_top and curve_bottom on the left side====</span></span>
<span id="cb15-5">curve_side_left <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> min_x_point <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_row</span>(min_x_point <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;">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.5</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(y)</span></code></pre></div>
</div>
<p><br></p>
<p>Now that we have all of our sides in our wave, we can actually just bind all of these data frames together into one data frame with the <a href="https://dplyr.tidyverse.org/reference/bind.html"><code>bind_rows()</code></a> function and then view it:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Slap all of them together into one dataframe IN ORDER (top,right,bottom,left)</span></span>
<span id="cb16-2">wave <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;">bind_rows</span>(curve_top,curve_side_right,curve_bottom,curve_side_left)</span>
<span id="cb16-3"></span>
<span id="cb16-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##View the entire thing====</span></span>
<span id="cb16-5">wave <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb16-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>x, <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></span>
<span id="cb16-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/codesnip10-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>YESSSS!! (I’m way t00 excited, I know.) If you now try to visualize creating this shape in a clockwise fashion, it might make sense why we arranged those different variables. The following rules will become extremely important once we get further into this piece:</p>
<ul>
<li><p><strong>curve_top</strong> is drawn from <strong>left to right</strong>. The x variable needs to be in <strong>ascending</strong> order to go right.</p></li>
<li><p><strong>curve_side_right</strong> is drawn from <strong>top to bottom</strong>. The y variable needs to be in <strong>descending</strong> order to go down.</p></li>
<li><p><strong>curve_bottom</strong> is drawn from <strong>right to left</strong>. The x variable needs to be in <strong>descending</strong> order to go left.</p></li>
<li><p><strong>curve_side_left</strong> is drawn from <strong>bottom to top</strong>. The y variable needs to be in <strong>ascending</strong> order to go up.</p></li>
</ul>
<p><br></p>
<p>This is also visualized with notations below:</p>
<center>
<img src="https://www.thetidytrekker.com/post/making-waves/images/detailedcurves.png" width="100%">
</center>
<blockquote class="blockquote">
<p>Keep in mind that the true behavior of <code>geom_polygon()</code> is <strong>not</strong> to go in a clockwise fashion, but it’s actually to follow the order of the data you supply to it. Because of the order in which the sides of the waves are created in this example, this is one possible version that makes sense. All visuals made with <code>geom_polygon()</code> do <strong>not</strong> need to be ordered in a clockwise fashion.</p>
</blockquote>
<p>Ok so we have one wave, but the <em>wave patch</em> image has multiple patterns of these waves. While I won’t recreate the whole thing, I can plant the seed. Our next step is to make a function to help us with this.</p>
<hr>
</section>
<section id="creating-functions-for-the-waves" class="level1">
<h1>Creating Functions for the Waves</h1>
<p>We could take our waves data frame and repeatedly change our ggplot code to create different layers of these waves, but, we can save a lot of work by creating a custom function for this purpose. If you need to brush up on functions and iterations in R programming, you can always check out <em><a href="https://r4ds.had.co.nz/index.html">“R for Data Science”</a></em> by <a href="https://github.com/hadley">Hadley Wickham</a> and <a href="https://github.com/garrettgman">Garrett Grolemund</a>. Specifically, chapters 19 and 21 on <a href="https://r4ds.had.co.nz/functions.html">Functions</a> and <a href="https://r4ds.had.co.nz/iteration.html">Iterations</a>, respectively.</p>
<p>What we want to do is take our waves data frame that we’ve already created and iteratively make new data frames with slightly different y values and combine all of that together into one data set. This will create different wave layers on a single plot and streamline our code because we only have to refer back to one data frame instead of multiple ones.</p>
<p>We’re going to create a function called <code>wave_maker</code> that can do this below:</p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating a function for iterations====</span></span>
<span id="cb17-2"></span>
<span id="cb17-3">wave_maker <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>(n, wave_df){</span>
<span id="cb17-4"></span>
<span id="cb17-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Creating an empty list to store our multiple dataframes(waves)#</span></span>
<span id="cb17-6">  wave_list<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;">list</span>()</span>
<span id="cb17-7">  </span>
<span id="cb17-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Creating a for loop to iteratively make "n" amount of waves#</span></span>
<span id="cb17-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</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>n)){</span>
<span id="cb17-10">    </span>
<span id="cb17-11">    wave_list[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> wave_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-12">      <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;">y =</span> y <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.5</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>i),</span>
<span id="cb17-13">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> i)  </span>
<span id="cb17-14">  }</span>
<span id="cb17-15">  </span>
<span id="cb17-16"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#returning the completed data frame to the environment#</span></span>
<span id="cb17-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bind_rows</span>(wave_list))</span>
<span id="cb17-18">}</span></code></pre></div>
</div>
<p><br></p>
<p>Great! This is a smaller function that can probably be more efficient, but it gets the job done. Let’s break it down:</p>
<ul>
<li><p>First we create the function with the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/function"><code>function()</code></a> function (lol). Inside we are supplying two arguments. <code>n</code> which is the amount of wave layers that we want to draw on the ggplot and <code>wave_df</code>, which will be the initial wave data frame we created earlier.</p></li>
<li><p>Next, we create an empty list that will be used to store each <code>n</code> (layer) of waves/data frames.</p></li>
<li><p>Next, we initialize the “for-loop” to run through <code>n</code> amount of iterations. For each iteration, we will mutate the <code>y</code> variable of the original wave data frame, and add a <code>group</code> variable that will automatically be identified by each iteration (<code>i</code>)</p></li>
<li><p>Finally, we have the function return a nice, tidy data frame with all the data for each of our <code>n</code> layers.</p></li>
</ul>
<p>Now we can actually create our data frame with this function like so:</p>
<div class="cell" data-layout-align="center">
<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;"># Creating the final data frame used for plotting====</span></span>
<span id="cb18-2">wave_layers <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;">wave_maker</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, wave)</span></code></pre></div>
</div>
<p><br></p>
<p>We can plot this as is, or we can add some color! Let’s add some color using the <a href="https://www.r-graph-gallery.com/38-rcolorbrewers-palettes.html">RColorBrewer package</a>. Because we made 5 layers back in our <code>wave_maker</code> function, we’ll have five polygons to fill. Let’s just randomly pick 5 colors from the <code>Spectral</code> palette in the package and set them in a vector called <code>colors_pal</code>. Because we want to make sure enough color values are applied to all of the points in the <code>wave_layers</code> data frame, we can use the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/rep"><code>rep()</code></a> function to repeat the values enough times to fill all of the polygons. I’ll touch more on those specifics in a bit:</p>
<div class="cell" data-layout-align="center">
<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;"># Picking a random color palette from RColorBrewer for the waves</span></span>
<span id="cb19-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#We made 5 waves, so let's pick 5 colors#</span></span>
<span id="cb19-3">colors_pal <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;">sample</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spectral"</span>),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(wave_layers)<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;">nrow</span>(wave), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>Finally, we can plot our new waves!</p>
<div class="cell" data-layout-align="center">
<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;"># Final Plotting====</span></span>
<span id="cb20-2">wave_layers <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-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>x,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-4">  <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="cb20-5">  <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;">plot.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;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffffff"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</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>
<span id="cb20-7">               <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>
<span id="cb20-8">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(colors_pal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(wave)))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/codesnip14-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Fantastic! Because I didn’t set a <a href="http://rfunction.com/archives/62">seed</a>, your colors might be different than mine, but that’s OK. While I won’t go into full detail because this tutorial expects you to already be familiar with ggplot and its aesthetics, I will call attention to the group aesthetic and the color aesthetic in this example. If you recall the <code>wave_maker</code> function, it automatically created a new variable called <code>group</code> in each iteration of our wave data frame. The group aesthetic is our way of telling R and the ggplot system that we want to keep data points that are associated with the same group value drawn together. This is how we can get our waves and their colors mapped as intended. If we didn’t set this <code>group</code> argument, our plot would look a little different:</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://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/nogroup-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>You should also note the color aesthetic in the <code>geom_polygon()</code> function. Although we only have 5 layers, we’re actually supplying 460 values to this layer. This is because we need to apply a color value for each point that makes up a polygon. Because our initial wave data frame has 92 rows (or points) in it, if we want to color one wave we’d need 92 color values. If we want to color 5 waves we’d need 92 * 5 which is the 460. This code is set up for reproducibility, so as you change your n in the <code>wave_maker</code> function, it should continue to cycle through whatever color values you supply it.</p>
<p>Depending on how you change the coding for the color palette, you may need to be mindful of the logic needed as you start to explore different possibilities. Always try to make it easier on yourself by coding for reproducibility. I’m sure there are more efficient ways than the methods I’ve presented here but it’s a good start!</p>
<hr>
</section>
<section id="go-forth-and-make-some-waves" class="level1">
<h1>Go Forth and Make Some Waves!</h1>
<p>OK! You have the foundation needed to make some awesome rtistry! Please feel free to take this wave maker function and explore. Use it as an opportunity to practice ggplot iterations, functional programming, building polygons, and more! Not much is required to even make drastic changes to how these waves look. For example, changing how the y variable is mapped in the <code>aes()</code> argument changes the wave:</p>
<div class="cell" data-layout-align="center">
<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;"># You've got the foundation you need now! Go forth and make some awesome waves! Manipulating these aren't too bad#</span></span>
<span id="cb21-2"></span>
<span id="cb21-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#An Example: - Simply changing the y value in the aes call creates a different set of waves :)#</span></span>
<span id="cb21-4">wave_layers <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-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>x,<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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-6">  <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="cb21-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;">plot.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;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffffff"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb21-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</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>
<span id="cb21-9">               <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>
<span id="cb21-10">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(colors_pal, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(wave)))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/making-waves/making-waves_files/figure-html/codesnip15-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p><em>Are you going to make some awesome waves? Or feel the need to strip this code apart and make it your own? Please feel free to do so! If you’re on Twitter and want to share your creations, don’t hesitate to share with the <a href="https://twitter.com/search?q=rtistry&amp;src=spelling_expansion_revert_click">#rtistry tag</a> or even mention me if you want to (<a href="https://twitter.com/meghansharris"><span class="citation" data-cites="meghansharris">@meghansharris</span></a>) I’d absolutely love to see what you create!</em></p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section>

 ]]></description>
  <category>r</category>
  <category>rtistry</category>
  <category>data art</category>
  <category>ggplot2</category>
  <category>r walkthrough</category>
  <category>statistical programming</category>
  <guid>https://www.thetidytrekker.com/post/making-waves/making-waves.html</guid>
  <pubDate>Wed, 24 Nov 2021 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/making-waves/images/makingwaves.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Thinking Outside The Grid - A “bare bones” intro to Rtistry concepts in R using ggplot.</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid.html</link>
  <description><![CDATA[ 






<p><em>Before I write any further, I want to give a special shout out to <a href="https://twitter.com/ijeamaka_a">@ijeamaka_a</a>, <a href="https://twitter.com/djnavarro">@djnavarro</a>, &amp; <a href="https://twitter.com/jiwanheo">@jiwanheo</a> who I personally credit for inspiring me within the last month specifically (my only month being an aRtist so far)!</em></p>
<p><br></p>
<p>So, this post will be a bit different from my others. I’m going to talk about some very basic concepts and perspectives you can think about while starting your own Rtistry journey in ggplot. This includes basics on geoms, aesthetics, layering, etc. But then I’m also going to walk you through two of my Rtistry examples and code to get you started.</p>
<p><br></p>
<p><strong>This article is intended for those who have some experience with ggplot building in R but may not have realized how to transition from making “regular” visuals to Rtistry</strong>. This article goes over basic concepts that more seasoned users may already know. If you believe it is too basic, you can jump straight to the code for the finished pieces to see these basics applied together. Seeing how others have constructed things may help you understand how you can apply your programming knowledge to create Rtistry of your own! For those of you that need these basics, I hope this article is helpful and encourages you to start thinking outside of the ggplot grid and start creating your own pieces!</p>
<p><strong><em>Don’t want to read and just want the code? Here’s the code for:</em></strong> <br></p>
<div class="alignRow">
<div class="imageColumn" style="display:inline-block; float:left; max-width:50%;">
<p><a href="https://github.com/Meghansaha/Data_Art/blob/master/creations/Collages/Planet%20Quad/Planet%20Quad.R"><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/planetquad.png" style="max-width:100%;" alt="Planet Quad - Rtistry - Four different colored planets in outer space made in the R programming language on a 2 x 2 grid"></a> <br></p>
<center>
<a href="https://github.com/Meghansaha/Data_Art/blob/master/creations/Collages/Planet%20Quad/Planet%20Quad.R"><strong>Planet Quad</strong></a>
</center>
</div>
<div class="imageColumn" style="display:inline-block; float:left; max-width:50%;">
<p><a href="https://github.com/Meghansaha/Data_Art/blob/master/creations/Lines/Rainbow%20Rose/Rainbow_Rose.R"><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/rainbowrose.png" style="max-width:100%;" alt="Rainbow Rose - abstract Rtistry of rainbow colored lines jutting from the center of the image in a layered fashion on a black background."></a> <br></p>
<center>
<p><a href="https://github.com/Meghansaha/Data_Art/blob/master/creations/Lines/Rainbow%20Rose/Rainbow_Rose.R"><strong>Rainbow Rose</strong></a></p>
</center>
</div>
</div>
<p><br><br><br></p>
<center>
<p>If you want to follow along with any of the following basic examples, you can find the Intro to Data Art repo on my GitHub <a href="https://github.com/Meghansaha/Intro_to_Data_Art">here</a>.</p>
</center>
<p><br></p>
<section id="introduction" class="level2">
<h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>First things first: let’s get one thing straight, I am NOT an expert, nor am I a computational scientist or a professional generative artist. While knowledge of some “math things” can be super helpful, you don’t need to know complex computational mathematics to make Rtistry (aRt). Granted, if you enjoy and want to create aRt similar to the works of <a href="https://www.data-imaginist.com/art">Thomas Lin Pedersen</a> and <a href="https://art.djnavarro.net/">Danielle Navarro</a>, then maybe you do! But as a beginner, it’s important to remember that your Rtistry is your work and opportunity to learn, grow as an R programmer, and create pieces you can be proud of. Remember that your aRt is an expression of you and no one else and that there is no “right” way to do Rtistry. One of the things I love most about creating aRt is the fact that I don’t have to get anything “right.” For those of us who use R for work/educational endeavors, we must ensure that our statistics, programming, and literally everything else is correct. Creating Rtistry can provide your mind with not only a creative outlet but a way to still practice programming in R in a low-stress environment where things don’t have to be “right.”</p>
<section id="why-rtistry" class="level3">
<h3 class="anchored" data-anchor-id="why-rtistry">Why Rtistry?</h3>
<p>Some people may think of the concept of creating aRt and may think it’s a waste of time since no analyses are performed. If you think that way, you have every right to that opinion. What I can share with you, though, is why I personally love creating Rtistry:</p>
<ul>
<li>Gives my brain a rest from “real work”</li>
<li>Allows me to practice programming skills in a stress-free environment</li>
<li>Gives me satisfaction from flexing my creative skills</li>
<li>Gives me more content for professional portfolios</li>
</ul>
</section>
</section>
<section id="ethics" class="level2">
<h2 class="anchored" data-anchor-id="ethics">Ethics</h2>
<p>While using others’ work as inspiration to give yourself ideas is OK, you should always strive to create something of your own merit. Just like in the physical art world, you may also find that some aRtists may not want to share their process or code with you. Those that are new to Rtistry may be shocked by this. Some newbies may even think that creators come off as standoffish or rude if they decline to share their work. This could be because of how supportive the R community usually is and how we are always seemingly ready to share our code.</p>
<p>One thing to remember is that a creator could decline to share their code for many reasons. Those decisions are personal and left up to each aRtist on an individual basis. The aRtists that do exceptionally unique work will have usually put a lot of effort and hours into their pieces to a point where it may even feel proprietary. Some aRtists choose to sell their outputs not only because they have made something truly personal to them, but because they’ve also made something unique that probably hasn’t existed before.</p>
<p>When you find an Rtistry piece that appeals to you, you could try to understand what’s happening behind the scenes instead of asking directly for the code. It tends to be a fun challenge to figure out what’s happening yourself. It’s a lot of work, yes, but I am willing to bet that if you use R for professional work, the work and study you do to figure out how to create your pieces will benefit you in the long run.</p>
<p>What you choose to do with the pieces you create is entirely up to you. Some aRtists get their work printed for sale or personal use, some look into selling them as <a href="https://en.wikipedia.org/wiki/Non-fungible_token">Non-fungible Tokens (NFTs)</a> and some literally save their output and never save their code, completely erasing its footprint in a cathartic click of a button. Whatever you do, always give credit where credit is due; don’t assume that everyone is willing to share their code with you (but if they do, always express gratitude and give credit for the assist); and try to be ethical in everything that you do.</p>
</section>
<section id="geoms" class="level2">
<h2 class="anchored" data-anchor-id="geoms">Geoms</h2>
<p>If you want to start making data aRt in ggplot, having some knowledge on ggplot geoms will prove to be very useful. If you’ve ever participated in a <a href="https://github.com/rfordatascience/tidytuesday">#tidytuesday challenge</a> or have made visuals in <a href="https://ggplot2.tidyverse.org/index.html">ggplot</a>, you’re probably familiar with them. The key to using geoms to create your aRt is to understand what type of data is expected for each geom. Some basic ones to start with are:</p>
<ul>
<li><a href="https://ggplot2.tidyverse.org/reference/geom_point.html"><code>geom_point()</code></a></li>
<li><a href="https://ggplot2.tidyverse.org/reference/geom_path.html"><code>geom_line()</code> &amp; <code>geom_path()</code></a></li>
<li><a href="https://ggplot2.tidyverse.org/reference/geom_segment.html"><code>geom_segment()</code> &amp; <code>geom_curve()</code></a></li>
<li><a href="https://ggplot2.tidyverse.org/reference/geom_polygon.html"><code>geom_polygon()</code></a></li>
</ul>
<p>There is no limit to which geoms you can use for aRt in ggplot. Your only barrier would be not having your data formatted appropriately for whichever geom you wish to use. For example, if you want to use <code>geom_segment()</code> you’d need to know that this function expects four different data points (<code>x</code>, <code>xend</code>, <code>y</code>, and <code>yend</code>). The geom functions will usually do a good job of throwing an error and letting you know if you’re missing any data points. If you’re interested, you can always learn more about ggplot with the official <a href="https://github.com/rstudio/cheatsheets/blob/master/data-visualization-2.1.pdf">cheat sheet</a> or <a href="https://ggplot2-book.org/">book</a>.</p>
<section id="geoms-examples" class="level3">
<h3 class="anchored" data-anchor-id="geoms-examples">Geoms Examples:</h3>
<p>You can think of basic geoms as instruments (paintbrushes) in your Rtistry toolkit. Here are a few simple examples of how each can be used:</p>
<p><strong>Geom points</strong> can be used for textures, smaller details, or optical illusions like in the image <em>Candy Rope</em>:</p>
<center>
<img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/candyrope.png" alt="An abstract art piece created in the R programming language." style="max-width:100%">
<figcaption>
<i>Candy Rope</i>
</figcaption>
</center>
<p><br><br></p>
<p><strong>Geom line</strong> and <strong>path</strong> can be used to create linear details and abstract aRt :</p>
<center>
<img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/redfury.png" alt="An abstract art piece created in the R programming language." style="max-width:100%">
<figcaption>
<i>Linear Fury</i>
</figcaption>
</center>
<p><br><br></p>
<p><strong>Geom segment</strong> &amp; <strong>geom curve</strong> can be used for linear/curved details, or larger components:</p>
<center>
<img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/moonscape.png" alt="An abstract art piece created in the R programming language." style="max-width:100%">
<figcaption>
<i>Moonscape</i>
</figcaption>
</center>
<p><br><br></p>
<p><strong>Geom Polygon</strong> can be used to make any shape that can exist on ggplot’s coordinate system, Like lots of randomized squares:</p>
<center>
<img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/besquare.png" alt="An abstract art piece created in the R programming language." style="max-width:100%">
<figcaption>
<i>Be Squared</i>
</figcaption>
</center>
<p><br><br></p>
</section>
</section>
<section id="making-and-applying-rtistry-art-data-to-geoms" class="level2">
<h2 class="anchored" data-anchor-id="making-and-applying-rtistry-art-data-to-geoms">Making and Applying (Rtistry) aRt Data to Geoms</h2>
<p>If the geoms are the instruments in your toolkit, the data is your ink/paint. The data you provide to each geom will produce visuals on ggplot’s coordinate system. Thinking about it broadly, to make one point on a basic ggplot you need at least one x and one y value:</p>
<div class="cell" data-layout-align="center">
<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 everything data====</span></span></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.0     ✔ stringr   1.5.2
✔ 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="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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 1: Data Frame====</span></span>
<span id="cb3-2">single_point <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;">tibble</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;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 1 ggplot====</span></span>
<span id="cb3-5">single_point <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb3-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> x, <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></span>
<span id="cb3-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip1-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>A Single Point</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Having several x and y points is what will get you some shapes, lines, and other things:</p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 2: Data Frame====</span></span>
<span id="cb4-2">random_lines <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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;">500</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>),</span>
<span id="cb4-3">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">50</span>)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 2: ggplot====</span></span>
<span id="cb4-6">random_lines <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(x), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(y)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip2-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Random Lines</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>The magic happens when you can start viewing the grid differently. When you begin to understand the relationship the values in a data frame have with the ggplot coordinate system, it becomes easier to realize what data is needed to create your desired output. For example, if you want to create abstract art, you might practice making datasets with randomized values. If you want to make intentional pieces, you might spend time studying the axes of the ggplot and determine what range of values you need to make the figures you want. The process of creating data for Rtistry and applying them to geoms can be as simple or complicated as your creativity allows. Let’s just go over a simple example with a basic square.</p>
<p>We know that a square consists of four lines. Each line has a start and endpoint. So, we at least need to make four points on our plot. Since each point consists of two values (x and y), we’ll need eight values in total. Four x points and four y points.</p>
<p><br></p>
<div class="cell">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 3: Data Frame====</span></span>
<span id="cb5-2">square <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>), </span>
<span id="cb5-3">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">5</span>),</span>
<span id="cb5-4">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">labels =</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;">4</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>Because we want to make a shape that’s closed (so we could potentially fill it with color), we’d want to use <code>geom_polygon()</code>. You might be thinking “why not use <code>geom_line()</code> instead?” because a square consists of four lines, but let’s look at that. If we make a data set of the points that are supposed to make a square but then pass it through to <code>geom_line()</code> or <code>geom_path()</code> functions, we get these instead:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 3: Geom_line() ggplot====</span></span>
<span id="cb6-2">square <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-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> x,</span>
<span id="cb6-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb6-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> labels))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb6-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip4-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>“Square” made with geom_line()</figcaption>
</figure>
</div>
</div>
</div>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 4: Geom_path() ggplot====</span></span>
<span id="cb7-2">square <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb7-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> x,</span>
<span id="cb7-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb7-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> labels))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb7-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip5-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>“Square” made with geom_path()</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p><strong>geom_line()</strong> looks this way because it plots points in order by the x axis. Try to take a moment to compare the data and the result and understand why it came to be.</p>
<p><strong>geom_path()</strong> looks closer to a square! But we’re missing a side! This is because <code>geom_path()</code> doesn’t create a fully closed shape by default. <code>geom_path()</code> is the same as <code>geom_line()</code> except it plots points in the order in which it appears in the dataset.</p>
<p>Now let’s use <strong>geom_polygon()</strong> with the same data set:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 5: Geom_polygon ggplot====</span></span>
<span id="cb8-2">square <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;">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>
<span id="cb8-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb8-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> labels))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip6-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>“Square” made with geom_polygon()</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>There we go; An actual square. <code>geom_polygon()</code> is similar to how <code>geom_path()</code>’s process of drawing lines works. The only difference is that the start and endpoints are automatically connected and you can add a “fill” aesthetic to your new shape. By default, ggplot does fill the square. You can set <code>fill = NA</code> to make it empty, but remember to set a color argument if you want to see the “border” of the polygon make its shape:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 6: Geom_Polygon ggplot with no fill====</span></span>
<span id="cb9-2">square <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;">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>
<span id="cb9-4">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y,</span>
<span id="cb9-5">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> labels))<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;">geom_polygon</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> <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="cb9-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_label</span>()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip7-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Square - geom_polygon() ggplot with no fill</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>As a beginner, it’s tempting to try and make every cool idea that comes to you, but it’s a good idea to spend time with these basics first. You can explore creating aRt data sets with formulas you may be interested in or with any of the “non-visual” functions I’ll discuss later in the article. Once you feel comfortable creating aRt datasets and applying them to geoms, you might be able to find cool and unexpected ways to plot things!</p>
<p><br></p>
</section>
<section id="aesthetics" class="level2">
<h2 class="anchored" data-anchor-id="aesthetics">Aesthetics</h2>
<p>The next important set of basics to learn about is ggplot aesthetics. We’ve already used aesthetics in the previous section’s examples, but let’s think about them a bit more. Each base ggplot and geom function comes with available aesthetics. If you’ve ever made a ggplot visualization and changed any colors, shapes, etc., you’re already familiar with the foundations needed to start customizing the look/style of your work.</p>
<p>There’s two ways you can map aesthetics onto your visuals:</p>
<ol type="1">
<li>The <a href="https://ggplot2.tidyverse.org/reference/aes.html"><code>aes()</code></a> function in your initial ggplot() function</li>
<li>Directly into the geom function you wish to change.</li>
</ol>
<p>As you explore what you want to do and how you want to style your data, you may find that you prefer one way over the other. If you get stuck on this part, take some time to think about what you’d like to see on the grid and then think logically about which method would make sense.</p>
<p>For example, if you have a variable that contains <a href="https://en.wikipedia.org/wiki/Web_colors">hex color codes</a> attached to each row in a dataset, it may make sense to set up the aesthetic in the <code>aes()</code> function since it’s already connected to the data. If you have a circumstance where you only want one geom to be one color, or you want to map a different data set of aesthetics to the geom, you can put it into the actual function.</p>
<p><strong>A word of caution though, if you want to add values from a different dataset, make sure they have the same number of rows as the initial data set that you are piping into the initial ggplot’s function.</strong> If they don’t match up and you don’t set the <code>inherit.aes</code> argument to <strong><code>FALSE</code></strong> in the geom, you’ll get an error. There are ways to use different data sets in addition to the initial one used at the beginning of your ggplot call. I’ll touch on this in the layering section of this article. For now, let’s take a quick look into setting aesthetics in our square example:</p>
<p>Let’s say we want to color our square purple and make colored dots on each point of the square. Because the polygon will only be filled with one color, we can set that in the <code>geom_polygon()</code> call. If we add a color variable to our data set with individual hex color codes attached to each observation, we can place it in the <code>aes()</code> function and get our desired output with the following code:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 7: Geom_Polygon ggplot with colored points====</span></span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 7: Data Frame====</span></span>
<span id="cb10-4">square_color_points <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">0</span>),</span>
<span id="cb10-5">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">5</span>),</span>
<span id="cb10-6">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colors =</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;">"#c0d943"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#027337"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#d12017"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000a96"</span>))</span>
<span id="cb10-7">                 </span>
<span id="cb10-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 7: Geom_Polygon ggplot with colored points====</span></span>
<span id="cb10-9">square_color_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb10-10">  <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>
<span id="cb10-11">             <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></span>
<span id="cb10-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</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;">"#4b1980"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-13">  <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> square_color_points<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>colors)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb10-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip8-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Square- geom_polygon() with colored points</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>We can also change multiple aesthetics at once. We can change the size of our points too:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 8: Geom_Polygon ggplot with colored points and Random Sizes====</span></span>
<span id="cb11-2">square_color_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb11-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> x,</span>
<span id="cb11-4">             <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></span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</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;">"#4b1980"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-6">  <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> square_color_points<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>colors,</span>
<span id="cb11-7">             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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;">20</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(square_color_points)))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip9-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Square - geom_polygon() with randomly sized points</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>As you get more comfortable with aesthetics, you will find that there may be different ways to explore these settings programmatically using various ideas or functions. One of the greatest things about Rtistry is realizing that you may already have the programming knowledge necessary to use these basic components to create amazing things.</p>
<p>At the end of the day, if you find yourself stuck on how to do something, it may be helpful to search google, the <a href="https://community.rstudio.com/">RStudio Community forums</a>, or even <a href="https://stackoverflow.com/questions/tagged/r">Stack Overflow</a> for the answers to programming questions that may help you piece together your Rtistry. In my experience, I’ve always had the best results when I take a moment to stop and think about what I am asking R to do programmatically. R doesn’t know that I want to randomize colors for an image, but it will know if I want it to pick random hex color values from a vector by using the <code>sample()</code> function. I can then apply that result to an aesthetic option and get the desired output.</p>
<p>If I were googling for the answer to this example, I wouldn’t type <em>“how to randomize colors of geom_point()”</em> but instead I’d type <em>“how to randomly pick values from a vector in R programming.”</em> It may take a little bit to understand this thought process, but you are programming after all, so try to approach any troubleshooting as if you were coding for “actual work”.</p>
<p><br></p>
</section>
<section id="using-non-visual-functions-for-your-visuals" class="level2">
<h2 class="anchored" data-anchor-id="using-non-visual-functions-for-your-visuals">Using “non-visual” Functions For Your Visuals</h2>
<p>The first time I realized that you could pass additional functions into aesthetic options - my mind was blown! I did not realize that anything other than a string of a hex color could go into a color aesthetic in ggplot. My mind was so stuck on how I learned to code for ggplot. It was also bound by the fact that I never needed to think about setting any options outside of regular color “strings” for my everyday work. Take the random data below. We could change the colors of these random points by applying some random colors…</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 9: Data Frame====</span></span>
<span id="cb12-2">random_points <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;">tibble</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;">100</span>,</span>
<span id="cb12-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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;">100</span>))</span>
<span id="cb12-4"></span>
<span id="cb12-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 9: ggplot of randomized points====</span></span>
<span id="cb12-6">random_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-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>x, <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></span>
<span id="cb12-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>()</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip10-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Ggplot of randomized points</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 10: ggplot of randomized points with randomized colors====</span></span>
<span id="cb13-2">random_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-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>x, <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></span>
<span id="cb13-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;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRGn"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(random_points), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip11-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Ggplot of randomized points with randomized colors</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>…Or by placing a logical function in the color aesthetic for <code>geom_point()</code>. For example, we can tell R that we only want to apply randomized colors to any data points that have a y value that’s less than 50, otherwise observations with a y value that’s greater than or equal to 50 will just be colored black with an <code>ifelse()</code> function:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 11: ggplot of randomized points with randomized colors and logic applied====</span></span>
<span id="cb14-2">random_points <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-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>x, <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></span>
<span id="cb14-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;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(random_points<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y <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;">50</span>,</span>
<span id="cb14-5">                            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"PRGn"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(random_points), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb14-6">                            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip12-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Ggplot of randomized points with randomized colors and logic applied</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Since there is no limit to the different types of functions a user can make, the possibilities of different functions that can be added to aesthetics seems endless. A few functions that might help you not only in aesthetics, but working with your data prepping are listed below to get you started:</p>
<p><br></p>
<ul>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/sample"><code>sample()</code></a> – Randomly take a sample from elements of an object</li>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/rep"><code>rep()</code></a> – Replicate (repeat) elements in a vector or list</li>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/seq"><code>seq()</code></a> – Generates numeric sequences</li>
<li><a href="https://www.rdocumentation.org/packages/compositions/versions/2.0-2/topics/runif"><code>runif()</code></a> – Returns a random composition of numbers with a uniform distribution</li>
<li><a href="https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/Normal"><code>dnorm()</code> &amp; <code>rnorm()</code></a> – Creates a normal density distribution and random normal distribution of numbers</li>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Trig">Trigonometric Functions (<code>cospi()</code>,<code>cos()</code>,<code>sin()</code>, etc.)</a> – Results in the computation of various trigonometric functions.</li>
<li><a href="https://tidyr.tidyverse.org/reference/expand.html"><code>Crossing()</code>, <code>expand()</code>, <code>nesting()</code></a> – Tidyr functions that expand data frames to include possible combinations of values.</li>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Control">Control-flow constructs (<code>while</code>, <code>repeat</code>,<code>if</code>, <code>for</code>, etc.)</a> – Can be used to create iterations, patterns, and functions of your own.</li>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/apply">Apply</a> family functions (<a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/lapply"><code>lapply</code>, <code>sapply</code>, etc.</a>) – Can be used to apply functions on different objects like matrices, vectors, or lists.</li>
</ul>
<p><br></p>
<p>Remember that this is not an exhaustive list, but just a taste of some commonly used functions to hopefully get the gears turning about different ways you can manipulate your ggplot aesthetics and actual Rtistry data.</p>
</section>
<section id="layering" class="level2">
<h2 class="anchored" data-anchor-id="layering">Layering</h2>
<p>The last basic concept in this post is ggplot layering. Adding layers with the <a href="https://ggplot2.tidyverse.org/reference/layer.html"><code>layer()</code></a> function or even annotations with the <a href="https://ggplot2.tidyverse.org/reference/annotate.html"><code>annotate()</code></a> function is very useful as it allows the user to add different layers of data. You can also achieve this by adding geom functions as you would normally, but if you ever want to add geoms that pull from different data sources this way, you need to set the <code>inherit.aes</code> argument to <code>FALSE</code>, as this will then allow the ggplot to build without error.</p>
<p>These multiple ways of layering allows for endless possibilities as piecing together an image this way provides a bit more flexibility. Take the two datasets below. One is intended to just create some lines with <code>geom_segment()</code>, and the other is intended to just create circles in between each line with <code>geom_point()</code>. Because we want 4 lines and 3 circles, the amount of data needed to map these things don’t match up. We can tell this on a quick glance by seeing that the number of rows in each data set don’t match up:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 12: Lines_data Data Frame====</span></span>
<span id="cb15-2">Lines_data <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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;">4</span>),</span>
<span id="cb15-3">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</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;">4</span>),</span>
<span id="cb15-4">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">0</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;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb15-5">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> y )</span>
<span id="cb15-6"></span>
<span id="cb15-7"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 12: Circles_data Data Frame====</span></span>
<span id="cb15-8">Circles_data <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;">tibble</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;">3</span>,</span>
<span id="cb15-9">                       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</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;">0</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;">by =</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;">1</span>)),</span>
<span id="cb15-10">                       <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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb15-11"></span>
<span id="cb15-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">##Logic Check: Do these dataframes have the same number of row?##</span></span>
<span id="cb15-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Lines_data) <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;">nrow</span>(Circles_data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
<p><br></p>
<p>If we try to add these together by just using a geom function ,<code>geom_point()</code>, and supplying the <code>Circles_data</code> directly into <code>geom_point()</code> without typing <code>inherit.aes = FALSE</code>, the console will throw an error:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 12: Lines_data and Circles_data error====</span></span>
<span id="cb17-2">Lines_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</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;">15</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb17-5">  <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;">data =</span> Circles_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, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> size)) </span></code></pre></div>
</div>
<p><br></p>
<p>This error tells you that object <code>xend</code> is not found. This is because we are attempting to override the data frame that is attached to the entire ggplot (<code>Lines_data</code>) with <code>Circles_data</code> now and <code>xend</code> definitely does not exist in <code>Circles_data</code> and therefore “can’t be found.” To fix this specific example and get the desired results, simply add <code>inherit.aes = FALSE</code> into the <code>geom_point()</code> function. If desired, you also could just move the aesthetics for <code>Lines_data</code> into the <code>geom_segment()</code> function but as you will find, there are multiple ways to get to the same result. For this example though, we’ll just add the <code>inherit.aes</code> argument to <code>geom_point()</code>.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Example 12: Adding Lines_data and Circles_data together with inherit.aes in geom====</span></span>
<span id="cb18-2"></span>
<span id="cb18-3">Lines_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb18-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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</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;">15</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb18-6">  <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;">data =</span> Circles_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, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> size), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></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-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip15-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>ggplot of Lines_data and Circles_data layered together with the addition of geom_point()</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>We can also use the <code>layer()</code> function or the <code>annotate()</code> function to achieve similar results. Both methods are used in the example below:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 13: Adding Lines_data and Circles_data together with layer function====</span></span>
<span id="cb20-2">Lines_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb20-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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_segment</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;">15</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">layer</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;">"point"</span>,</span>
<span id="cb20-6">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> Circles_data,</span>
<span id="cb20-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>,</span>
<span id="cb20-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>,</span>
<span id="cb20-9">        <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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> size),</span>
<span id="cb20-10">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">inherit.aes =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip16-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>ggplot of Lines_data and Circles_data layered together with the layer() function</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Example 14: Adding Lines_data and Circles_data together with annotate function====</span></span>
<span id="cb21-2">Lines_data <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>x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span>y, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xend =</span> xend, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yend =</span> yend))<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_segment</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;">15</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;">annotate</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;">"point"</span>,</span>
<span id="cb21-6">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x=</span> Circles_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>x, </span>
<span id="cb21-7">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y=</span> Circles_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>y, </span>
<span id="cb21-8">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> Circles_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>size)</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip17-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>ggplot of Lines_data and Circles_data layered together with the annotate() function</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Both functions produce similar images. The <code>layer()</code> and <code>annotate()</code> functions have their own pros and cons based on what you’d like to do. As shown in the code snippets, each method of layering in ggplot is similar but has its own differences that may be favored in certain situations. I’d encourage you to read and explore these methods of layering a bit more, and to see what works best to execute your ideas.</p>
<p><br></p>
</section>
<section id="planet-quad-art-example" class="level2">
<h2 class="anchored" data-anchor-id="planet-quad-art-example">Planet Quad – aRt Example</h2>
<br> <img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/planetquad.png" style="max-width:100%;" alt="Planet Quad - Rtistry - Four different colored planets in outer space made in the R programming language on a 2 x 2 grid">
<figcaption>
<i>
<center>
Planet Quad
</center>
</i>
</figcaption>
<p><br></p>
<p>Now that we’ve gone over some technical basics, let’s look at two Rtistry examples I’ve created. The first is Planet Quad. The creation of Planet Quad demonstrates how we can utilize other packages like <a href="https://patchwork.data-imaginist.com/index.html">patchwork</a> and <a href="https://wilkelab.org/cowplot/index.html">cowplot</a> to create our own Rtisrty. Let’s break this down step-by-step.</p>
<p>Each layer in ggplot is built on top of each other in the order in which it appears in the code. For this image, that means we want the space/stars background to be <strong>behind</strong> our planets- so we can start with that <strong>first</strong>.</p>
<p>We want four different plots with a similar design in different colors, so we can start by creating a named vector of the background colors we want to use. I always use hex colors, but you can use whatever format you’d like. I’ll name mine <code>Space_colors</code>:</p>
<p><br></p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Library Load-In====</span></span>
<span id="cb22-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 everything data</span></span>
<span id="cb22-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(cowplot) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For placing plots on top of each other easily with ggdraw and draw_image</span></span></code></pre></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:lubridate':

    stamp</code></pre>
</div>
<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;">library</span>(patchwork) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For packaging up the plots in the final image</span></span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'patchwork'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following object is masked from 'package:cowplot':

    align_plots</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Star Backgrounds====</span></span>
<span id="cb28-2"></span>
<span id="cb28-3"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Making four different stars "backgrounds" with four different colors====</span></span>
<span id="cb28-4">Space_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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topleft"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#000000"</span>,</span>
<span id="cb28-5">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topright"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#130321"</span>,</span>
<span id="cb28-6">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomleft"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#330500"</span>,</span>
<span id="cb28-7">                  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomright"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#8a4500"</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>Next, we want to create the data for the stars in our backgrounds. Because I want the image to look pretty filled in, I’ll apply the <code>crossing()</code> function to the data in this <code>tibble</code> that I’ll name stars:</p>
<p><br></p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Making a data set that will be used to create all star backgrounds====</span></span>
<span id="cb29-2">df_stars <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;">tibble</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossing</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>),</span>
<span id="cb29-3">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">1</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)))</span></code></pre></div>
</div>
<p><br></p>
<p>Next, we want to make a list because we’ll run iterations of our <code>df_stars</code> dataset through a ggplot call that will also iterate through our vector Space_colors. This will result in a list of four similar plots that all have a different color scheme. We’ll name this list <code>All_stars</code> and name the plots inside of the list to make it easier to track each plot as our session continues:</p>
<p><br></p>
<div class="cell">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Making the list to hold the "space" plots====</span></span>
<span id="cb30-2">All_stars <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;">list</span>()</span>
<span id="cb30-3"></span>
<span id="cb30-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Iterating through the "space_colors" to make four different plots====</span></span>
<span id="cb30-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(Space_colors)){</span>
<span id="cb30-6">  </span>
<span id="cb30-7">  All_stars[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_stars <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb30-8">    <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="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></span>
<span id="cb30-9">    <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;">size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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;">02</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">04</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">06</span>,.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df_stars), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb30-10">    <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="cb30-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;">plot.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;">fill =</span> Space_colors[i], <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;">"#ffffff"</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;">6</span>)) </span>
<span id="cb30-12">}</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
ℹ Please use the `linewidth` argument instead.</code></pre>
</div>
<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"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Naming the plots just to help keep track of what's going where====</span></span>
<span id="cb32-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(All_stars) <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;">names</span>(Space_colors)</span></code></pre></div>
</div>
<p><br></p>
<p>Now we can check and look at one of our plots by typing All_stars$Topleft in the console to verify that it looks like what we were expecting:</p>
<div class="cell" data-layout-align="center">
<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">All_stars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Topleft</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip21-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>The “Topleft” image in the All_stars list. A ggplot of the stars data</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Now, we can work on our planets. The planets are actually just a <a href="https://ggplot2.tidyverse.org/reference/geom_density_2d.html"><code>geom_density2d_filled()</code></a> plot that will go through four iterations of the planet data set with its coordinate system set to [<code>coord_polar()</code>](https://ggplot2.tidyverse.org/reference/coord_polar.html. Just like we did for the stars data, we also want four very similar plots with different colors. Again, we can make a similar function to create a list of these plots. We just need to generate four different iterations of the data set and pick some colors for the planet surfaces. Let’s start with picking color palettes for planets and different colors for the border lines of each planet. We can place each palette into a list called <code>Planet_colors</code> and each border color into a vector called <code>Planet_borders</code>:</p>
<p><br></p>
<div class="cell">
<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;"># Planet Creations====</span></span>
<span id="cb34-2"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Setting the color palettes for each planet quad====</span></span>
<span id="cb34-3">Planet_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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topleft"</span> <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#194157"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#008dd7"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#085b88"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#26925e"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#095c88"</span>),</span>
<span id="cb34-4">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topright"</span> <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#480463"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#392242"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1e0329"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#bf9232"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#120b17"</span>),</span>
<span id="cb34-5">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomleft"</span> <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#47322d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6b1c09"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a30000"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#6b1d09"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#851205"</span>),</span>
<span id="cb34-6">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomright"</span> <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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#c7a602"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#998523"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ba690d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#755b3d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#dbab39"</span>))</span>
<span id="cb34-7"></span>
<span id="cb34-8"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Setting the colors of each planet's borders====</span></span>
<span id="cb34-9">Planet_borders <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;">"Topright"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#333333"</span>,</span>
<span id="cb34-10">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topright"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#120b17"</span>,</span>
<span id="cb34-11">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomleft"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#260f09"</span>,</span>
<span id="cb34-12">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomright"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#5c523a"</span>)</span></code></pre></div>
</div>
<p>We’ll create some randomly sampled data to draw out our planets to encourage some weirdness in its final presentation. We’ll name the dataset planet:</p>
<p><br></p>
<div class="cell">
<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;"># Making a dataset that will be used to create all the planets====</span></span>
<span id="cb35-2">planet <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;">tibble</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">crossing</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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;">1000</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;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb35-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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;">2000</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;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)))</span></code></pre></div>
</div>
<p><br></p>
<p>Now we’ll create our list, named All_planets, that will be filled with our four different planets after it runs through our for-loop:</p>
<p><br></p>
<div class="cell">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Making the list to hold the "planet" plots====</span></span>
<span id="cb36-2">All_planets <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;">list</span>()</span>
<span id="cb36-3"></span>
<span id="cb36-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Iterating through the "planet_colors" to make four different planet plots====</span></span>
<span id="cb36-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(Planet_colors)){</span>
<span id="cb36-6">  </span>
<span id="cb36-7">  All_planets[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> planet <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb36-8">    <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="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></span>
<span id="cb36-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</span>(<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;">sample</span>(Planet_colors[[i]],<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;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</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;">#100 is just a arbitrary "safe" number I picked. geom_density does background calcs to create levels that varies based on data.</span></span>
<span id="cb36-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_density2d_filled</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> Planet_borders[i], <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="cb36-11">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_polar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">clip =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"on"</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb36-12">    <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="cb36-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>
<span id="cb36-14">}</span>
<span id="cb36-15"></span>
<span id="cb36-16"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Naming the plots just to help keep track of what's going where====</span></span>
<span id="cb36-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(All_planets) <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;">names</span>(Planet_colors)</span></code></pre></div>
</div>
<p><br></p>
<p>All these plots are now in our list <code>All_planets</code>. We can peek at one by typing <code>All_planets$Bottomright</code> into the console. Because a seed hasn’t been set anywhere, yours may look different than mine and that’s all right!</p>
<div class="cell" data-layout-align="center">
<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">All_planets<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Bottomright</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip25-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>The “Bottomright” image in the All_stars list. A ggplot of the stars data</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Now we can save all these planet plots as images out into the directory so we can load them right back in as <a href="https://en.wikipedia.org/wiki/Portable_Network_Graphics">png files</a>. Please note that if you are following along, you may have to alter the directory to avoid errors as this directory is located within my <a href="https://github.com/Meghansaha/Data_Art/tree/master/creations/Collages/Planet%20Quad">Data_aRt GitHub repo</a> in my Planet Quad project file.</p>
<p><br></p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Saving Planets into the directory====</span></span>
<span id="cb38-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(All_planets)){</span>
<span id="cb38-3">  </span>
<span id="cb38-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggsave</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"planets/"</span>,</span>
<span id="cb38-5">              <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(All_planets)[i],</span>
<span id="cb38-6">              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_planet.png"</span>),</span>
<span id="cb38-7">       All_planets[[i]],</span>
<span id="cb38-8">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bg =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"transparent"</span>, </span>
<span id="cb38-9">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">device =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"png"</span>)</span>
<span id="cb38-10">}</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Saving 7 x 5 in image
Saving 7 x 5 in image
Saving 7 x 5 in image
Saving 7 x 5 in image</code></pre>
</div>
</div>
<br> <br> <img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/snip1.png" style="width:100%;" alt="A snapshot of the processed PNG files in my personal working directory locally.">
<figcaption>
<i>
<center>
A snapshot of the processed PNG files in my personal working directory locally.
</center>
</i>
</figcaption>
<p><br></p>
<p>Next, we need to bring the processed png files back into the environment. We can do this with the <a href="http://www.rforge.net/png/index.html">png package</a>. Because I’m only using this once, I chose to call in the <a href="https://www.rdocumentation.org/packages/png/versions/0.1-7/topics/readPNG"><code>readPNG()</code></a> function directly from the package instead of loading the package itself. We’ll store the images in a list called <code>Planet_PNGs</code>.</p>
<p><br></p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading Planets back into the environment as PNGs====</span></span>
<span id="cb40-2">Planet_PNGs <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;">list</span>()</span>
<span id="cb40-3"></span>
<span id="cb40-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(All_planets)){</span>
<span id="cb40-5">  </span>
<span id="cb40-6">Planet_PNGs[[i]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> png<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;">readPNG</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"planets/"</span>,</span>
<span id="cb40-7">                                        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(All_planets)[i],</span>
<span id="cb40-8">                                        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_planet.png"</span>))</span>
<span id="cb40-9">}</span>
<span id="cb40-10"></span>
<span id="cb40-11"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Setting names to keep track of the planets====</span></span>
<span id="cb40-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Planet_PNGs) <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;">names</span>(All_planets)</span></code></pre></div>
</div>
<p><br></p>
<p>Now that we have png files, we can place these “on top” of our star backgrounds we made earlier using the <a href="https://www.rdocumentation.org/packages/cowplot/versions/1.1.1/topics/ggdraw"><code>ggdraw()</code></a> and <a href="https://www.rdocumentation.org/packages/cowplot/versions/1.1.1/topics/draw_image"><code>draw_image()</code></a> functions from the cowplot package with for-loops. This time, we’ll store them in a list called <code>Combined_plots</code>:</p>
<p><br></p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Combining both the stars and planets to create four plots in total====</span></span>
<span id="cb41-2">Combined_plots <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;">list</span>()</span>
<span id="cb41-3"></span>
<span id="cb41-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span>(i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(All_planets)){</span>
<span id="cb41-5">  </span>
<span id="cb41-6">  Combined_plots[[i]] <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;">ggdraw</span>(All_stars[[i]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb41-7">                                  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">draw_image</span>(Planet_PNGs[[i]])</span>
<span id="cb41-8">}</span>
<span id="cb41-9"></span>
<span id="cb41-10"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Setting names to keep track of the plots===</span></span>
<span id="cb41-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Combined_plots) <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;">names</span>(Planet_PNGs)</span></code></pre></div>
</div>
<p><br></p>
<p>Just like before, we can take a peek at any of the plots in the Combined_plots list to make sure it looks as expected. I’ll use <code>Combined_plots$Bottomleft</code> in the console to see:</p>
<div class="cell" data-layout-align="center">
<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">Combined_plots<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Bottomleft</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip29-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>The stars and planet plots combined</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Looking good! Almost done! Now we can <a href="https://purrr.tidyverse.org/reference/pluck.html"><code>pluck()</code></a> each of our plots out and save them to individual objects! Because there’s only four of them, I’ll just copy and paste that code because this is Rtistry and it’s totally OK to be lazy. It’s your work. It’s your aRt . Do what you want!</p>
<p><br></p>
<div class="cell">
<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"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Plucking out all the individual plots===</span></span>
<span id="cb43-2">Topleft <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Combined_plots <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;">pluck</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topleft"</span>)</span>
<span id="cb43-3">Topright <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Combined_plots <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;">pluck</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Topright"</span>)</span>
<span id="cb43-4">Bottomright <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Combined_plots <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;">pluck</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomright"</span>)</span>
<span id="cb43-5">Bottomleft <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Combined_plots <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;">pluck</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bottomleft"</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>Finally, we use <a href="https://patchwork.data-imaginist.com/articles/guides/layout.html">pacthwork’s layout syntax</a> to combine these plots into one image:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Final output construction with patchwork functions====</span></span>
<span id="cb44-2">Planet_Quad <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (Topleft <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Topright) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (Bottomleft <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> Bottomright)</span>
<span id="cb44-3"></span>
<span id="cb44-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View the piece#</span></span>
<span id="cb44-5">Planet_Quad</span></code></pre></div>
</div>
<p><br></p>
<p>And voila! We have our finished piece! I’ve noticed that sometimes patchwork may render in some borders around these images if <a href="https://ggplot2.tidyverse.org/reference/ggsave.html"><code>ggsave()</code></a> is used. In these cases, I just export using the Export button in the RStudio IDE.</p>
<p><br></p>
</section>
<section id="rainbow-rose-art-example" class="level2">
<h2 class="anchored" data-anchor-id="rainbow-rose-art-example">Rainbow Rose – aRt Example</h2>
<br> <img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/rainbowrose.png" style="max-width:100%;" alt="An abstract image that was created in the R programming language. It shows a spiral on lines progressing from the center of the image towards the user with a progressive rainbow color palette on a black background">
<figcaption>
<i>
<center>
Rainbow Rose
</center>
</i>
</figcaption>
<p><br></p>
<p>Finally, we’re at our last example! Rainbow Rose is an Rtistry piece that showcases one way trigonometric functions can be used to create cool results. This Rtistry piece was created from a beautiful accident when I mistyped some code while attempting to draw a circle from scratch in ggplot (yes, the <a href="https://ggforce.data-imaginist.com/reference/geom_circle.html"><code>geom_circle()</code></a> function from <a href="https://ggforce.data-imaginist.com/index.html">ggforce</a> exists, but I was trying to improve my manual ggplot skills 🤷🏾️).</p>
<p><br></p>
<p>To start out, we need to set up some specifications that will alter how the data is plotted onto ggplot’s Cartesian coordinate system. This includes calculating the “theta”(angles) of our intended circle, the number of divisions/data points we want in the dataset “<code>n</code>”, and the radial setting we wish to have for the visual. Because we want to give an effect that starts from the center of the image, we set the radial setting, “<code>r</code>” to a vector that spans from 1 to our “<code>n</code>” value.</p>
<p><br></p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Library Load-In====</span></span>
<span id="cb45-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 everything data</span></span>
<span id="cb45-3"></span>
<span id="cb45-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting parameters to prep ggplot to plot data in a "circular" fashion on the Cartesian coordinate system====</span></span>
<span id="cb45-5"></span>
<span id="cb45-6"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Angle "slices"/ Sine/Cosine Frequency====</span></span>
<span id="cb45-7">theta <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;">seq</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;">40</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pi, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) </span>
<span id="cb45-8"></span>
<span id="cb45-9"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Number of divisions/rows in the data wanted====</span></span>
<span id="cb45-10">n <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;">500</span></span>
<span id="cb45-11"></span>
<span id="cb45-12"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## "Radial" setting of the "circle" to create "n" different marks====</span></span>
<span id="cb45-13">r <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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>n</span></code></pre></div>
</div>
<p><br></p>
<p>Next, I’ll use one of my favorite color palettes that I found for one of my <a href="https://github.com/Meghansaha/TidyTuesday/blob/master/2021/9-14-21%20Billboard_100/final_plot.png">#TidyTuesday submissions</a>. I’ll store them in a vector simply called “colors”:</p>
<p><br></p>
<div class="cell">
<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"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">## Setting up the custom color palette====</span></span>
<span id="cb46-2">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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#af3918"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a21152"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#822b75"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#612884"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#154baf"</span>,</span>
<span id="cb46-3">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0b82b9"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#277e9d"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#488e35"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#e3a934"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b2336a"</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>Now I’ll create a simple data frame. I’ll create my <code>x</code> and <code>y</code> variables using the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Trig"><code>cos()</code> and <code>sin()</code></a> functions respectively:</p>
<p><br></p>
<div class="cell">
<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;"># Placing everything into a dataset====</span></span>
<span id="cb47-2">data <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;">tibble</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cos</span>(theta)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r,</span>
<span id="cb47-3">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sin</span>(theta)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>r)</span></code></pre></div>
</div>
<p><br></p>
<p>Finally, we’ll put everything together. Using <code>geom_path()</code> on this dataset and passing our colors vector into it creates our Rainbow Rose!</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Pulling it all together====</span></span>
<span id="cb48-2">Rainbow_Rose <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="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> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y, <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></span>
<span id="cb48-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(colors, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> n<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 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="cb48-5">  <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="cb48-6">  <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>
<span id="cb48-7">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">panel.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;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>))</span>
<span id="cb48-8"></span>
<span id="cb48-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View it #</span></span>
<span id="cb48-10">Rainbow_Rose</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip35-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Rainbow Rose - Finished</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Mesmerizing! Play around and investigate the options used to try and understand how/why ggplot generated the data this way.</p>
<p>For example, if we change <code>geom_path()</code> to <code>geom_line()</code> we get this:</p>
<p><br></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://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid_files/figure-html/codesnip36-1.png" class="img-fluid figure-img" width="672"></p>
<figcaption>Rainbow Rose with geom_line() instead of geom_path()</figcaption>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Still pretty, but a completely different piece! Earlier in the article I mentioned the difference between <code>geom_path()</code> and <code>geom_line()</code> . Given their differences, does it make sense why this version looks this way? These are just some of the questions you can think about and ask yourself while you’re on your Rtistry journey!</p>
<p><br></p>
</section>
<section id="theres-a-package-for-that" class="level2">
<h2 class="anchored" data-anchor-id="theres-a-package-for-that">There’s a Package for That</h2>
<p>If you read all of this and still feel intimidated, that’s OK. You can still try to get into Rtistry with some of the following packages: <br></p>
<ul>
<li><p><a href="https://github.com/koenderks/aRtsy">aRtsy</a> developed by <a href="https://github.com/koenderks">Koen Derks</a> <em>“Aims to make generative aRt accessible to the general public in a straightforward and standardized manner”</em></p></li>
<li><p><a href="https://ambient.data-imaginist.com/">Ambient</a> developed by <a href="https://github.com/thomasp85">Thomas Lin Pedersen</a> and <a href="https://github.com/Auburn">Jordan Peck(FastNoise)</a> <em>“…is a an R package that provides access to the FastNoise C++ library for generating noise.”</em></p></li>
<li><p><a href="https://cran.r-project.org/web/packages/ggvoronoi/vignettes/ggvoronoi.html">Ggvoroni</a> developed by <a href="https://github.com/garretrc">Robert Garrett</a> and Thomas Fisher <em>“With ggvoronoi we can easily draw Voronoi diagram heatmaps, with help from packages deldir and ggplot2.”</em></p></li>
<li><p><a href="https://github.com/Ijeamakaanyene/contouR">contouR</a> developed by <a href="https://github.com/Ijeamakaanyene">Ijeamaka Anyene</a> <em>“contouR is a package that is a wraparound for ggplot2::geom_contour() to use for generative aRt .”</em></p></li>
<li><p><a href="https://github.com/djnavarro/jasmines">Jasmines</a> developed by <a href="https://github.com/djnavarro">Danielle Navarro</a> <em>“The jasmines package is what Danielle uses to make a lot of her generative artwork with R. Internally it relies heavily on the ambient package, and you may need to install the developer version of ambient to make it work.”</em></p></li>
</ul>
<p><br></p>
</section>
<section id="final-remarks" class="level2">
<h2 class="anchored" data-anchor-id="final-remarks">Final Remarks</h2>
<p>This concludes The Tidy Trekker Intro to Data Art post! I really hope that those of you who are just trying to start out in Rtistry found this to be useful. While the basics content truly is basic, it really is a good chunk of the foundation needed to eventually progress into more complex generative aRt . Sometimes it can be difficult to disseminate the how-tos or guides for a creative process. As you saw with the Rainbow Rose piece, sometimes aRt can be created by accident. Those accidents can only be unique to you. The best way to improve your Rtistry skills is to just jump in and play. Use the time to zone out, relax, and engage with data in a way you may not have before.</p>
<p><em>Have you started your Rtistry journey? Do you think some crucial basics were left out of this post? Feel free to contact me directly! Respectful discourse is always welcome!</em></p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section>

 ]]></description>
  <category>r</category>
  <category>data art</category>
  <category>rtistry</category>
  <category>ggplot</category>
  <category>beginner</category>
  <guid>https://www.thetidytrekker.com/post/thinking-outside-the-grid/thinking-outside-the-grid.html</guid>
  <pubDate>Tue, 19 Oct 2021 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/thinking-outside-the-grid/images/rainbowrose.png" medium="image" type="image/png" height="125" width="144"/>
</item>
<item>
  <title>Where Have I Been?</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/where-have-i-been/where-have-i-been.html</link>
  <description><![CDATA[ 






<p>So, it has been a good five months since I last made a post on The Tidy Trekker. I can assure you that I am alive and kicking! Though I know I do not have any obligations or posting schedules, I would prefer not to go that long without posting anything at all. I know it’ll happen sometimes, but I love adding content here and interacting with others about it over on Twitter (you can <a href="https://twitter.com/meghansharris">follow me</a> there if you haven’t already, it’s a good time! Lots of data, lots of memes, and lots of R stuff).</p>
<p>I just wanted to make a more low-key, short, and personal post to take a moment, stop, and say hello to any readers out there. How are you guys? How is the pandemic treating you on your end of the universe? Well, for me, it’s been an interesting challenge, and by “interesting” I mean kind of sucky.</p>
<center>
<br>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/where-have-i-been/images/lifesucks.png" alt="life sucks" style="width:100%" class="figure-img">
<figcaption>
<center>
<i>Life sucks sometimes. It’s OK to say it.</i>
</center>
</figcaption>
</figure>
<p><br></p>
</center>
<p>I had a lot of challenges, setbacks, and downright painful things thrown at me this year. These things threw me off track for a bit. While I’m not ready to talk about everything, I can touch on a few things. My last Tidy Trekker post was in April. Around this time, I was finishing up my TarotreadR app for the Shiny contest this year. I actually just wrote up a post about that experience as it was the first Shiny app I ever made. You can read more about that <a href="https://www.thetidytrekker.com/post/making-my-first-shiny-app">here</a> if you’d like.</p>
<p>During all of this, I was also hospitalized for a bit but then was able to go to Rochester (NY) for my birthday when I got out. When I got back, my efforts moved towards wrapping up some projects at work as I was preparing to leave the country in August for my wedding anniversary. While I was doing that, I finally landed a second job as a contracted Data Analyst (finally, some good stuff)!</p>
<p>I eventually got through work, and a whole bunch of other responsibilities, to finally receive my reward: an anniversary trip to Mexico. When I stepped foot onto the resort, I nearly wept. I was so grateful and privileged to be able to just escape everything for a bit given the year I’d been having.</p>
<center>
<br>
<figure class="figure">
<center>
<img src="https://www.thetidytrekker.com/post/where-have-i-been/images/gift.png" alt="A gift - The book Advanced R by Hadley Wickham" style="width:60%" class="figure-img">
</center>
<figcaption>
<center>
<i>One of the gifts my husband gave me in Mexico.</i>
</center>
</figcaption>
</figure>
<p><br></p>
</center>
<p>But now vacation is over and I’m back in reality. Having the time away was really important for me to think about the next steps I’d like to consider for my career. Currently, I’m still in love with my position and have been learning so much and am so grateful for it. Moving forward, I’d like to focus more on a few things:</p>
<p><br></p>
<ul>
<li>Strengthening my knowledge of statistical methodology</li>
<li>Working on improving the efficiency of my programming in general</li>
<li>Getting familiar with Python and SQL</li>
<li>Trying to complete a tiny ML project before the year is up</li>
</ul>
<p><br></p>
<p>I plan to use The Tidy Trekker to document these endeavors, but given that I am starting a contracted position, I will have to play everything by ear of course.</p>
<p><em>How has your 2021 been so far? How’s life? Got any updates you want to share? Feel free to contact me directly! Respectful discourse is always welcomed!</em></p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>



 ]]></description>
  <category>personal</category>
  <category>intentions</category>
  <category>life</category>
  <category>r</category>
  <category>machine learning</category>
  <category>python</category>
  <category>sql</category>
  <category>2021</category>
  <guid>https://www.thetidytrekker.com/post/where-have-i-been/where-have-i-been.html</guid>
  <pubDate>Tue, 07 Sep 2021 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/where-have-i-been/images/clock.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Making My First Shiny App</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/making-my-first-shiny-app/making-my-first-shiny-app.html</link>
  <description><![CDATA[ 






<p>This year was the first time that I ever competed in a RStudio Shiny competition by submitting my first ever Shiny app, the <a href="https://meghansaha.shinyapps.io/TarotreadR/">TarotreadR</a>. For those of you that read one of my previous blog posts <a href="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r">“How I Became a ‘Not-Beginner’ in R”</a>, you’ll know that I tried to make Shiny apps first before doing anything else in R and RStudio. This was before I tried learning basic programming in, or even learning how to load data into, the RStudio environment. I can credit Shiny with being the reason why I almost gave up on learning R and almost did not make a transition into the field of data science.</p>
<p><br></p>
<center>
<strong>Don’t have time to read and just want the source code? <a href="https://github.com/Meghansaha/TarotreadR">Click Here</a></strong>
</center>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/appmeme.jpg" alt="One does not simply build an app (meme)">
<figcaption>
I don’t know what I was thinking.
</figcaption>
</center>
<p><br></p>
<p>Now fast forward about 2-3 years to present day; I can comfortably say that I still know so little about Shiny. In March of this year, RStudio’s 3rd Annual <a href="https://blog.rstudio.com/2021/03/11/time-to-shiny/">Shiny Competition</a> was first announced and, for some reason, I felt compelled to face my fears and <a href="https://community.rstudio.com/t/tarotreadr-shiny-contest-submission/103681">enter the competition</a>. The thought of competing was honestly terrifying and daunting because I had never worked in Shiny— well, besides my first failed attempt over 2 years ago. Just like in the past with my machine learning experiences, every time I tried to get started with reading materials about creating my own app in Shiny, I would just freeze up mentally. Everything always seemed overwhelming, and I could never understand how people could create such amazing and intricate apps just using Shiny. Despite all of this, I somehow managed to pull together a working app. The process involved a lot of resources, a lot of time, some tears, and some sleepless nights. Although I got no honorable mentions or placements in the competition, I have to say that it is something I am extremely proud of. Plus, I also got my first hex stickers for free so I can’t complain at all!</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/stickers.jpg" alt="An RStudio postcard and 15 RStudio hex stickers are aligned on a table." width="80%">
<figcaption>
Things I received for the competition. Totally made up for the sadness of not placing!
</figcaption>
</center>
<p><br></p>
<p>A few months ago, I could never have imagined that I would be able to create a <strong>working</strong> app that I made by myself in Shiny. The TarotreadR isn’t the best app and might not be the most creative, but it is something that is a huge accomplishment for me. Especially so because it was made in about three months with no prior Shiny experience. I wanted to take the time to talk about this process of creating the TarotreadR and what I learned from the experience, so let’s dive in!</p>
<p><br></p>
<section id="the-idea" class="level2">
<h2 class="anchored" data-anchor-id="the-idea">The Idea</h2>
<center>
<a href="https://www.muddycolors.com/2018/05/magic-for-artists-part-2-tarot-other-decks/"><img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/modernwitch.jpg" alt="An Image of the box cover for the Modern Witch Tarot Deck by Lisa Sterle and two tarot cards. The Fool and The High Priestess."></a>
<figcaption>
<a href="https://www.muddycolors.com/2018/05/magic-for-artists-part-2-tarot-other-decks/">The Modern Witch Tarot Deck by Lisa Sterle. Image credit: Lauren Panepinto | Muddycolors.com</a>
</figcaption>
</center>
<p><br></p>
<p>My TarotreadR idea was thought up before the Shiny competition this year. Recently, I’ve personally become interested in tarot cards (and was gifted <a href="https://www.lisasterle.com/mwt">The Modern Witch Tarot Deck by Lisa Sterle</a>.) This is not because I was planning my career transition into tarot reading, but because I grew fond of the different art styles that different decks can have. I find it so interesting to look at how artists’ interpretations of these decks vary. I must also admit that I do like pulling cards for myself from time to time. I find that some people get surprised when I tell them that. I’m viewed as this highly analytical person that works in data science and “real things.” For me, I liken reading cards to using a fidget spinner in some sense.</p>
<p><br></p>
<p>As a mental health advocate, I’ve always been open about my mental health struggles and I must say that sometimes pulling tarot cards prompts me to use the <a href="https://en.wikipedia.org/wiki/Barnum_effect">Barnum Effect</a> to my advantage by creating some distance from my anxious thoughts. This gives me the space to acknowledge what I already know within me. Being that I started doing this more frequently, I wanted an app that could spell out full interpretations of groups of tarot cards based on what was pulled. Interpreting tarot cards can get messy and confusing if you start doing more than one, and if you allow them to be reversed. It can get complicated because a reversed tarot card can have subtle but key differences in interpretations when compared to upright cards. So, given that I was already interested in this and wanted to do it, the idea of a simpler app, the TarotreadR, was born.</p>
<p><br></p>
<p>Instead of making fully tailored interpretations, the app would randomly pull cards for the user and give them keywords for each card based on if the card was upright or reversed. To get started with this, I wanted to do the tasks that I thought would take the longest: making the card images and creating tarot card data.</p>
<p><br></p>
</section>
<section id="making-the-card-art-and-data" class="level2">
<h2 class="anchored" data-anchor-id="making-the-card-art-and-data">Making the Card Art and Data</h2>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/lovers.gif" alt="An animated image of a collage forming to create The Lovers tarot card." width="80%">
<figcaption>
One of my favorite cards that I made. This gif shows all the pieces I had to individually find to create the visual for this card.
</figcaption>
</center>
<p><br></p>
<p>The process of making the card art was a bit daunting but something I really wanted to do. The original tarot deck consists of 78 cards in total. Making the card art did take about 2 months over the course of a few sessions. It was the perfect time to get some use out of my Pro <a href="https://www.canva.com/">Canva</a> account. I created each image by “collaging” various icons, graphics, and shapes together. This was a tedious process as it required thinking outside of the box to get the shapes and figures I wanted. The hardest part was trying to keep the figures (human silhouettes) consistent. Let’s just say I like some cards more than others.</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/towerkop.png" alt="Two tarot cards. The Tower and the King of Pentacles.">
<figcaption>
One of my best cards and one of my worst. Note the inconsistency in the style and design of the human silhouettes.
</figcaption>
</center>
<p><br></p>
<p>While I was making the card art, I was also creating a dataset to go with it. For an app like this, the dataset was straightforward. I would just need three variables: The card’s name, the associated keywords (card meanings), and the position (upright or reversed/upside down) of the card. I used a combination of published meanings from <a href="https://www.biddytarot.com/tarot-card-meanings/">biddytarot.com</a> and <a href="https://labyrinthos.co/blogs/tarot-card-meanings-list">labyrinthos.co</a> to fill in the keywords of each card. This process also took about a few months as I only filled in the data once I completed the art for the card.</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/tarotdataset.png" alt="A screen capture of the tarot dataset on my github account. It has three variables: 'Card', 'Keywords,' and 'Reversed' which is the card position. The keywords are written in plaintext HTML.">
<figcaption>
The finished tarot card dataset in the TarotreadR Github Repository
</figcaption>
</center>
<p><br></p>
<p>Creating the dataset was an iterative process. While I was working on the cards and data, I did start to think about the app structure. Trial and error eventually lead to me figuring out that the text in the “keywords” variable could be in plaintext HTML. This required me fixing the data to follow plaintext HTML and to then introduce line breaks in the UI of the app when keywords were displayed.</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/threeofcups.png" alt="A combined screenshot of the Three of Cups in the dataset and the Three of Cups in the app. The card shows a picture of three hands holding cups together.">
<figcaption>
The Three of Cups card displayed in the TarotreadR app and a snip of the dataset. Plaintext HTML was used to format the keywords.
</figcaption>
</center>
<p><br></p>
<p>After I had a good foundation going with the cards and dataset, I had to finally face my fears and start trying to build the app in Shiny. I thought the best way to do this would be to try to wrap my head around the Shiny environment. I would be lying if I said I wasn’t terrified.</p>
<p><br></p>
</section>
<section id="understanding-the-shiny-environment-and-reactivity" class="level2">
<h2 class="anchored" data-anchor-id="understanding-the-shiny-environment-and-reactivity">Understanding the Shiny Environment and Reactivity</h2>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/scared.gif" alt="Jake the Dog from Adventure Time, cowering in fear on the ground and hiding his eyes.">
<figcaption>
Me opening a new default Shiny app script in R.
</figcaption>
</center>
<p><br></p>
<p><strong>Spoiler Alert:</strong> this part was actually not that bad! The worst is designing your app to make it look pretty and I’ll get to that nightmare shortly. If you’ve read one of my previous R walkthroughs, <a href="https://www.thetidytrekker.com/post/dull-dashboards">Making Dull Dashboards</a>, you may recall me talking about the concept of reactivity when creating <a href="https://pkgs.rstudio.com/flexdashboard/">flexdashboards</a>. The concepts I learned using the <a href="https://rstudio.github.io/crosstalk/">Crosstalk</a> and <a href="https://kent37.github.io/summarywidget/using.html">SummaryWidgets</a> packages to make flexdashboards prepared me to comprehend how the Shiny environment works. While I am still a Shiny noob in my mind, it’s definitely not as scary when you have a basic understanding of how the two major components of a Shiny app work together.</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/shinystructure.png" alt="A picture of a car diagram with a basic interior and an engine. The structure of a Shiny app is like a car... The UI is everything the user need to control the car and feel comfortable while using it. The Server is everything 'under the hood' of the car that's required to make it run.">
<figcaption>
A basic thought process that helped me understand the Shiny environment.
</figcaption>
</center>
<p><br></p>
<p>I think of a Shiny app as if it were a car made of two main components: the Server and the UI (User Interface). I think of the Server as everything under the hood of the car: the engine, fuel tank, electrical system, etc. I think of the UI as the frame of the car, the seats, the steering wheel, and anything else I would need to comfortably control the car.</p>
<p>I think of it this way: there’s a bare minimum that’s required for a car to turn on and operate. This is like having functions in your Server that, at its bare minimum, will allow the app to operate regardless of efficiency or the warnings that we might ignore in the console while our app is deploying. At its bare minimum, the UI should similarly be able to give the user basic functionality. In our car comparison this could be something as simple as making sure the car has a working ignition and a steering wheel. In the same sense, while our cars can operate with these bare minimums, we’d prefer to not see a check engine light, we’d prefer seat belts, and maybe some nice leather seats to sit on.</p>
<p>Once I started thinking about Shiny apps this way, I realized the importance of the interaction between the UI and the Server. I’m very excited to build from this knowledge to make better apps in the future, but for now, this basic understanding was all I needed to make the TarotreadR happen.</p>
<p><br></p>
</section>
<section id="designing-the-ui" class="level2">
<h2 class="anchored" data-anchor-id="designing-the-ui">Designing the UI</h2>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/UIcodesnip.png" alt="A screenshot of some of the code from the TarotreadR's UI.">
<figcaption>
A piece of the UI code used to make the TarotreadR. This code creates a one card draw.
</figcaption>
</center>
<p><br></p>
<p>Creating the UI was also an iterative process. It seemed I was constantly going back and forth between the UI and Server through trial and error. I expected this as I was essentially teaching myself as I progressed. One of the biggest challenges was learning that it’s REALLY helpful to have some knowledge of HTML, CSS, and even JavaScript when designing a Shiny app’s UI. While it’s not required to make an app functional, it is required to make an app pretty. I had a vision of what I wanted the app to look like, and to get that vision, I definitely had to spend some nights refreshing my HTML skills and learning new skills in CSS and JavaScript. These backgrounds were needed to figure out small things like not having cards appear until a user has clicked on an action button and having the cards animate onto the screen when a user “pulled” them.</p>
<p>Another challenge was figuring out spacing and sizing. It required thinking about the app and its components in a modular fashion. Boxes within boxes, if you will. Again, trial and error was my best friend here. The code you see above is a “Frankenstein” masterpiece that came to be after many iterations and failed deployments.</p>
<p><br></p>
</section>
<section id="setting-up-the-server" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-the-server">Setting Up the Server</h2>
<p>Besides trying to teach myself CSS and minimal JavaScript in less than a month, one of the most difficult things about building the TarotreadR was figuring out the Server component. I needed to figure out how to randomize cards being pulled, but I also needed to get images to show up in the UI based on those randomizations. Things get even more difficult once you add in the reversed card positions.</p>
<p>My original thought was to only make the deck of cards one time and then figure out how to get the images to display in a reversed position programmatically. In my darkest hour, I gave up trying to figure out the CSS to make this happen. Every time I thought I had the answer, the image would somehow be off and not displaying correctly. In that moment, I shed a tear, closed RStudio, and opened up Canva and proceeded to make “reversed” images for each of the cards. In retrospect, I really wish that I would have stuck it out as it might have improved the TarotreadR’s performance (instead of loading 78 card images, it was now loading 156 card images for upright and reversed positions).</p>
<p>Having separate images did end up working for me with the help of this <a href="https://ropensci.org/blog/2017/11/07/magick-knitr/">post</a> by <a href="https://twitter.com/opencpu">Jeroen Ooms</a>. Once I got over the biggest hurdle of getting the images to display properly, I was able to add things like toggling (so that cards are only shown after a button is clicked) and sounds that play with the card animations.</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/animationscodesnip.png" alt="A piece of the Server code from the TarotreadR app.">
<figcaption>
A snip of the animations and interactions coded for the TarotreadR. Most of these functions came from packages like ShinyAnimate.
</figcaption>
</center>
<p><br></p>
</section>
<section id="putting-it-all-together" class="level2">
<h2 class="anchored" data-anchor-id="putting-it-all-together">Putting It All Together</h2>
<p>Finally, after about three months, all of the work paid off! I finally finished all of the cards, completed all of the data entry, got the UI and Server to talk to each other, and successfully deployed the TarotreadR to Shinyapps.io. If you haven’t seen it action and would like to, you can <a href="https://meghansaha.shinyapps.io/TarotreadR/">here</a>.</p>
<p>If you’d like to see the full code and files used to make all of this, you can find it in my Github Repo <a href="https://github.com/Meghansaha/TarotreadR">here</a>.</p>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/tarotreadrsnip.png" alt="A screenshot of the finished TarotreadR app. The images shows three tarot cards. The Magician, The Hermit, and the Three of Cups.">
<figcaption>
The completed TarotreadR app. My first Shiny app!
</figcaption>
</center>
<p><br></p>
</section>
<section id="lessons-learned" class="level2">
<h2 class="anchored" data-anchor-id="lessons-learned">Lessons Learned</h2>
<p>After this whole experience, my professional and personal life got extremely hectic, and I didn’t get a chance to really talk about this publicly (which is what prompted this post). There are a few lessons I’ve learned that I want to highlight:</p>
<ul>
<li><p>CSS, HTML, and JavaScript are crucial to making attractive UIs (in my opinion).</p></li>
<li><p>Sometimes it’s OK if your code is not the most efficient. You can still get your app to work – but it is in good practice to try and revisit the code to improve it if possible.</p></li>
<li><p><a href="https://pkgs.rstudio.com/flexdashboard/">Flexdashboards</a>, <a href="https://rstudio.github.io/crosstalk/">Crosstalk</a>, and <a href="https://kent37.github.io/summarywidget/using.html">SummaryWidgets</a> are absolutely the reason why I was able to do all of this in three months. Learning these packages can ease you into understanding how app reactivity works.</p></li>
<li><p>I have even more respect for web devs and those that make Shiny apps for a living (I already had a lot of respect for you guys, now it’s just <strong>astronomical</strong>).</p></li>
<li><p>Shiny isn’t all that scary once you slow down and take the time to understand the environment.</p></li>
</ul>
<p><br></p>
<p><em>Have you ever made Shiny apps before? Do you have “Shinyphobia” like I did? Are you screaming at the inefficiency of my program? (I know, I’m sorry!) Feel free to contact me directly! Respectful discourse towards efficient solutions or new insights is always welcomed!</em></p>


</section>

 ]]></description>
  <category>shiny</category>
  <category>data science</category>
  <category>r programming</category>
  <category>r</category>
  <category>statistical programming</category>
  <category>html</category>
  <category>css</category>
  <category>javascript</category>
  <category>tarotreadr</category>
  <guid>https://www.thetidytrekker.com/post/making-my-first-shiny-app/making-my-first-shiny-app.html</guid>
  <pubDate>Sat, 04 Sep 2021 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/making-my-first-shiny-app/images/shinyapp.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Tallying “Checkbox” Survey Responses: R Walkthrough</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/tallying-checkbox-survey-responses-r-walkthrough.html</link>
  <description><![CDATA[ 






<p><strong>The Problem:</strong> You need to tally (add) checkbox survey responses, but they are combined and comma-separated together in one string.</p>
<p><strong>The Fix:</strong> You need to count the occurrences of each response within each observation’s string. You can do this in a few lines with tidyr and dplyr, or in a more involved way while practicing string manipulation with regex/grepl and stringr.</p>
<section id="the-example" class="level2">
<h2 class="anchored" data-anchor-id="the-example">The Example:</h2>
<p>Let’s say we have a simple survey question that asks participants to select colors that they like. We allow them to “Check all that apply” and give them eight choices to choose from. It might look like this:</p>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/snip1.png" alt="Example of a checkbox question type. The question reads 'what colors do you like?' with the answers red, blue, green, yellow, black, orange, brown, and pink" style="width:30%" class="figure-img">
<figcaption>
<center>
<i>“A snapshot of example checkbox survey questions”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
<p>Checkbox question-types allow participants to select multiple choices. This can be useful for building strong analyses. However, we may find that the “checkbox” question-type produces undesirable data that will have to be dealt with. Let’s explore this by setting up our R session.</p>
<p><em>If you’d like to follow along within the R project files you can download them from the <a href="http://github.com/Meghansaha/Tallying_Checkboxes/archive/master.zip">Tidy Trekker Github Repository</a>. Be sure to fully extract the ZIP. folders for proper access. You can also download the data set used and load it into your own session for practice.</em></p>
<center>
<a href="http://github.com/Meghansaha/Tallying_Checkboxes/archive/master.zip"><img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/GHsnip.png" alt="GitHub Download"></a>
<figcaption>
<i>Direct <a href="http://github.com/Meghansaha/Tallying_Checkboxes/archive/master.zip">download link</a> for this post’s example data</i>
</figcaption></center>


<p><br></p>
</section>
<section id="using-separate_rows-from-tidyr-and-count-from-dplyr" class="level2">
<h2 class="anchored" data-anchor-id="using-separate_rows-from-tidyr-and-count-from-dplyr">Using “separate_rows” from Tidyr and “count” from Dplyr:</h2>
<p>The simplest and fastest way to tally up these results is to use tidyr and dplyr together. We start by loading in the packages we need: <a href="https://readr.tidyverse.org/">readr</a>, <a href="https://dplyr.tidyverse.org/">dplyr</a>, and <a href="https://tidyr.tidyverse.org/">tidyr</a>. These allow us to get our data into the environment and prepare it for cleaning and manipulation. We’ll name the resulting data set “Colors.”</p>
<p><br></p>
<div class="cell">
<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>(readr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'dplyr'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:stats':

    filter, lag</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union</code></pre>
</div>
<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>(tidyr)</span>
<span id="cb5-2">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;">read_csv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Data/Colors.csv"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col_types =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cols</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;">What colors do you like?</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">col_character</span>()))</span></code></pre></div>
</div>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/careful.png" style="width:20%"><br> <em>Pay attention to the file path in the “read_csv” function. If you copy and paste that command and are not using the project files within the Github link above, you will get an error!</em>
</center>
<p><br></p>
<p>We can see our data set in the data viewer. Notice that each observation has a combined, comma-separated string for each response:</p>
<br>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/snip2.png" alt="snapshot of the example data in a dataframe. Most observations have multiple string values comma separated and stored into each row together" style="width:30%" class="figure-img">
<figcaption>
<center>
<i>“A snapshot of example checkbox survey question loaded into a dataframe”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
<p>We need to tell R to separate the values in the <code>What colors do you like?</code> column into individual rows and then count and tally those occurrences. Let’s do this and store it in a data set called <code>TidyrColors</code>. The code for this can be written in one chunk with the following:</p>
<p><br></p>
<div class="cell">
<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">TidyrColors <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Colors <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">separate_rows</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;">What colors do you like?</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;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</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;">What colors do you like?</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;">sort =</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;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tally"</span>)</span></code></pre></div>
</div>
<p>The <a href="https://tidyr.tidyverse.org/reference/separate_rows.html"><code>separate_rows()</code></a> function from tidyr separates all the values in the column while using “,” as the characters to separate on. Note that we have a whitespace after the comma in the <code>sep=</code> option. This is because whitespaces are present in the original data. If you had options that were only separated by commas and no spaces, you would just need a comma there.</p>
<p>Next, we used the <a href="https://dplyr.tidyverse.org/reference/count.html">count()</a> function from dplyr to count the occurrences of our colors. Setting the <code>sort=</code> option to TRUE will arrange the observations in descending order in the dataset. The <code>name=</code> option will allow us to set the column name that will hold the count of the values. In this case, we’ve set it to <code>Tally</code>.</p>
<br>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/snip3.png" alt="snapshot of the example data in a dataframe with the values separated and tallied together" style="width:30%" class="figure-img">
<figcaption>
<center>
<i>“A snapshot of example checkbox survey results separated and tallied together”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
<p>And that’s it! Pretty simple and straightforward. Continue on for more involved ways to do this that will give you some string/regex practice if interested!</p>
<p><br></p>
</section>
<section id="using-grepl-or-stringr-dplyr-and-tidyr" class="level2">
<h2 class="anchored" data-anchor-id="using-grepl-or-stringr-dplyr-and-tidyr">Using Grepl (or Stringr), Dplyr, and Tidyr:</h2>
<p>There are other ways we can do this that are a bit more involved, but can provide some grepl/regex and string manipulation practice if desired. If you choose to use Stringr instead of grepl, you can load in the <a href="https://stringr.tidyverse.org/index.html">stringr</a> package as well.</p>
<p><br></p>
<div class="cell">
<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;"># If using the stringr package===</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span></code></pre></div>
</div>
<p><br></p>
<p>In order to get a tally of the individual responses, we need to break up these strings and create variables for each color. We do this by creating a new column for each of our color options available for our survey question. Let’s create a reference vector of all of our color options to help us. We’ll name it <code>Colorref</code>:</p>
<p><br></p>
<div class="cell">
<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">Colorref <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;">"Red"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Blue"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Green"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Yellow"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Black"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Orange"</span>,</span>
<span id="cb8-2"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brown"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Pink"</span>)</span></code></pre></div>
</div>
<p><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/careful.png" style="width:20%"><br> <em>Pay attention to the strings you enter into your reference vector. These strings need to match your survey responses/options <strong>PERFECTLY</strong>. Be mindful of any discrepancies in <strong>capitalization, spelling, spaces, or punctuations</strong>.</em>
</center>
<p><br></p>
<p>Using the reference vector we just created, we can now create an empty data frame that we can populate with our color tallies. We’ll call it <code>Colorsnew</code>. We can use base functions in R to do this. To ensure the tallies are properly recorded in the new data frame, we’ll set the number of columns to match the number of options (Colors) we have and the number of rows to match the number of observations (or rows) we have in the original data set. Afterwards, we replace the column names with the name of each color choice. This can be done with the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/names">names</a> function.</p>
<p><br></p>
<div class="cell">
<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">Colorsnew <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;">as.data.frame</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Colorref), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(Colors)))</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Colorsnew) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Colorref</span></code></pre></div>
</div>
<p><br></p>
<p>The next step is to simultaneously populate all the columns in the data set with an accurate tally for each response. We can do this with the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/ifelse">ifelse</a> and <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/grep">grepl</a> functions within one single “for-loop.” This will populate our currently empty <code>Colorsnew</code> data set so we can easily see our changes. The for-loop function will run iterations along the length of the <code>Colorref</code> vector and use the vector’s values to fill in the column names of our data. At the same time we’ll be getting getting a count of how often each color is present in the original <code>Colors</code> dataset with the <code>grepl</code> function. Detailed information about for-loops can be found in the Iteration chapter in the free textbook <a href="https://r4ds.had.co.nz/index.html">R for Data Science</a> (<em>written by <a href="https://github.com/hadley">Hadley Wickham</a> and <a href="https://github.com/garrettgman">Garrett Grolemund</a></em>) :</p>
<p><br></p>
<div class="cell">
<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="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(Colorref)){</span>
<span id="cb10-2">  Colorsnew[i] <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;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(Colorref[i],Colors<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;">What colors do you like?</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">0</span>)}</span></code></pre></div>
</div>
<p><br></p>
<p>To reiterate, this code uses the <code>i</code> variable as a placeholder to rename the columns within the <code>Colorsnew</code> data set with the strings found in <code>Colorref</code> (All of our color options.) For each column, we are using an <code>ifelse()</code> test to scan the original strings in the <code>What colors do you like</code> column from the <code>Colors</code> data set (Colors$<code>What colors do you like?</code>). Simultaneously, we check for matches using the <code>grepl</code> function. If you aren’t a fan of using grepl, you can also opt for Stringr’s <a href="https://stringr.tidyverse.org/reference/str_detect.html"><code>str_detect()</code></a> function that performs the same action.</p>
<p><br></p>
<div class="cell">
<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="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(Colorref)){</span>
<span id="cb11-2">  Colorsnew[i] <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;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(Colors<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;">What colors do you like?</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>,Colorref[i]),<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;">0</span>)}</span></code></pre></div>
</div>
<p>Whether you use grepl or stringr, the result will be the same. Each match will produce a “1” while non-matches produce a “0.” The result is a matrix of observations for all of our color choices:</p>
<br>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/snip4.png" alt="snapshot of the example data in a dataframe with the values separated" style="width:90%" class="figure-img">
<figcaption>
<center>
<i>“A snapshot of example checkbox survey results separated”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
<p>Our last step will be to transpose this wide data into a long data format for easier graphing and analyses. (You can check out a tutorial on wide and long data in R from DataCamp <a href="https://www.datacamp.com/community/tutorials/long-wide-data-R">here</a>.) We do this using the <a href="https://tidyr.tidyverse.org/reference/pivot_longer.html">pivot_longer</a>, <a href="https://dplyr.tidyverse.org/reference/group_by.html">group_by</a>, and <a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise</a> functions from the dplyr and tidyr packages. In our pivot_longer function, we tell R we want to transpose all of the columns in the “Colorsnew” dataset with dplyr’s <a href="https://dplyr.tidyverse.org/reference/select.html?q=everything">everything()</a> option. We tell R that we want all of our current column names to be put into one variable called <code>Color</code> and that we want the values in these columns to be put into its own column called <code>Tally</code>. Finally, we want to group the data set observations by the “Color” column and add all of the tallies up so we get a sum for each value within the <code>Tally</code> column. This will produce a data frame with a complete tabulation of all the color choices that were picked in our survey.</p>
<p><br></p>
<div class="cell">
<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">Colorsnew <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Colorsnew <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;">pivot_longer</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>(),</span>
<span id="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Color"</span>, </span>
<span id="cb12-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tally"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(Color) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb12-6">  <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;">Tally =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(Tally))</span></code></pre></div>
</div>
<br>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/snip5.png" alt="snapshot of the example data in a dataframe with the values separated and tallied together" style="width:30%" class="figure-img">
<figcaption>
<center>
<i>“A snapshot of example checkbox survey results separated and tallied together”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
<p><em>Have any thoughts or suggestions? Know of a better solution or way to make this process more efficient? Feel free to contact me directly! Respectful discourse towards efficient solutions or new insights is always welcomed!</em></p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section>

 ]]></description>
  <category>data science</category>
  <category>survey processing</category>
  <category>data manipulation</category>
  <category>r</category>
  <category>statistical programming</category>
  <category>r programming</category>
  <category>data carperntry</category>
  <category>r walkthrough</category>
  <guid>https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/tallying-checkbox-survey-responses-r-walkthrough.html</guid>
  <pubDate>Thu, 18 Mar 2021 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/tallying-checkbox-survey-responses-r-walkthrough/images/Tallying Checkboxes.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Making Dull* Dashboards in R (* Without Shiny)</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/dull-dashboards/dull-dashboards.html</link>
  <description><![CDATA[ 






<p><strong><em>Don’t have time to read and just want the source code? <a href="https://github.com/Meghansaha/NYS_MH_Programs/blob/master/Dull_NYMHprogramDash.Rmd">Click Here</a></em></strong></p>
<p><br></p>
<p><strong>The Problem:</strong> <em>You want to put cleaned/prepared data into an interactive dashboard but you can’t or don’t want to code with Shiny at the moment.</em></p>
<p><strong>The Fix:</strong> <em>You need to use the flexdashboard package and create a markdown document instead.</em></p>
<p><br></p>
<section id="getting-started" class="level2">
<h2 class="anchored" data-anchor-id="getting-started">Getting Started:</h2>
<p>To start, we’ll look at a cleaned and prepped dataset that includes a mix of categorical and geographic variables. The data is publicly sourced from the New York State Department of Mental Health and contains information for <a href="https://data.ny.gov/Human-Services/Local-Mental-Health-Programs/6nvr-tbv8">New York State’s Local Mental Health Programs</a>.</p>
<p>For this example, I’ve already cleaned and prepped the set. If you’re interested, you can view the <a href="https://github.com/Meghansaha/NYS_MH_Programs/blob/master/Scripts/DataProcessing.R">cleaning</a> script and <a href="https://github.com/Meghansaha/NYS_MH_Programs/blob/master/Scripts/GeocodingProcessing.R">geoprocessing</a>* script. Our main purpose for this project is to create a dashboard with a map, filters for the map, and a data table that let’s us view selected data points.</p>
<p><em>*The geoprocessing coding in this script came from Dmitry Kisler’s article “OSM Nominatim with R: getting Location’s Geo-coordinates by its Address” Check it out to learn more about the process if you’d like!</em></p>
<p>If you’d like to follow along within the R project files you can download them from the <a href="https://github.com/Meghansaha/NYS_MH_Programs/archive/master.zip">Tidy Trekker Github Repository</a>. Be sure to fully extract the ZIP. folders for proper access. You can also download the data set used and load it into your own session for practice.</p>
<center>
<p><a href="https://github.com/Meghansaha/NYS_MH_Programs/archive/master.zip"><img src="https://www.thetidytrekker.com/post/dull-dashboards/images/GHsnip.png" alt="GitHub Download"></a></p>
<figcaption>
<p><i>Direct <a href="https://github.com/Meghansaha/NYS_MH_Programs/archive/master.zip">download link</a> for this post’s example data</i></p>
</figcaption></center>


<p><br></p>
</section>
<section id="creating-the-markdown-file" class="level2">
<h2 class="anchored" data-anchor-id="creating-the-markdown-file">Creating the Markdown File</h2>
<p>In order to create the dashboard, we need to write out code in an R Markdown file (.RMD) instead of an R Script (.R). If you don’t have the flexdashboard package installed already you’ll want to do that first:</p>
<div class="cell">
<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;"># Installing the flexdashboard package===</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;">"flexdashboard"</span>)                                                    </span></code></pre></div>
</div>
<p>After installing flexdashboard, you should be able to create a new markdown document with the flexdashboard template. You can do so by going to create a new markdown document like you normally would…</p>
<p><br></p>
<center>
<p><img src="https://www.thetidytrekker.com/post/dull-dashboards/images/snip1.png" alt="A screenshot of the R Studio IDE's menu to create a new document. 'R Markdown' is third on the list an highlighted with the alt text: 'Create a new R Markdown document' displayed."></p>
<figcaption>
<p>Creating a new markdown document in the R Studio IDE</p>
</figcaption>
</center>
<p><br></p>
<p>…but instead of accepting the default document settings, click on the “From Template” option in the left pane, select the “Flex Dashboard” template in the right pane, and finally click “OK” at the bottom:</p>
<p><br></p>
<center>
<p><img src="https://www.thetidytrekker.com/post/dull-dashboards/images/snip2.png" alt="A screenshot of the New R Markdown Template menu in the R Studio IDE. The menu consists of two panes. One on the left and right. The left pane has four markdown options: 'Document', 'Presentation', 'Shiny', and 'From Template'. The 'From Template' option in highlighted and selected. The right pane has various Template options with 'Flex Dashboard' at the top which is selected and highlighted. The bottom of the screen shot has three buttons. In the right hand bottom corner, the button reads 'Create Empty Document.' In the bottom left hand corner are two buttons that read 'OK' and 'Cancel' from left to right."></p>
<figcaption>
<p>R Markdown Template Menu in R Studio IDE</p>
</figcaption>
</center>
<p><br></p>
<p>You’ll now see a pre-generated Flex Dashboard markdown script! You can use this for practice. If you have some time and are interested, play around with the syntax and get a feel for how changes to this template will affect the final knitted document. You can read up more about Flex Dashboard basics <a href="https://rmarkdown.rstudio.com/flexdashboard/">here</a>. If you want more information about different layout options you can find them <a href="https://rmarkdown.rstudio.com/flexdashboard/layouts.html">here</a>. For this guide, we’ll be using the “Chart Stack (fill)” layout.</p>
<p><br></p>
</section>
<section id="setting-the-yaml" class="level2">
<h2 class="anchored" data-anchor-id="setting-the-yaml">Setting the YAML</h2>
<p>The first part of the markdown code that we see is the YAML (Yet Another Markup Language or YAML Ain’t Markup Language) Header. Without getting bogged down with an explanation, this is basically a special block of code that’s going to tell R how to process our markdown document. It’s the “magic battery” that’s needed to power the translation process that converts code to pretty documents. For this project our YAML looks like this:</p>
<div class="cell">
<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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb2-2">title<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;">"New York State's (Brooklyn,NY) Local Mental Health Programs"</span></span>
<span id="cb2-3">output<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-4">  flexdashboard<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>flex_dashboard<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span></span>
<span id="cb2-5">    vertical_layout<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> fill</span>
<span id="cb2-6">    source_code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> embed</span>
<span id="cb2-7">    css<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> scripts<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>nycmhstyle.css</span>
<span id="cb2-8"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div>
</div>
<p><br></p>
<p><strong>The Components of this YAML:</strong></p>
<ul>
<li><p><strong>Title:</strong> This is the string that will show up in the top left corner of your dashboard.</p></li>
<li><p><strong>Output:</strong> This is where the document type is set. You can set options for this on the same line, or underneath it (shown here.)</p></li>
<li><p><strong>Output Options:</strong> In this YAML, our options are underneath the “output” line and are indented. <font color="red"><strong>The indents are extremely important.</strong></font> If these options aren’t indented in a hierarchical (ordered) fashion, it will not work! Let’s look at them further:</p></li>
</ul>
<p><br></p>
<center>
<p><img src="https://www.thetidytrekker.com/post/dull-dashboards/images/careful.png" style="width:20%"><br> <strong><em>Please be mindful that the YAML output options are</em></strong> <font color="red">properly ordered and indented!</font> You’ll know if it’s not because you will get an error.</p>
</center>
<p><br></p>
<ul>
<li><p><strong>flexdashboard::flex_dashboard:</strong> Note how this is tabbed or indented only <strong>once</strong> under the output option. This tells R we want this document processed as a Flex Dashboard.</p></li>
<li><p><strong>vertical_layout: fill:</strong> Also note how it’s indented/tabbed again. This tells R that these are options that we want applied to this dashboard. This option tells R that we want the dashboard to fill the page vertically. This option can also be set to “scroll.” You can always play around with it and see which is best for your needs.</p></li>
<li><p><strong>source_code: embed:</strong> This option tells R that we want to share our source code on the dashboard. This will appear as a button in the top right corner of the dashboard. If we have source code elsewhere on the internet (like GitHub) you can replace “embed” with the actual URL as a string (with quotation marks - “<a href="https://github.com/user/code" class="uri">https://github.com/user/code</a>”)</p></li>
<li><p><strong>css:</strong> scripts/nycmhstyle.css: This option tells R that we want to use an external css file to change the appearance of our flexdashboard. This file is included on the <a href="https://github.com/Meghansaha/NYS_MH_Programs/blob/master/Scripts/nycmhstyle.css">GitHub repository</a> if you’re interested.</p></li>
</ul>
<p><br></p>
</section>
<section id="configuring-the-setup-code-chunk" class="level2">
<h2 class="anchored" data-anchor-id="configuring-the-setup-code-chunk">Configuring the “Setup” Code Chunk</h2>
<p>Now that the YAML is set, we need to load in all the packages we’ll use and our data.</p>
<p><br></p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Library load in.#</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(flexdashboard)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(knitr)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></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.0     ✔ stringr   1.5.2
✔ 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="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>(leaflet)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(leafem)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(crosstalk)</span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(DT)</span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(summarywidget)</span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(raster)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loading required package: sp

Attaching package: 'raster'

The following object is masked from 'package:dplyr':

    select</code></pre>
</div>
</div>
<p><br></p>
<p>There’s a good amount of packages here. I’ve already mentioned flexdashboard, leaflet, crosstalk, DT, and summary widgets, but there’s a few more needed for our particular example: - <a href="https://yihui.org/knitr/">knitr</a>: Helps with markdown/report generation - <a href="https://www.rdocumentation.org/packages/leafem/versions/0.1.3">leafem</a>: Gives extensions and additional options for the leaflet (map) package. - <a href="https://www.tidyverse.org/">tidyverse</a>: Group of packages used to aid with data wrangling/manipulation - <a href="https://www.rdocumentation.org/packages/raster/versions/3.4-5">raster</a>: Helps with geographical data analysis and modelling.</p>
<p><br></p>
<p>After loading in all of the libraries, we can now load in our data. The data for this project is stored as an .RDS object. The original csv file was converted to an .RDS object to ensure reproducibility and maintain integrity of the data’s structure. The <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/readRDS"><code>readRDS()</code> function</a> will automatically open the object and restore it as a data frame with all original components intact. This code stores the data frame as <code>MHprograms</code></p>
<p><br></p>
<div class="cell">
<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;">#Loading the datasets into the environment.#</span></span>
<span id="cb7-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#MH programs with geocodes#</span></span>
<span id="cb7-3"></span>
<span id="cb7-4">MHprograms <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;">readRDS</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data/mhprogramsnyc.RDS"</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>Now that our data is in the environment, we can use the crosstalk package to translate our data into a shared data object. This will allow interactivity between our dashboard components.</p>
<div class="cell">
<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;">#Converting data frames into shared data objects with crosstalk. This will allow the map, filters, and tables to be interactive and dynamic.#</span></span>
<span id="cb8-2">Sharedgeodata <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> SharedData<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;">new</span>(MHprograms)</span></code></pre></div>
</div>
<p><br></p>
</section>
<section id="thinking-about-dashboard-components-and-real-estate" class="level2">
<h2 class="anchored" data-anchor-id="thinking-about-dashboard-components-and-real-estate">Thinking about Dashboard Components and Real Estate</h2>
<p>When creating our dashboard we must think about the type of data we have and how that data will fill the space on our dashboard. Before thinking about where things will go, we need to think about what would be useful to have on our dashboard. There’s 17 variables in the set, but for the sake of this example let’s focus on the following:</p>
<center>
<p><br> Agency Name<br> Program Name<br> Agency Phone<br> Program Address 1<br> Program State<br> Program Zip<br> Program Category<br> Program Subcategory<br> Populations Served<br></p>
</center>
<p><br></p>
<p>These variables should be just enough to give the user basic description information about the programs in the data set. Keeping this in mind, we might now start to think about what would be useful to add to our dashboard and map. How about the following?</p>
<p><br></p>
<p><strong><em>On the map itself:</em></strong></p>
<ul>
<li><p><strong>A “Home Button”</strong> - A button that will snap our map view back to our data if we go wondering off in the map.</p></li>
<li><p><strong>Conditional Formatting</strong> - for map icons based on the program category (i.e.&nbsp;different icons based on the program type)</p></li>
</ul>
<p><strong><em>On the dashboard, separate of the map:</em></strong></p>
<ul>
<li><p><strong>Reactive Values</strong> - Values that change based on the data that’s shown on the map. For this example we’ll do a simple “count” of all mental health programs that were found.</p></li>
<li><p><strong>A Simple Data Table</strong> - A simple table that can display information about all programs shown on the map.</p></li>
<li><p><strong>Filters</strong> - Filters that will limit what locations are seen on the map based on options that the user selects.</p></li>
</ul>
<p><br> When you start to work with flexdashboard more, you’ll learn about the quirks of <a href="https://rmarkdown.rstudio.com/flexdashboard/layouts.html">layout templates</a> and will begin to see what’s possible. It’s recommended to take time to understand how you want your components to work and look together. For some people this may mean taking the time to draw on paper or making a “mock” dashboard in <a href="https://en.wikipedia.org/wiki/Microsoft_Paint">MS Paint</a>. Whatever your method of planning, just make sure you have a plan! Here’s our mock plan drawn up for our dashboard:</p>
<p><br></p>
<center>
<p><img src="https://www.thetidytrekker.com/post/dull-dashboards/images/snip3.png" style="width:80%"><br></p>
<figcaption>
<p>Mock-up Plan for our NY Mental Health Dashboard</p>
</figcaption>
</center>
<p><br></p>
</section>
<section id="setting-up-our-first-column-and-map" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-our-first-column-and-map">Setting up our First Column and Map</h2>
<p>So looking at our mock-up, we just need two columns. One on the left with just a map and one of the right with three modules and some text at the bottom. To set flexdashboard columns in markdown you need to put the following “outside” of r code chunks in the regular markdown editor:</p>
<div class="cell">
<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">Column {data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>width<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">550</span>}</span>
<span id="cb9-2"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-----------------------------------------------------------------------</span></span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### </span></span></code></pre></div>
</div>
<p>Because we want the first column to be filled with our map, we’ll set the width to be a bit wider than the second column. The series of dashes (—) under the “column” option along with the three hashes (#) let’s R know to place everything underneath those hashes in it’s own column within it’s own box. The hashes are also where you can place the title of your box within your column. This one will be left blank to maximize the space for the map.</p>
<p>Now that the column is initialized, we can place more r code chunks underneath it to get our data in. Our first chunk will be for our map. Recall that we designate the beginning of R code chunks with three back-ticks (aka: <a href="https://en.wikipedia.org/wiki/Grave_accent">Grave Accents</a>) and square brackets with options and end them with an additional three back-ticks at the bottom of the chunk:</p>
<pre><code>```{r map}

```</code></pre>
<p><br></p>
</section>
<section id="creating-the-home-button-bounding-box-and-map-center" class="level2">
<h2 class="anchored" data-anchor-id="creating-the-home-button-bounding-box-and-map-center">Creating the Home Button, Bounding Box, and Map Center</h2>
<p>Let’s recall what we wanted on our map. A home button and conditional formatting for program types. Let’s code for our home button to start. Say we want an actual image of a home. We’ve gone ahead and found a <a href="https://i.ibb.co/D8tSw9q/Homebutton.png">url of one</a>. To create this button, we’ll need to use the <a href="https://www.rdocumentation.org/packages/htmltools/versions/0.5.1.1/topics/HTML"><code>HTML</code> function</a> in the <a href="https://www.rdocumentation.org/packages/htmltools/versions/0.5.1.1"><code>htmltools</code> package</a> to use some HTML for this:</p>
<p><br></p>
<div class="cell">
<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;">#Pulling a "Home" icon I've hosted on the internet===</span></span>
<span id="cb11-2">Homeimg <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> htmltools<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;">HTML</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;">"&lt;img src='https://i.ibb.co/D8tSw9q/Homebutton.png' width = '15', height '15', title = 'Home'&gt;"</span>))</span>
<span id="cb11-3"></span>
<span id="cb11-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Naming the home image for the addhomebutton function in the leaflet map===</span></span>
<span id="cb11-5">Home <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Home"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> Homeimg)</span></code></pre></div>
</div>
<p><br></p>
<p>This code will show us a pretty home icon, but we’ll need to feed the button the <a href="https://wiki.openstreetmap.org/wiki/Bounding_Box">bounding box</a> (bbox) so that it knows where to set the map’s view when it’s clicked on. We can do this easily with the <a href="https://www.rdocumentation.org/packages/ggmap/versions/3.0.0/topics/make_bbox"><code>make_bbox</code> function</a> in the <a href="https://github.com/dkahle/ggmap"><code>ggmap</code> package</a> as long as our data has longitude and latitude columns with numeric values (Ours does!)</p>
<p><br></p>
<div class="cell">
<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;">#Generating a bounding box of the entire data set. This will act as our "homebase" to zoom back to when the home button is clicked on===</span></span>
<span id="cb12-2">homebase <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggmap<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;">make_bbox</span>(lon, lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> MHprograms)</span></code></pre></div>
</div>
<p><br></p>
<p>We may also want to designate a “center” for our map that is first shown when the dashboard is open. We can do this by using the <a href="https://www.rdocumentation.org/packages/sp/versions/1.4-5/topics/coordinates"><code>coordinates</code></a> and <a href="https://rdrr.io/cran/sp/man/is.projected.html"><code>proj4strings</code></a> functions from the <a href="https://www.rdocumentation.org/packages/sp/versions/1.4-5"><code>sp</code> package</a>, The <a href="https://www.rdocumentation.org/packages/raster/versions/3.4-5/topics/extent"><code>extent</code></a> function from the <a href="https://www.rdocumentation.org/packages/raster/versions/3.4-5"><code>raster</code> package</a>, and the <a href="https://www.rdocumentation.org/packages/methods/versions/3.6.2/topics/as"><code>as</code></a> function that comes from the <a href="https://www.rdocumentation.org/packages/methods/versions/3.6.2"><code>methods</code></a> package that usually loads in with base R.</p>
<p><br></p>
<div class="cell">
<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;"># Retrieving the center point for the map data to set the "home" view===</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coordinates</span>(MHprograms) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">~</span>lon<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span>lat</span>
<span id="cb13-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">proj4string</span>(MHprograms) <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;">"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"</span></span>
<span id="cb13-4">mapcenter <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;">coordinates</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">extent</span>(MHprograms), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SpatialPolygons"</span>))</span></code></pre></div>
</div>
<p><br></p>
<p>The options set in the <code>proj4string</code> line is referring to the Coordinate Reference System that R uses to create our maps. You can learn more about it with some reading material provided by the <a href="https://www.nceas.ucsb.edu/sites/default/files/2020-04/OverviewCoordinateReferenceSystems.pdf">National Center for Ecological Analysis and Synthesis</a> if you’re interested.</p>
<p><br></p>
</section>
<section id="creating-the-actual-map" class="level2">
<h2 class="anchored" data-anchor-id="creating-the-actual-map">Creating the Actual Map</h2>
<p>Now we can finally get our map into the column. The code is as follows:</p>
<div class="cell">
<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;">#Creating the leaflet map from the shared geo data object===</span></span>
<span id="cb14-2">Sharedgeodata <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">leaflet</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addProviderTiles</span>(providers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>OpenStreetMap) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addAwesomeMarkers</span>(</span>
<span id="cb14-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">popup =</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;">paste0</span>(</span>
<span id="cb14-7">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;b&gt;&lt;h3&gt;"</span>,MHprograms<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;">Agency Name</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;">"&lt;/h3&gt;&lt;/b&gt;&lt;br&gt;"</span>,</span>
<span id="cb14-8">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;h4&gt;"</span>,MHprograms<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;">Program Name</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;">"&lt;/h4&gt;&lt;br&gt;"</span>,</span>
<span id="cb14-9">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Phone: "</span>,MHprograms<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;">Agency Phone</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;">"&lt;br&gt;"</span>,</span>
<span id="cb14-10">      MHprograms<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;">Program Address 1</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;">"&lt;br&gt;"</span>,</span>
<span id="cb14-11">      MHprograms<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>City, MHprograms<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;">Program State</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, MHprograms<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;">Program Zip</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>), </span>
<span id="cb14-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">icon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">awesomeIcons</span>(</span>
<span id="cb14-13">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">library =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fa"</span>,</span>
<span id="cb14-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">icon =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(</span>
<span id="cb14-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> MHprograms<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;">Program Category Description</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">"Outpatient"</span>, </span>
<span id="cb14-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yes =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fa-stethoscope"</span>,</span>
<span id="cb14-17">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(</span>
<span id="cb14-18">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> MHprograms<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;">Program Category Description</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">"Inpatient"</span>, </span>
<span id="cb14-19">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yes =</span>  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fa-bed"</span>,</span>
<span id="cb14-20">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(</span>
<span id="cb14-21">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">test =</span> MHprograms<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;">Program Category Description</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">"Emergency"</span>, </span>
<span id="cb14-22">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yes =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fa-ambulance"</span>,</span>
<span id="cb14-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">no =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fa-users"</span></span>
<span id="cb14-24">      ))),</span>
<span id="cb14-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">iconColor =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#ffffff"</span>,</span>
<span id="cb14-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">markerColor =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"darkpurple"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">addHomeButton</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ext =</span> homebase, </span>
<span id="cb14-28">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> Home,</span>
<span id="cb14-29">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb14-30">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setView</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lng =</span> mapcenter[<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;">lat =</span> mapcenter[<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;">zoom =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span></code></pre></div>
</div>
<p><br></p>
<p>This part of the code starts out by using a <a href="https://uc-r.github.io/pipe">pipe operator</a> (%&gt;%) to connect our leaflet map functions to our shared data object <code>Sharedgeodata</code>. Let’s break down the main functions we have here:</p>
<p><br></p>
<ul>
<li><p><strong>leaflet():</strong> Initiates our leaflet map widget.</p></li>
<li><p><strong>addProviderTiles():</strong> This draws on actual map tiles to create a basemap. In this example we are using OpenStreetMaps. Being as this is Leaflet’s default, we could replace this with <code>addTiles()</code> with no options set inside and it would produce the same result.</p></li>
<li><p><strong>addAwesomeMarkers():</strong> This allows us to add markers on our map with custom color and icons on them.</p></li>
</ul>
<p><strong><em>Arguments within the addAwesomeMarkers() function:</em></strong></p>
<ul>
<li><p><strong>popup</strong> = This is the text that will popup when a marker is clicked on. You can paste data from the original data set that was converted into a shared data object (in this case, the <code>MHprograms</code> set). In this example, we’re pasting the Agency Name, Program Name, Phone Number and Address associated with each marker (observation) in the data set. I’m utilizing HTML here to assist with formatting because I did not spend years learning it on Myspace for nothing. If you’d like to learn more about HTML basics you can do so here.</p></li>
<li><p><strong>icon</strong> = These are the options that we set for our icons that show up on our marker. For this example we are using an additional function <code>awesomeIcons()</code> which will allow us to further customize the icons that are shown on our markers. In this function we have two additional arguments (library= &amp; icon=.) In this example we set <code>library=</code> to <code>"fa"</code> because we are using the Font Awesome library. Our icon argument is set to an <code>ifelse</code> function because we want the icons to change conditionally based on the type of program we are displaying at the marker.</p></li>
<li><p><strong>iconcolor</strong> = &amp; <strong>markercolor</strong> = Self-explanatory. Changes the color of the icon in the marker and the marker itself.</p></li>
<li><p><strong>addHomeButton():</strong> This allows us to add a “home/zoom to” button onto our map. Recall that we did some work for this already. This is where we fill in details to make the magic happen.</p></li>
</ul>
<p><br> <strong><em>Arguments within the addHomeButton() function:</em></strong></p>
<ul>
<li><p><strong>ext</strong> = This is the extent variable or bounding box that you’d like the home button to take the user to when clicked on. We already created our bounding box earlier and named it <code>homebase</code>.</p></li>
<li><p><strong>group</strong> = This is the group or layer that will be shown in the home button on the map. A character string is expected here. Because we wish to use the home icon for our button that we set up earlier, we use the name we set it to earlier (<code>home</code>). This tells R to place our picture there instead of the text “home.”</p></li>
<li><p><strong>position</strong> = Self-explanatory. This lets us choose where our home button will be displayed on the map. The options are <code>topleft</code>, <code>topright</code>, <code>bottomleft</code>, or <code>bottomright</code>. The default is <code>bottomright</code>.</p></li>
<li><p><strong>setView():</strong> This allows us to determine the view that is seen when the map is open. <code>lng</code> and <code>lat</code> refer to longitude and latitude respectively. We can pull these values from the object we’ve already created called <code>mapcenter</code>. We do this by subsetting the first and second elements of <code>mapcenter</code> which is our longitude and lattiude values for the center of all of our data. We can also set the zoom level with the zoom argument. The zoom scale for Leaflet goes from 0-18 with 0 being a view of the entire world and 18 being the closest you can get to a “street” view.</p></li>
</ul>
<p><br></p>
</section>
<section id="setting-up-our-second-column" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-our-second-column">Setting up our Second Column</h2>
<section id="reactive-summary-count-of-records" class="level3">
<h3 class="anchored" data-anchor-id="reactive-summary-count-of-records">Reactive Summary Count of Records</h3>
<p>Great! Our first column is done. Now we need to create our second (right) column. Looking at our mock-up will let us know what order we wanted our elements to appear in. The first element is a reactive count of the amount of locations that are present on our map at any given time. Because we are creating a new column, we can repeat the same coding header we used for the last column with some tweaks to the sizing. We’ll make this column smaller because we want the map to take up most of the space on the page. We’ll get our reactive count by using the <strong><code>summarywidget</code></strong> function:</p>
<div class="cell">
<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;">#Start of the second Column</span></span>
<span id="cb15-2">Column {data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>width<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">450</span>}</span>
<span id="cb15-3"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-----------------------------------------------------------------------</span></span>
<span id="cb15-4"></span>
<span id="cb15-5"><span class="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### **Total Locations Found:** {data-height=70}</span></span>
<span id="cb15-6"><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span>center<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span>h4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span>font color<span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#593869"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span>b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</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;">r summarywidget(Sharedgeodata, statistic='count', digits=0)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">/</span>b<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span>font<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span>h4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span>center<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div>
</div>
<p><br></p>
<p>Recall that we tell R that we want to create a box within our column with the three hashes (###). This time we will give it the name “Total Location Found:” and we will <strong>bold</strong> it by wrapping it in two asterisks (If you need a quick cheat sheet for R Markdown formatting, you can find one <a href="https://rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf">here</a>!) We will also set the height of this box which is similar to setting widths of columns.</p>
<p>Using some HTML tags, we can center and change the size and color of the <a href="https://rmarkdown.rstudio.com/lesson-4.html">inline code</a> we are using. In R Markdown, if you would like to insert R code to be evaluated within the lines of your markdown document, you can use backticks with an “r” inside of it before the code you wish to evaluate. Let’s pull this inline code out and look at what’s happening really quick:</p>
<p><br></p>
<div class="cell">
<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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r summarywidget(Sharedgeodata, statistic='count', digits=0)</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span></code></pre></div>
</div>
<p><br></p>
<p>The first backtick and “r” let’s R know that it’s about to evaluate whatever comes next and then print that result in the markdown document as if it’s not code, meaning it’ll just print out the result. The <strong>summarywidget</strong> function is interacting with our shared data object <code>Sharedgeodata</code> to grab our statistic of choice (a count) for the records that are available at any given time. Because this is the same shared data object that’s used in our map (and our data table and filters that we’ll create in a moment), whenever anything on the dashboard is changed, this value with reactively change as well. Finally, we also set the digits in this function to zero to avoid any decimal numbers.</p>
<p><br></p>
</section>
<section id="creating-a-simple-reactive-data-table" class="level3">
<h3 class="anchored" data-anchor-id="creating-a-simple-reactive-data-table">Creating a Simple Reactive Data Table</h3>
<p>We can now move on to our next box which is a data table. We set it up similarly with the hashes and set the data height like we did for the previous box. To display a data table, we’ll now use the <a href="https://www.rdocumentation.org/packages/DT/versions/0.19/topics/datatable"><code>datatable</code></a> function from the <a href="https://rstudio.github.io/DT/"><code>DT</code> package</a>. Note that this code will go into it’s own R code chunk named <code>datatable</code>.</p>
<p><br></p>
<div class="cell">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### **Program Information:** {data-height=200}</span></span></code></pre></div>
</div>
<pre><code>```{r datatable}
Sharedgeodata %&gt;% 
  datatable(
    rownames = FALSE,  
    style = "bootstrap",
    class = "compact",
    selection = "multiple",
    options = list(
      dom = "tip", 
      columnDefs = list(
        list(width = '50%',
          visible = FALSE,
          targets = c(0,4:13,15:16))),
    colnames = c(
      "Location Name" = "Program Name",
      "Ages Served" = "Populations Served",
      "Phone Number" = "Program Phone",
      "Address" = "Complete Address"
      ))) 
```</code></pre>
<p><br></p>
<p>After using the pipe operator (%&gt;%) to link to our shared data object <code>Sharedgeodata</code>, we use the <code>datatable()</code> function to make our table. There’s a lot going on here so let’s break it down:</p>
<p><br></p>
<ul>
<li><p><strong>rownames</strong> = Simply the option for controlling the row names on the table. You can also set this to a character vector with the strings desired in it.</p></li>
<li><p><strong>style</strong> = The table style you’d like. You can learn more about the DT table styles here. Sources say that R can only utilize the options <code>"default"</code>, <code>"bootstrap"</code>, and <code>"bootstrap4"</code> at this time.</p></li>
<li><p><strong>selection</strong> = Controls how many rows/columns the user can select at a time. Options are <code>"multiple"</code>, <code>"single"</code>, and <code>"none"</code>.</p></li>
<li><p><strong>options</strong> = This contains a list of options used to initialize our data table. Let’s break the options down further:</p></li>
</ul>
<p><br> <strong><em>Arguments within <code>options=</code>:</em></strong></p>
<ul>
<li><p><strong>dom</strong> = This is the <strong>D</strong>ocument <strong>O</strong>bject <strong>M</strong>odel for the table and can be seen in JavaScript usually. This is how we can get things like the actual table, pagination control, and more. For our example we’ve set it to “tip”. This translates to <strong>t</strong>-“table”, <strong>i</strong>-“table information summary”, and <strong>p</strong>-“pagination control”. These options can be placed in any order you wish, but it will affect the order in which it’s layered onto the dashboard. In this example our table gets printed first, then our information summary with the pagination buttons at the very bottom. To learn more about possible DOM options, you can read more about them <a href="https://datatables.net/reference/option/dom">here</a>.</p></li>
<li><p><strong>columnDefs</strong> = These are the options we apply specifically to our columns. Note that R needs this info within a nested list (a list within a list) to interpret it properly. <strong>width</strong> = simply controls the sizing of the columns in the table. You can play around with this to see which percentage is better for your needs. <strong>visible</strong> = tells R whether or not we want specific columns to be shown or not. We have ours set to <code>FALSE</code> because we want R to hide most of our columns. <strong>targets</strong> = is where we tell R which columns to hide with the column index numbers. Note that the indexing within datatable arguments are zero-based. Meaning that the first column in the dataset is considered index “0” instead of index “1” like R normally does.</p></li>
<li><p><strong>colnames</strong> = Self-explanatory. This is a vector of names that you want shown in the table. The function expects the name you want displayed first set to the name of the column in the actual data set.</p></li>
</ul>
<p><br></p>
</section>
</section>
<section id="setting-up-reactive-filters" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-reactive-filters">Setting up Reactive Filters</h2>
<section id="creating-a-checkbox-filter" class="level3">
<h3 class="anchored" data-anchor-id="creating-a-checkbox-filter">Creating a Checkbox Filter</h3>
<p>We can finally set up our filters that will interact with our map and data table. We do so by creating a new box in our dashboard. We’ll bold its title and name it “Map Filters:”.</p>
<p><br></p>
<div class="cell">
<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="do" style="color: #5E5E5E;
background-color: null;
font-style: italic;">### **Map Filters:**</span></span>
<span id="cb19-2"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>center<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div>
</div>
<pre><code>```{r mapfilters}

  filter_checkbox(
    id = "Program Category Description",
    label = "Program Type",
    sharedData = Sharedgeodata,
    group = ~`Program Category Description`,
    inline = TRUE
  )

```
&lt;/center&gt;</code></pre>
<p><br></p>
<p>We’ve wrapped the R code block in some HTML tags to center the filter within the box. Using the crosstalk package, we can create our filter with the <a href="https://rdrr.io/cran/crosstalk/man/filter_select.html"><code>filter_checkbox()</code></a> function.</p>
<p><strong><em>Arguments within filter_Checkbox():</em></strong></p>
<p><strong><em>id</em></strong> = Reserved for element IDs. In this example we use the dataset’s column name that we want to filter on.</p>
<p><strong><em>label</em></strong> = Expects a character string that will be shown on the actual dashboard.</p>
<p><strong><em>sharedData</em></strong> = Where we put our shared data object we created (the same object that’s used in the datatable and map).</p>
<p><strong><em>group</em></strong> = Is where we put a formula that will be used for filling the data within this particular filter. Normally this is just a column that’s either filled with factors or character vectors. The tilde (~) let’s R know it needs to interpret it as a function. In our example, our data is already cleaned and prepared for use but in other cases where special selections need to be made, you can enter more complicated functions on a column to achieve your desired result.</p>
<p><strong><em>inline</em></strong> = Tells R whether to display the filter options horizontally (<code>TRUE</code>) or vertically (<code>FALSE</code>).</p>
<p><br></p>
</section>
<section id="creating-selection-filters" class="level3">
<h3 class="anchored" data-anchor-id="creating-selection-filters">Creating Selection Filters</h3>
<p><br></p>
<p>Selection filters are very similar in it’s syntax but will display the options in a drop-down menu instead:</p>
<p><br></p>
<pre><code>```{r mapfilters2}

  filter_select(
    id = "Program Subcategory Description",
    label = "Program Setting",
    sharedData = Sharedgeodata,
    group = ~`Program Subcategory Description`,
    multiple = TRUE
  )

```</code></pre>
<pre><code>```{r mapfilters3}

  filter_select(
    id = "Populations Served",
    label = "Ages Served",
    sharedData = Sharedgeodata,
    group = ~`Populations Served`,
    multiple = FALSE
  )

```</code></pre>
<p><br></p>
<p>Note that we have two more R code chunks. Both of these filters could go in one code chunk, but I personally like to break my filters up into different chunks for easier debugging if things go wrong. It’s a matter of preference. The code for the <strong><code>filter_select()</code></strong> function is very similar to the <strong><code>filter_checkbox()</code></strong> function we just used. The only addition is that of the <strong>multiple</strong> = argument. This dictates whether the user can select multiple options at once. For example purposes, we’ve set our <code>Program Setting</code> filter to <code>TRUE</code> for this (meaning we can select multiple options) and we’ve set our <code>Ages Served</code> filter to <code>FALSE</code> (meaning we can only select one option at a time).</p>
<p><br></p>
</section>
</section>
<section id="finishing-up-the-dashboard" class="level2">
<h2 class="anchored" data-anchor-id="finishing-up-the-dashboard">Finishing up the Dashboard</h2>
<p>To finish up this dashboard, you can add some brief information about the data and it’s source. I feel this code chunk is a perfect example of some of flexdashboard limitations:</p>
<p><br> <br> <br> <br> <br> <br> <br> <br> <br> Data provided by the <b><a href="https://omh.ny.gov/">New York State Office of Mental Health</a></b> and found publicly on <b><a href="https://data.ny.gov/Human-Services/Local-Mental-Health-Programs/6nvr-tbv8">DATA.NY.GOV</a></b> <br> Example created by <b><a href="https://twitter.com/meghansharris"> Meghan Harris</a></b> with the <b><a href="https://rmarkdown.rstudio.com/flexdashboard/">flexdashboard</a></b>, <b><a href="https://rstudio.github.io/crosstalk/&quot;"> Crosstalk</a></b>, <b><a href="https://kent37.github.io/summarywidget/index.html"> SummaryWidget</a></b>, and <b><a href="https://rstudio.github.io/DT/">DT</a></b> packages.</p>
<p>````</p>
<p><br></p>
<p>You can see the finished dashboard in action on the github repo <a href="https://meghansaha.github.io/NYS_MH_Programs/">here</a>.</p>
<p><br></p>
<center>
<p><a href="https://meghansaha.github.io/NYS_MH_Programs/"><img src="https://www.thetidytrekker.com/post/dull-dashboards/images/snip4.png" alt="screenshot of finished flexdashboard" width="80%"></a></p>
<figcaption>
<p><a href="https://meghansaha.github.io/NYS_MH_Programs/">Finished FlexDashboard</a></p>
</figcaption>
</center>
<p><br></p>
</section>
<section id="limitations" class="level2">
<h2 class="anchored" data-anchor-id="limitations">Limitations</h2>
<p><br></p>
<p>While flexdashboard is great because it allows you to whip up some great dashboards without shiny, you are confined to the environment set by the template. It calls for some “hacking” sometimes and if you want your dashboard appearance to be customized (color/themes), some knowledge in CSS/HTML will make it an easier process. You can also use <a href="https://www.datadreaming.org/post/r-markdown-theme-gallery/">preset themes</a> to change the appearance of your dashboard as well.</p>
<p><a href="https://rmarkdown.rstudio.com/flexdashboard/using.html#sizing">Sizing</a> is also a tricky issue with flexdashboards. Because the template uses a browser-based flexbox engine, the sizing will vary based on things like browser window and user monitor size. You’ll also have to consider <a href="https://rmarkdown.rstudio.com/flexdashboard/using.html#mobile_layout">mobile layouts</a> for viewing the dashboard on mobile devices if this is of interest. Despite the limitations, flexdashboards can be pretty useful and a less intimidating way for those without experience in Shiny to start making dashboards in R.</p>
<p>Lastly, special thanks to <a href="https://twitter.com/mattdray">Matt Dray</a> for his <a href="https://github.com/matt-dray/earl18-presentation">presentation</a> and <a href="https://matt-dray.github.io/earl18-crosstalk/04_leaflet-flexdash-dt-crosstalk.html#interactives">work</a> on these packages! He’s the reason I found flexdashboards!</p>
<p><em>Have you ever used flexdashboards before? Have any thoughts or suggestions? Know of a better solution or way to make this script more efficient? Feel free to contact me directly! Respectful discourse towards efficient solutions or new insights is always welcomed!</em></p>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section>

 ]]></description>
  <category>r</category>
  <category>r markdown</category>
  <category>markdown</category>
  <category>dashboards</category>
  <category>mapping</category>
  <category>leaflet</category>
  <category>flexdashboards</category>
  <category>summarywidget</category>
  <category>shiny</category>
  <guid>https://www.thetidytrekker.com/post/dull-dashboards/dull-dashboards.html</guid>
  <pubDate>Mon, 15 Mar 2021 04:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/dull-dashboards/images/dulldashes.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>How I Became A “Not-Beginner” in R</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/not-beginner.html</link>
  <description><![CDATA[ 






<div id="post" style="overflow: hidden;">
<p>I’ve been in my current position as a Data Integration Specialist for about nine months now and <strong>I absolutely love it!</strong> I have achieved a lot of professional development that intertwined perfectly with my actual position. I’ve learned how to clean data more efficiently, deal with multiple data sources, and even learned how to create automated reports and dashboards in R and R Markdown.</p>
<p>More recently, I’ve had a local opioid dashboard (that I created in R) featured on our local news station. <a href="https://www.wkbw.com/news/local-news/overdose-deaths-rise-in-erie-county-during-pandemic">Like on actual TV</a> (yeah, I’m still in shock about that one.)</p>
<p>It was then that I realized: <em>I’m not a beginner in R anymore</em></p>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/images/mindblown.gif" alt="mindblown" style="width:100%" class="figure-img">
<figcaption>
<center>
<i>“Me realizing I’m not a beginner anymore”</i>
</center>
</figcaption>
</figure>
<p>That realization made me think about things though. It was clear that I wasn’t a beginner anymore, but if I wasn’t a beginner, what was I? I suddenly had an existential moment where I realized I needed to have a conversation with Data Science and R programming (as if they were actual people) and ask them about our relationship status. I had to ask them:</p>
<center>
<b>“What are we?”</b>
</center>
<p><br></p>
<p>I’m definitely <strong>not an expert</strong>, not by a long shot, but I can do A LOT of really essential things in R now. Whenever I’m interacting with others in the field, or assisting others with their programming, I always felt I needed to give a disclaimer that “I’m still learning.” People would then kind of roll their eyes at me after I explain things by telling me I’m a “wizard” or “expert” and to stop being modest. So that made me think about it. How should I be labeling myself? The more I tried to put a label on my data science/R programming proficiency, the more I realized that I’m currently in some weird gray area of my journey; and honestly? I don’t think it’s talked about enough.</p>
<p>There’s a wide range of resources out there for aspiring R programmers and data scientists. The first thing everyone tells you to do (myself included) is to start reading <a href="https://r4ds.had.co.nz/">R for Data Science</a> and to just <a href="https://www.r-exercises.com/start-here-to-learn-r/?__cf_chl_jschl_tk__=fc94d2586bba2c2a9a144f64b85da815ebdb1fd7-1613591190-0-AZy69AhWgoRCeqDS4XLA_wG0zU2Z7BbPCXmUX0Lz_wEK8xcoSdUTRMi5BqX9nYu0tHWvdMZaTfgFB4elHHMBgVz0Xp2bUYsjxoNTW-htgBCAO0aBRb14hLAmrsrnrgxb8TA9FDyYLf3fh4bsQ6H9F6QM9moGz6QqBAJr1m_FDuQIwvDtiJA4ubTea0w-k_PEwvBISqCJU1ArQnY_ZSQTyCuNIDmh4S2PFb_PZ0cj8_Agl-DHtx4NRHb80nIB7wXlwgYl21HMq0WkaqSqU9AkuctVOakoEiv7i5wTDEJKePHKU2kfUCzvuT6SbYjwUf2vrf6_9vjvVCAdbUCQXfDIhgA">practice</a>. There’s also a wealth of information about more advanced R topics like modeling and advanced Shiny app development. I remember longing to join discussions about these things at the <a href="https://rstudio.com/resources/rstudioglobal-2021/">RStudio::Global</a> conference this year, but I ultimately felt like I wasn’t quite ready to join those conversations yet. This made me feel like the space wasn’t for me and that I couldn’t really participate or engage with anyone. I found myself asking “where are the resources for people who aren’t beginners or experts?”</p>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/images/confused.gif" alt="confused" style="width:100%" class="figure-img">
<figcaption>
<center>
<i>“Me looking for something labeled ‘Intermediate R Resources’”</i>
</center>
</figcaption>
</figure>
<p>So it’s not like there aren’t any resources available at an intermediate level. <a href="https://www.datacamp.com/courses/intermediate-r">DataCamp</a> has a course (if you’re willing to pay) and there are also other resources crafted by wonderful heroes in the field that can be found by googling. But there isn’t a lot of consistency between the resources available.</p>
<p>These inconsistencies are to be expected though because there isn’t a standardized guideline that lets you know how you’re doing in the field or in R programming in general. How do aspiring data scientists/R programmers know when they’re experts? Do we ever know? Or do we have to wait until enough people deem us as “experts”? Context also matters. The set of skills one person may have may be above-and-beyond what’s needed at one company but might not cut it at another.</p>
<p>I also spent way too much time thinking about when you can officially “earn” the right to call yourself a data scientist. Do you absolutely need to master machine learning and <a href="https://aws.amazon.com/">AWS</a> before doing so? How do you know you’re not crossing into data engineering territory as those lines are kind of murky as well? Do you need to actually go out and get a degree in data science? <i><sub>(My opinion on that is no by the way, although for students who know this is what they want to do, it can’t hurt to get that degree if you like the program?)</sub></i></p>
<p>Toward the end of my existential crises, I asked myself if thinking about all of this was worth it. Is it really important to label myself? Does it matter if I’m seen as an expert in the field? Does it matter if people say I’m not a data scientist until I learn more things? As long as I’m employed, probably not. What I do think is important to realize is that the climate of the data science field in general is very fluid. Especially when bringing R proficiency into the mix. I mean think about it: if you want to be a doctor, or a lawyer, or many other professions, you normally have some sort of standardized “guide” to let you know if you’re proficient in what you do. You may have options to be certified in specialized domains as well as obtain a degree to “prove” what you can do. In Data Science and R Programming, it’s a bit different.</p>
<p>You can get <a href="https://education.rstudio.com/trainers/#info">certified to be a tidyverse instructor</a>. But from what I could tell, there’s no “official” or standardized certifications that exist globally for R programming. The certifications that I could find were either a part of newly designed data science programs at various universities, or certifications from Massive Open Online Courses (MOOC) on sites like Coursera or Udemy. I wonder if this is because R is an open-sourced program.</p>
<p>Well, after spending a good week thinking about all of this, I came to the conclusion that I didn’t really determine too much of anything. The only thing that I <strong><em>WAS</em></strong> able to determine is this:</p>
<center>
I’m not a beginner anymore… I’m a “<strong>Not-Beginner</strong>”
</center>
<p><br></p>
<p>I use the term “Not-Beginner” because I don’t know if I could even call myself intermediate! I know a lot about some things, and little about others. So I am fully comfortable and proud to use this term to describe myself!</p>
<p>So how did I do it? How did I cross the imaginary boundary into “Not-Beginner” territory in data science/R programming? Keep in mind that we’re all different. Everyone learns and operates differently, but I can retrospectively tell you how I think I got here:</p>
<p><br></p>
<section id="environment" class="level3">
<h3 class="anchored" data-anchor-id="environment">Environment</h3>
<p>This was absolutely the <strong>BIGGEST</strong> factor for me. I fell in love with R programming/Data Science in a past position while attending an <a href="https://www.eval.org/">AEA</a> conference a few years back. When I returned to work, I was bursting with motivation and energy and was itching to put all of my time and effort into learning R and Data Science. The environment I was in allowed me to do this to some degree, but it wasn’t sustainable. I found myself putting in LOADS of overtime for professional development and I always felt like others in the environment didn’t have the bandwidth to really help guide me on the journey. Although it’s understandable (due to the initial learning curve you need to get over with R), that work environment left me feeling unsupported and stagnated in the process. I was forced to continue working on my skills outside of work to eventually get to the position I’m in now because it turns out it is really hard to learn new things if you’re constantly in daily Zoom meetings (I partially blame COVID on that one).</p>
<p>While I’m the only person utilizing R in my department now, the current environment I work in is conducive to my development. It’s nurturing, flexible, and supportive which I think helps a lot.</p>
<center>
<i><sub>(Also not spending 70% of my time in meetings is a big help. This is a PSA and plea to abolish unnecessary meetings ✊🏾)</sub></i>
</center>
<p><br></p>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/images/excited.gif" alt="excited" style="width:60%" class="figure-img">
<figcaption>
<center>
<i>“How I feel logging into work most days”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
</section>
<section id="communityonline-presence" class="level3">
<h3 class="anchored" data-anchor-id="communityonline-presence">Community/Online Presence</h3>
<p>I mentioned I’m the only one that uses R in my department and that’s totally OK. (Absolutely nothing wrong with Excel, SAS, and other data tools.) Consequently, I was prompted to create an online presence for myself to get some support. Along with the Tidy Trekker site, I also joined Twitter and made more of an effort to join data science/R programming groups on Facebook and LinkedIn. Being engaged in a community can give an extra level of support that you might not be able to get within your department. Sometimes (if data agreements allow) it can be so helpful to get outside opinions on visualizations or general assistance with programming. In particular, the <a href="https://twitter.com/search?q=rstats&amp;src=spelling_expansion_revert_click">#rstats</a> and <a href="https://twitter.com/search?q=r4ds&amp;src=typed_query">#r4ds</a> Twitter communities are places that should be cherished and treasured.</p>
<p><br></p>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/images/twitter.gif" alt="excited" style="width:60%" class="figure-img">
<figcaption>
<center>
<i>Come hang out on Twitter if you aren’t already there (<a href="https://twitter.com/meghansharris"><span class="citation" data-cites="meghansharris">@meghansharris</span></a>)</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
</section>
<section id="passionmotivation" class="level3">
<h3 class="anchored" data-anchor-id="passionmotivation">Passion/Motivation</h3>
<p>I recently saw a post on Twitter that stated that you should not get into the Data Science field if you’re just in it for the money, and I fully agree. I cannot tell you how many nights I accidentally stayed up coding and learning for hours. Not because I “had” to, but because I absolutely <em>wanted</em> to. People that are here because they are passionate about the field can understand. There were times where I had to sit and ask myself if this career choice was what I wanted purely because the process of getting through those initial phases of R programming/data science were <strong>BRUTAL</strong>. If you’re trying to get your way out of the “beginner” phases of your journey, or even if you’re just beginning, you have to be honest with yourself and make sure that it is something you truly want to do. That passion is what’s going to excel you forward in your journey.</p>
<p>Back in a past position, I was overworked and sick as a result. This was around the time I was learning R. I remembered having a legitimate fever dream about coding. I would close my eyes and see the <a href="https://rstudio.com/products/rstudio/">RStudio IDE</a> staring back at me. Once that fever broke, I was thrilled to return to whatever script I was working on at the time. That’s when I knew this was for me. Now you don’t have to be at the same level of insanity that I am, clearly, but this isn’t something you should fake. If you’re not passionate about it, get out while you still can!</p>
<p><br></p>
<center>
<figure class="figure">
<img src="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/images/typing.gif" alt="Cat typing furiously on a laptop." style="width:60%" class="figure-img">
<figcaption>
<center>
<i>“Me grinding everyday because ‘coding is a lifestyle’”</i>
</center>
</figcaption>
</figure>
</center>
<p><br></p>
</section>
<section id="patience" class="level3">
<h3 class="anchored" data-anchor-id="patience">Patience</h3>
<p>This is the one I hate the most. I am admittedly NOT a patient person. I want to be a data science/R wizard and I wanted that status <strong><em>yesterday</em></strong>. I think of a personal example whenever this comes to mind.</p>
<p>When I first started learning to program in R, I immediately tried to make a <a href="https://shiny.rstudio.com/">Shiny</a> app. Needless to say I failed miserably and had almost quit the journey entirely. I felt so inadequate and not smart at all. I gave R a break for a week or two and realized I <em>really</em> wanted to learn. So I restarted the “right” way and started with the basics. I can’t tell you how long it took to get the basics down, but one day, it really did seem like things were starting to click together.</p>
<p>Soon, daunting data wrangling and mining became a breeze and an enjoyable puzzle to solve. And while I am far from where I began in my journey, I still have to remember to have patience; especially as I try to navigate these murky “not-beginner” waters. Having this level of discipline can ensure that you are learning things effectively. For me personally? I just reached the point where I am finally ready to dig into the basics of machine learning and Shiny app development. I’ll be sure to document these endeavors as I trek through them.</p>
<p><br></p>
<p><em>Are you a “Not-Beginner?” How did you know you weren’t a beginner anymore? If you ARE a beginner, what do you think will put you into that next category of data science “proficiency”? Feel free to leave a comment below to share or contact me directly! Respectful discourse is always welcomed!</em></p>
</section>
</div>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>



 ]]></description>
  <category>data science</category>
  <category>imposter syndrome</category>
  <category>r programming</category>
  <category>r</category>
  <category>statistical programming</category>
  <category>not-beginner</category>
  <category>beginner</category>
  <guid>https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/not-beginner.html</guid>
  <pubDate>Sat, 20 Feb 2021 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/how-i-became-a-not-beginner-in-r/images/lightbulb.png" medium="image" type="image/png" height="118" width="144"/>
</item>
<item>
  <title>Apply Family Notebook</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/applyfamilynotes/apply-family.html</link>
  <description><![CDATA[ 






<section id="apply-family-notes" class="level1">
<h1>Apply Family Notes</h1>
</section>
<section id="purpose" class="level1">
<h1><strong>Purpose</strong></h1>
<p>In this notebook session, I’ll be going over the “Apply” Family in base R. The “Apply Functions” refer to a group of functions that come with base R that allow you to do repetitive actions within different objects(i.e.&nbsp;data frames, lists, etc.)</p>
<p>The functions I’ll go over will be:</p>
<ul>
<li>apply()</li>
<li>lapply()</li>
<li>mapply()</li>
<li>rapply()</li>
<li>sapply()</li>
<li>tapply()</li>
<li>vapply()</li>
</ul>
<p><br></p>
<p><strong>Packages Used &amp; Loaded:</strong></p>
<pre><code>## [1] "tidyverse"  "knitr"      "kableExtra"</code></pre>
<p><br></p>
<hr>
</section>
<section id="apply-functions-inputs-outputs" class="level1">
<h1><strong>Apply Functions, Inputs, &amp; Outputs</strong></h1>
<p>A quick overview:</p>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable</span>(applychart) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-2">   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">kable_minimal</span>()</span></code></pre></div>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Name</th>
<th style="text-align: left;">What it does</th>
<th style="text-align: left;">Input</th>
<th style="text-align: left;">Output</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">apply()</td>
<td style="text-align: left;">Applies a function to the rows or columns of the object</td>
<td style="text-align: left;">Data Frame or Matrix</td>
<td style="text-align: left;">Matrix or Array</td>
</tr>
<tr class="even">
<td style="text-align: left;">lapply()</td>
<td style="text-align: left;">Applies a function to all elements within the input</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
<td style="text-align: left;">List</td>
</tr>
<tr class="odd">
<td style="text-align: left;">mapply()</td>
<td style="text-align: left;">Applies a function to multiple lists or vectors - can be considered a multivariate version of ‘sapply’</td>
<td style="text-align: left;">Multiple Lists or Vectors (i.e.&nbsp;a Data Frame)</td>
<td style="text-align: left;">List or Vector</td>
</tr>
<tr class="even">
<td style="text-align: left;">rapply()</td>
<td style="text-align: left;">Applies a function recursively through a list - nested lists</td>
<td style="text-align: left;">Nested Lists</td>
<td style="text-align: left;">Nested List or Vector depending on arguments passed</td>
</tr>
<tr class="odd">
<td style="text-align: left;">sapply()</td>
<td style="text-align: left;">A simpler version of ‘lapply’ that works on lists, data frames, and vectors</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
<td style="text-align: left;">Matrix or Vector</td>
</tr>
<tr class="even">
<td style="text-align: left;">tapply()</td>
<td style="text-align: left;">Applies a function over a ragged/jagged array (an array that has more than one dimension with varying lengths)</td>
<td style="text-align: left;">Data Frame or Vector that can be split (divided into groups/factors)</td>
<td style="text-align: left;">Array</td>
</tr>
<tr class="odd">
<td style="text-align: left;">vapply()</td>
<td style="text-align: left;">Similar to ‘sapply’, but you can pre-specify the type of value that is output, making it a bit faster</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
</tr>
</tbody>
</table>
<p><br></p>
<hr>
</section>
<section id="apply-function-apply" class="level1">
<h1><strong>Apply Function: apply()</strong></h1>
<p>The <strong>apply()</strong> function is used to apply a function to <strong>a</strong>ll rows or columns of an object. Consequently, only objects with more than one dimension can be used with apply, so a data frame or matrix.</p>
<p><br></p>
<p><code>apply(X, MARGIN, FUN)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">X</td>
<td style="text-align: left;">Data Frame or Matrix</td>
</tr>
<tr class="even">
<td style="text-align: left;">MARGIN</td>
<td style="text-align: left;">‘1’ or ‘2’ or ‘c(1,2)’ where 1 = Rows and 2 = Columns</td>
</tr>
<tr class="odd">
<td style="text-align: left;">FUN</td>
<td style="text-align: left;">The function you want to be applied to the data frame or matrix in question</td>
</tr>
</tbody>
</table>
<section id="data-frame-example" class="level2">
<h2 class="anchored" data-anchor-id="data-frame-example"><strong>Data Frame Example</strong></h2>
<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;">#Creating a mock data frame</span></span>
<span id="cb3-2">City <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;">"Buffalo"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"NYC"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seattle"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Austin"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Orlando"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Minneapolis"</span>)</span>
<span id="cb3-3">Cases <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;">500</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2012</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1876</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">635</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4512</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">823</span>)</span>
<span id="cb3-4">Controls <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;">3426</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5210</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6753</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5633</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2013</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1890</span>)</span>
<span id="cb3-5"></span>
<span id="cb3-6">records <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>(City,Cases,Controls, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb3-7"></span>
<span id="cb3-8">records</span></code></pre></div>
<pre><code>##          City Cases Controls
## 1     Buffalo   500     3426
## 2         NYC  2012     5210
## 3     Seattle  1876     6753
## 4      Austin   635     5633
## 5     Orlando  4512     2013
## 6 Minneapolis   823     1890</code></pre>
<p><br></p>
<p>We can use the apply function to calculate column sums…</p>
<p><br></p>
<div class="sourceCode" 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;">#Calculating the column sum of all applicable columns</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(records[,<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;">3</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,sum)</span></code></pre></div>
<pre><code>##    Cases Controls 
##    10358    24925</code></pre>
<p><br></p>
<p>…Or row sums. (Note that these both produce vectors and that we subset the dataframe with <strong><code>[,2:3]</code></strong> to avoid R throwing an error for the first column that has strings in it. Can’t perform a mathematical function on character strings)</p>
<p><br></p>
<div class="sourceCode" 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;">#Calculating row sums</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(records[,<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;">3</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,sum)</span></code></pre></div>
<pre><code>## [1] 3926 7222 8629 6268 6525 2713</code></pre>
<p><br></p>
<p>We can name the vectors in one line of code with the <code>names&lt;-</code> function:</p>
<p><br></p>
<div class="sourceCode" 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;">#Calculating row sums, but applying names from the "City" column</span></span>
<span id="cb9-2"><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;">City Totals</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names&lt;-</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(records[,<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;">3</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,sum), records<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>City)</span>
<span id="cb9-3"></span>
<span id="cb9-4"><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;">City Totals</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span></code></pre></div>
<pre><code>##     Buffalo         NYC     Seattle      Austin     Orlando Minneapolis 
##        3926        7222        8629        6268        6525        2713</code></pre>
</section>
<section id="applying-statistic-more-complex-functions" class="level2">
<h2 class="anchored" data-anchor-id="applying-statistic-more-complex-functions"><strong>Applying Statistic (More Complex) Functions</strong></h2>
<p><br></p>
<p>We can also do different statistical procedures based on each test’s requirement. Let’s do a T-test:</p>
<p><br></p>
<div class="sourceCode" 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;">#Making a mock data set for T-test</span></span>
<span id="cb11-2">Ex1_grades <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;">67</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">53</span>)</span>
<span id="cb11-3">Ex2_grades <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;">90</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>)</span>
<span id="cb11-4">Ex3_grades <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;">89</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>)</span>
<span id="cb11-5">Ex4_grades <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;">95</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">87</span>)</span>
<span id="cb11-6">Ex5_grades <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;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>)</span>
<span id="cb11-7">Student <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;">"Student1"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student2"</span>)</span>
<span id="cb11-8"></span>
<span id="cb11-9">Grades <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;">tibble</span>(Student,Ex1_grades,Ex2_grades,Ex3_grades,Ex4_grades,Ex5_grades)</span>
<span id="cb11-10"></span>
<span id="cb11-11">Grades</span></code></pre></div>
<pre><code>## # A tibble: 2 x 6
##   Student  Ex1_grades Ex2_grades Ex3_grades Ex4_grades Ex5_grades
##   &lt;chr&gt;         &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;
## 1 Student1         67         90         89         95        100
## 2 Student2         53         89         95         87         99</code></pre>
<p><br></p>
<p>Let’s just say we want the P.value of one sample T-tests for each student and we want to place it in this dataset as a new column. What’s important to note is that arguments that would normally be passed through to your functions, go as separate arguments at the end of the apply function, after you declare which function you want used.</p>
<p><br></p>
<div class="sourceCode" 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;">#Turning off scientific notation formatting</span></span>
<span id="cb13-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scipen =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">999</span>)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Getting index of columns that end with the word "grades"</span></span>
<span id="cb13-5">Gradeindexes <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;">grep</span>((<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grades$"</span>),<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Grades))</span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Using apply to apply the t.test.</span></span>
<span id="cb13-8">testresults <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;">apply</span>(Grades[,Gradeindexes],<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,t.test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alternative =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"two.sided"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">conf.level =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</span>)</span>
<span id="cb13-9"></span>
<span id="cb13-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Using do.call to bind p values to the data set. Because results are in a list, we can use lapply and wrap it in "as.vector" for clean transfer into the dataframe</span></span>
<span id="cb13-11"></span>
<span id="cb13-12">Grades<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;">P Values</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">as.vector</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(testresults, <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>p.value})), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))</span>
<span id="cb13-13"></span>
<span id="cb13-14">Grades</span></code></pre></div>
<pre><code>## # A tibble: 2 x 7
##   Student  Ex1_grades Ex2_grades Ex3_grades Ex4_grades Ex5_grades `P Values`
##   &lt;chr&gt;         &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt; &lt;chr&gt;     
## 1 Student1         67         90         89         95        100 0.000098  
## 2 Student2         53         89         95         87         99 0.000494</code></pre>
<hr>
</section>
</section>
<section id="lapply-function-lapply" class="level1">
<h1><strong>Lapply Function: lapply()</strong></h1>
<p>The <strong>lapply()</strong> function is used to apply a function to all elements of a <strong>l</strong>ist.</p>
<p><br></p>
<p><code>lapply(X, FUN)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">X</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
</tr>
<tr class="even">
<td style="text-align: left;">FUN</td>
<td style="text-align: left;">The function you want to be applied to the data frame or matrix in question</td>
</tr>
</tbody>
</table>
<section id="data-frame-example-1" class="level2">
<h2 class="anchored" data-anchor-id="data-frame-example-1"><strong>Data Frame Example</strong></h2>
<p><br></p>
<p>We can use lapply to make changes to a data frame.</p>
<p><br></p>
<div class="sourceCode" 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;"># Changing column names in the "records" data frame to be all CAPS</span></span>
<span id="cb15-2"></span>
<span id="cb15-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(records) <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;">lapply</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(records),str_to_upper)</span>
<span id="cb15-4"></span>
<span id="cb15-5">records</span></code></pre></div>
<pre><code>##          CITY CASES CONTROLS
## 1     Buffalo   500     3426
## 2         NYC  2012     5210
## 3     Seattle  1876     6753
## 4      Austin   635     5633
## 5     Orlando  4512     2013
## 6 Minneapolis   823     1890</code></pre>
</section>
<section id="list-example" class="level2">
<h2 class="anchored" data-anchor-id="list-example"><strong>List Example</strong></h2>
<p><br></p>
<p>We can use lapply to make changes to a list. Need to create a mock list.</p>
<p><br></p>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Creating list from randomly sampled numbers, then adding names from the "fruit" constant that comes with base R</span></span>
<span id="cb17-2"></span>
<span id="cb17-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Setting a seed for reproducibility</span></span>
<span id="cb17-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">555</span>)</span>
<span id="cb17-5"></span>
<span id="cb17-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Generating the random sample of numbers</span></span>
<span id="cb17-7">stock <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;">sample</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;">50</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb17-8"></span>
<span id="cb17-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Pulling the first five strings of the fruit constant from base R</span></span>
<span id="cb17-10">fruits <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fruit[<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="cb17-11"></span>
<span id="cb17-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Coercing the sampled numbers into a list  </span></span>
<span id="cb17-13">Inventory <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;">as.list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(stock))</span>
<span id="cb17-14"></span>
<span id="cb17-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Setting the names of each randomly sampled number to each string in out fruits vector</span></span>
<span id="cb17-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Inventory) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> fruits</span>
<span id="cb17-17"></span>
<span id="cb17-18">Inventory</span></code></pre></div>
<pre><code>## $apple
## [1] 42
## 
## $apricot
## [1] 49
## 
## $avocado
## [1] 24
## 
## $banana
## [1] 16
## 
## $`bell pepper`
## [1] 29</code></pre>
<p><br></p>
<p>We can alter the list by adding 100 to each fruit’s count and assigning the result back to <code>Inventory</code>:</p>
<p><br></p>
<div class="sourceCode" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">Inventory <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;">lapply</span>(Inventory, <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;">100</span>))</span>
<span id="cb19-2"></span>
<span id="cb19-3">Inventory</span></code></pre></div>
<pre><code>## $apple
## [1] 142
## 
## $apricot
## [1] 149
## 
## $avocado
## [1] 124
## 
## $banana
## [1] 116
## 
## $`bell pepper`
## [1] 129</code></pre>
<hr>
</section>
</section>
<section id="mapply-function-mapply" class="level1">
<h1><strong>Mapply Function: mapply()</strong></h1>
<p>The <strong>Mapply()</strong> function applies a function to <strong>m</strong>ultiple lists or vectors. This can be considered a multivariate version of ‘sapply.’</p>
<p><br></p>
<p><code>mapply(FUN, X, MoreArgs)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">FUN</td>
<td style="text-align: left;">The function you want to be applied to the lists or vectors in question</td>
</tr>
<tr class="even">
<td style="text-align: left;">X</td>
<td style="text-align: left;">The lists or vectors you want to the function applied to (normally wrapped within the ‘c()’ function</td>
</tr>
<tr class="odd">
<td style="text-align: left;">MoreArgs</td>
<td style="text-align: left;">A list (wrapped in the ’list() function) of additional arguments to pass to the function</td>
</tr>
</tbody>
</table>
<p><br></p>
<section id="multiple-list-example" class="level2">
<h2 class="anchored" data-anchor-id="multiple-list-example"><strong>Multiple List Example</strong></h2>
<p>We can use mapply() to alter different elements within multiple lists, as oppose to <em><code>lapply()</code></em> which only works within one list. Let’s create multiple lists to test <code>mapply()</code> out.</p>
<div class="sourceCode" 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;">#Want to take these separate list, add th last name "Smith" to all the names, then get the final result in one place (a list)</span></span>
<span id="cb21-2"></span>
<span id="cb21-3">names1 <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"John"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Abigail"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sam"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Judy"</span>)</span>
<span id="cb21-4">names2 <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mary"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lauri"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gus"</span>)</span>
<span id="cb21-5">names3 <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Harold"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Peter"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Natalie"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Scott"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fatima"</span>)</span>
<span id="cb21-6"></span>
<span id="cb21-7"><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;">Names List</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">mapply</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(x,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smith"</span>), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(names1,names2,names3))</span>
<span id="cb21-8"></span>
<span id="cb21-9"><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;">Names List</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span></span></code></pre></div>
<pre><code>##  [1] "John Smith"    "Abigail Smith" "Sam Smith"     "Judy Smith"    "Mary Smith"    "Lauri Smith"   "Gus Smith"     "Harold Smith"  "Peter Smith"   "Natalie Smith" "Scott Smith"   "Fatima Smith"</code></pre>
</section>
<section id="multiple-vector-example" class="level2">
<h2 class="anchored" data-anchor-id="multiple-vector-example"><strong>Multiple Vector Example</strong></h2>
<p>Mapply() can be used to vectorize function results from multiple vectors.</p>
<p>Let’s same we have vectors of numbers and we want to know the mean of all of them separately:</p>
<p><br></p>
<div class="sourceCode" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Making mock vectors, setting a seed for reproducibility.</span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">321</span>)</span>
<span id="cb23-3"></span>
<span id="cb23-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Assigning the vectors</span></span>
<span id="cb23-5">vector1 <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;">sample</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;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>)</span>
<span id="cb23-6">vector2 <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;">sample</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;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb23-7">vector3 <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;">sample</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;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)</span>
<span id="cb23-8"></span>
<span id="cb23-9">vector1</span></code></pre></div>
<pre><code>##  [1] 54 77 88 80 58 17 47 11 25 31 82 79</code></pre>
<div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">vector2</span></code></pre></div>
<pre><code>## [1] 98 75 31 82 36</code></pre>
<div class="sourceCode" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">vector3</span></code></pre></div>
<pre><code>## [1] 78 87 34 84  4 48 51 80 13</code></pre>
<p><br></p>
<p>Because we want summaries (the mean) of each vector, we can use the <strong><code>list()</code></strong> function instead of the <strong><code>c()</code></strong> function. We can use the <strong><code>MoreArgs</code></strong> argument to pass the <code>trim</code> argument to the <strong><code>mean()</code></strong> function. By default, this is set at zero, but passing it through to demonstrate.</p>
<p><br></p>
<div class="sourceCode" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Calculating the mean of each vector</span></span>
<span id="cb29-2">vectormeans <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;">mapply</span>(mean,<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(vector1,vector2,vector3), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">MoreArgs =</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;">trim =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>))</span>
<span id="cb29-3"></span>
<span id="cb29-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Setting names to the results</span></span>
<span id="cb29-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(vectormeans) <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;">"vector1"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vector2"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vector3"</span>)</span>
<span id="cb29-6"></span>
<span id="cb29-7">vectormeans</span></code></pre></div>
<pre><code>##  vector1  vector2  vector3 
## 54.08333 64.40000 53.22222</code></pre>
<hr>
</section>
</section>
<section id="rapply-function-rapply" class="level1">
<h1><strong>Rapply Function: rapply()</strong></h1>
<p>The <strong>rapply()</strong> function is used to apply a function <strong>r</strong>ecursively to all elements in a nested list.</p>
<p><br></p>
<p><code>rapply(object, f, classes, how)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Object</td>
<td style="text-align: left;">Nested Lists</td>
</tr>
<tr class="even">
<td style="text-align: left;">f</td>
<td style="text-align: left;">The function you want to be applied to the nested list in question</td>
</tr>
<tr class="odd">
<td style="text-align: left;">classes</td>
<td style="text-align: left;">Classes of elements to match on ex: ‘numeric’ , ‘character’</td>
</tr>
<tr class="even">
<td style="text-align: left;">how</td>
<td style="text-align: left;">Sets the action in which the function is executed. Standard options are: ‘replace’, ‘unlist’, ‘list’</td>
</tr>
</tbody>
</table>
<section id="nested-list-example" class="level2">
<h2 class="anchored" data-anchor-id="nested-list-example"><strong>Nested list example</strong></h2>
<p>Let’s say we have a list of cities that have a list of restaurant types embedded in them:</p>
<div class="sourceCode" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1">Restaurantdata <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Buffalo"</span> <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italian"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mexican"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"japanese"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"puerto rican"</span>),</span>
<span id="cb31-2">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Seattle"</span> <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"japanese"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"chinese"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"southern"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steakhouse"</span>),</span>
<span id="cb31-3">                       <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Miami"</span> <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;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"seafood"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cuban"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"italian"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"polish"</span>))</span>
<span id="cb31-4"></span>
<span id="cb31-5">Restaurantdata</span></code></pre></div>
<pre><code>## $Buffalo
## $Buffalo[[1]]
## [1] "italian"
## 
## $Buffalo[[2]]
## [1] "mexican"
## 
## $Buffalo[[3]]
## [1] "japanese"
## 
## $Buffalo[[4]]
## [1] "puerto rican"
## 
## 
## $Seattle
## $Seattle[[1]]
## [1] "japanese"
## 
## $Seattle[[2]]
## [1] "chinese"
## 
## $Seattle[[3]]
## [1] "southern"
## 
## $Seattle[[4]]
## [1] "steakhouse"
## 
## 
## $Miami
## $Miami[[1]]
## [1] "seafood"
## 
## $Miami[[2]]
## [1] "cuban"
## 
## $Miami[[3]]
## [1] "italian"
## 
## $Miami[[4]]
## [1] "polish"</code></pre>
<p><br></p>
<p>We want to change all of the elements so that each restaurant type is capitalized. We can do this with either the <code>tools</code> or <code>stringr</code> packages. I’ll use the <code>stringr</code> package for this example. Note that the <code>"replace"</code> option in the <code>how</code> argument will actually alter the <code>Restaurantdata</code> list, but in order to save it as such, we have to assign in back to the <code>Restaurantdata</code> object.</p>
<p><br></p>
<div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">Restaurantdata <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;">rapply</span>(Restaurantdata,stringr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>str_to_title,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">how =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"replace"</span>)</span>
<span id="cb33-2"></span>
<span id="cb33-3"></span>
<span id="cb33-4">Restaurantdata</span></code></pre></div>
<pre><code>## $Buffalo
## $Buffalo[[1]]
## [1] "Italian"
## 
## $Buffalo[[2]]
## [1] "Mexican"
## 
## $Buffalo[[3]]
## [1] "Japanese"
## 
## $Buffalo[[4]]
## [1] "Puerto Rican"
## 
## 
## $Seattle
## $Seattle[[1]]
## [1] "Japanese"
## 
## $Seattle[[2]]
## [1] "Chinese"
## 
## $Seattle[[3]]
## [1] "Southern"
## 
## $Seattle[[4]]
## [1] "Steakhouse"
## 
## 
## $Miami
## $Miami[[1]]
## [1] "Seafood"
## 
## $Miami[[2]]
## [1] "Cuban"
## 
## $Miami[[3]]
## [1] "Italian"
## 
## $Miami[[4]]
## [1] "Polish"</code></pre>
<p><br></p>
<p>We can also get a vector of our results by using the <code>unlist</code> option in the <code>how</code> function instead. Let’s add the word “restaurants” to each of these elements then <code>unlist</code> the object to place it in a vector.</p>
<p><br></p>
<div class="sourceCode" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">Restaurantvector <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;">rapply</span>(Restaurantdata,<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(x,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"restaurants"</span>),<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">how =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unlist"</span>)</span>
<span id="cb35-2"></span>
<span id="cb35-3"></span>
<span id="cb35-4">Restaurantvector</span></code></pre></div>
<pre><code>##                   Buffalo1                   Buffalo2                   Buffalo3                   Buffalo4                   Seattle1                   Seattle2                   Seattle3                   Seattle4                     Miami1                     Miami2                     Miami3                     Miami4 
##      "Italian restaurants"      "Mexican restaurants"     "Japanese restaurants" "Puerto Rican restaurants"     "Japanese restaurants"      "Chinese restaurants"     "Southern restaurants"   "Steakhouse restaurants"      "Seafood restaurants"        "Cuban restaurants"      "Italian restaurants"       "Polish restaurants"</code></pre>
<hr>
</section>
</section>
<section id="sapply-function-sapply" class="level1">
<h1><strong>Sapply Function: sapply()</strong></h1>
<p><br></p>
<p>The <strong>sapply()</strong> function is a <strong>s</strong>impler version of ‘lapply’ that works to apply functions across all elements of lists, data frames, and vectors.</p>
<p><br></p>
<p><code>sapply(X, FUN, simplify, USE.NAMES)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">X</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
</tr>
<tr class="even">
<td style="text-align: left;">FUN</td>
<td style="text-align: left;">The function you want to be applied to the data frame, list, or vector in question</td>
</tr>
<tr class="odd">
<td style="text-align: left;">simplify</td>
<td style="text-align: left;">Determines if the result should be simplified to a vector, matrix, or array</td>
</tr>
</tbody>
</table>
<p><br></p>
<p>For this example, let’s work with the <code>records</code> set form earlier. In order to get an error-free compilation, row names have to be set:</p>
<p><br></p>
<div class="sourceCode" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Pulling cities names and placing it into a vector</span></span>
<span id="cb37-2">cities <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> records<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>CITY</span>
<span id="cb37-3">  </span>
<span id="cb37-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Removing the CITY variable from the frame to isolate the numeric values</span></span>
<span id="cb37-5">records <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> records[,<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;">3</span>]</span>
<span id="cb37-6"></span>
<span id="cb37-7">records</span></code></pre></div>
<pre><code>##   CASES CONTROLS
## 1   500     3426
## 2  2012     5210
## 3  1876     6753
## 4   635     5633
## 5  4512     2013
## 6   823     1890</code></pre>
<p><br></p>
<p>Let’s divide the numbers in the dataset for each city by 10. We can store the results in a list by setting <code>simplify = FALSE</code>:</p>
<p><br></p>
<div class="sourceCode" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Dividing each number by 10 and setting the names of the elements in the list</span></span>
<span id="cb39-2">recordslist <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;">sapply</span>(records, <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;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simplify =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb39-3"></span>
<span id="cb39-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#setting the names for each element in the list. We use the a for loop to subset the two objects in the list (CASES and CONTROLS) while using the "names" function to copy the city names over</span></span>
<span id="cb39-5"></span>
<span id="cb39-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(recordslist))){</span>
<span id="cb39-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(recordslist[[i]]) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cities</span>
<span id="cb39-8">}</span>
<span id="cb39-9"></span>
<span id="cb39-10">recordslist</span></code></pre></div>
<pre><code>## $CASES
##     Buffalo         NYC     Seattle      Austin     Orlando Minneapolis 
##        50.0       201.2       187.6        63.5       451.2        82.3 
## 
## $CONTROLS
##     Buffalo         NYC     Seattle      Austin     Orlando Minneapolis 
##       342.6       521.0       675.3       563.3       201.3       189.0</code></pre>
<p><br></p>
<p>If we set <code>simplify = TRUE</code> we can get an array instead:</p>
<p><br></p>
<div class="sourceCode" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Dividing each number by 10 and setting the names of the elements in the list</span></span>
<span id="cb41-2">recordsarray <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;">sapply</span>(records, <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;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">simplify =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb41-3"></span>
<span id="cb41-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row.names</span>(recordsarray) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> cities</span>
<span id="cb41-5"></span>
<span id="cb41-6">recordsarray</span></code></pre></div>
<pre><code>##             CASES CONTROLS
## Buffalo      50.0    342.6
## NYC         201.2    521.0
## Seattle     187.6    675.3
## Austin       63.5    563.3
## Orlando     451.2    201.3
## Minneapolis  82.3    189.0</code></pre>
<hr>
</section>
<section id="tapply-function-tapply" class="level1">
<h1><strong>Tapply Function: tapply()</strong></h1>
<p><br></p>
<p><strong>Tapply()</strong> can be used when we want to perform a function over a ragged/jagged array (an array that has more than one dimension with varying lengths). It is best used to apply functions across a vector (or column in a data frame) and producing the result by factors (categories).</p>
<p><br></p>
<p><code>tapply(X, INDEX, FUN)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">X</td>
<td style="text-align: left;">Data Frame or Vector that can be `split` (has factors/categories to split on)</td>
</tr>
<tr class="even">
<td style="text-align: left;">INDEX</td>
<td style="text-align: left;">A list or vector of one or more factors or groupings that is the SAME length as X</td>
</tr>
<tr class="odd">
<td style="text-align: left;">FUN</td>
<td style="text-align: left;">The function you want to be applied to the data frame or vector in question</td>
</tr>
</tbody>
</table>
<section id="data-frame-example-2" class="level2">
<h2 class="anchored" data-anchor-id="data-frame-example-2"><strong>Data Frame Example</strong></h2>
<p>We can use <code>tapply()</code> to apply a function across a column in a dataframe. Let’s make a simple data frame:</p>
<div class="sourceCode" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setting a seed for reproducibility</span></span>
<span id="cb43-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">789</span>)</span>
<span id="cb43-3"></span>
<span id="cb43-4">Teams <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;">"UK"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USA"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Egypt"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ireland"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"UK"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USA"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USA"</span>)</span>
<span id="cb43-5">Seconds <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;">runif</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(Teams), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">240</span>)</span>
<span id="cb43-6">Runners <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>(Teams,Seconds)</span>
<span id="cb43-7"></span>
<span id="cb43-8">Runners</span></code></pre></div>
<pre><code>##     Teams   Seconds
## 1      UK 176.97782
## 2     USA  49.63476
## 3   Egypt  32.49623
## 4 Ireland 154.23733
## 5      UK 133.35138
## 6     USA  34.23435
## 7     USA 150.25773</code></pre>
<p><br></p>
<p>Let’s say we want to calculate the average time (seconds) for each Team. We can use <code>tapply()</code> for this.</p>
<p><br></p>
<div class="sourceCode" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1">Runner_means <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;">tapply</span>(Runners<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Seconds, Runners<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Teams, mean)</span>
<span id="cb45-2"></span>
<span id="cb45-3">Runner_means</span></code></pre></div>
<pre><code>##     Egypt   Ireland        UK       USA 
##  32.49623 154.23733 155.16460  78.04228</code></pre>
<p><br></p>
<p>The results are stored into an array. If a new table is desired it can be manipulated to do so:</p>
<p><br></p>
<div class="sourceCode" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1">Runners_summary <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Team =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(Runner_means), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Mean =</span>Runner_means, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb47-2"></span>
<span id="cb47-3">Runners_summary</span></code></pre></div>
<pre><code>##      Team      Mean
## 1   Egypt  32.49623
## 2 Ireland 154.23733
## 3      UK 155.16460
## 4     USA  78.04228</code></pre>
</section>
<section id="jagged-vectors-example" class="level2">
<h2 class="anchored" data-anchor-id="jagged-vectors-example"><strong>Jagged Vectors Example</strong></h2>
<p><br></p>
<p>We can use <code>tapply()</code> to also preform tasks across multiple vectors of different lengths as long as the amount of factors matches the amount of the elements overall in each vector</p>
<p><br></p>
<div class="sourceCode" 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;">#Create a list of factors </span></span>
<span id="cb49-2">Names <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;">"Meghan"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gus"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jennifer"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gus"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jennifer"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Natalie"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Meghan"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jennifer"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gus"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Natalie"</span>)</span>
<span id="cb49-3"></span>
<span id="cb49-4">Scores1 <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;">90</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">67</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>)</span>
<span id="cb49-5">Scores2 <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;">99</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">99</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>)</span>
<span id="cb49-6">Scores3 <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;">100</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>)</span>
<span id="cb49-7"></span>
<span id="cb49-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Placing them together gives us a vector that has a length of 10. Same as the "Names" vector</span></span>
<span id="cb49-9">Test_scores <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>(Scores1,Scores2,Scores3)</span>
<span id="cb49-10"></span>
<span id="cb49-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#We can now apply the same function across this vectors and get summarized results</span></span>
<span id="cb49-12">Test_averages <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;">tapply</span>(Test_scores,Names,mean)</span>
<span id="cb49-13"></span>
<span id="cb49-14">Test_averages</span></code></pre></div>
<pre><code>##      Gus Jennifer   Meghan  Natalie 
## 88.66667 88.66667 94.50000 88.50000</code></pre>
<hr>
</section>
</section>
<section id="vapply-function-vapply" class="level1">
<h1><strong>Vapply Function: vapply</strong></h1>
<p><br></p>
<p><strong>Vapply()</strong> is similar to ‘sapply’, but you can pre-specify the type of <strong>v</strong>alue that is output, making it a bit faster.</p>
<p><br></p>
<p><code>vapply(X, INDEX, FUN)</code></p>
<p><br></p>
<p>Where:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 22%">
<col style="width: 77%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Argument</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">X</td>
<td style="text-align: left;">Data Frame, List, Vector</td>
</tr>
<tr class="even">
<td style="text-align: left;">FUN</td>
<td style="text-align: left;">The function you want to be applied to the data frame or vector in question</td>
</tr>
<tr class="odd">
<td style="text-align: left;">FUN.VALUE</td>
<td style="text-align: left;">A template for the value you want returned</td>
</tr>
</tbody>
</table>
<p><br></p>
<p>So let’s look at our <code>records</code> data set again. We can use <code>vapply()</code> to compute the sum of each group (Cases and Controls) We can use vapply to ensure the result we get is numeric. Vapply is said to be a “safer” alternative to sapply because it will ensure you’re getting the results you are expecting. If you don’t set the <code>FUN.VALUE</code> argument in the function, it will throw an error, whereas sapply would automatically produce a result:</p>
<p><br></p>
<div class="sourceCode" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb51-1">records</span></code></pre></div>
<pre><code>##   CASES CONTROLS
## 1   500     3426
## 2  2012     5210
## 3  1876     6753
## 4   635     5633
## 5  4512     2013
## 6   823     1890</code></pre>
<div class="sourceCode" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb53-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Using vapply to get the sums. We place a "1" in the numeric argument to tell R we are expecting a non-zero number</span></span>
<span id="cb53-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(records, sum, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN.VALUE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">numeric</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div>
<pre><code>##    CASES CONTROLS 
##    10358    24925</code></pre>
<div class="sourceCode" id="cb55" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb55-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Without the FUN.VALUE argument, vapply will throw an error</span></span>
<span id="cb55-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">vapply</span>(records, sum)</span></code></pre></div>
<pre><code>## Error in vapply(records, sum): argument "FUN.VALUE" is missing, with no default</code></pre>
<div class="sourceCode" id="cb57" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb57-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#In comparison, sapply will give us the result without specifying the output type</span></span>
<span id="cb57-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sapply</span>(records, sum)</span></code></pre></div>
<pre><code>##    CASES CONTROLS 
##    10358    24925</code></pre>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>


</section>

 ]]></description>
  <category>r</category>
  <category>r programming</category>
  <category>base r</category>
  <category>apply</category>
  <category>lapply</category>
  <category>sapply</category>
  <category>functional programming</category>
  <guid>https://www.thetidytrekker.com/post/applyfamilynotes/apply-family.html</guid>
  <pubDate>Sat, 23 Jan 2021 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/applyfamilynotes/images/applyfamily.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Ordering Months</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/orderingmonths/ordering-months.html</link>
  <description><![CDATA[ 






<div id="post" style="overflow: hidden;">
<center>
<img src="https://www.thetidytrekker.com/post/orderingmonths/images/ordering_months.png">
</center>
<p><br><br></p>
<p><strong>The Problems:</strong> <em>1) You need to order your “Month” categories chronologically on a ggplot but they keep plotting alphabetically.</em></p>
<p><em>2) You don’t have data for all 12 months of the year, but want to have every month of the year plotted on your graph.</em></p>
<p><strong>The First Example:</strong></p>
<p>Here we have simple aggregated data for total new patients enrolled in a clinic during the year. The dataset might look like this:</p>
<figure class="figure">
<center>
<img src="https://www.thetidytrekker.com/post/orderingmonths/images/snip1.png" alt="snapshot of example data showing five months with varying values attached to them: February - 21, May - 16, August - 33, September - 40, December - 11" style="width:40%" class="figure-img">
<figcaption>
<i>The “Patients” Example Data</i>
</figcaption></center>

</figure>
<p>For those of you who are just starting out in ggplot and have managed to plot your first graphs, you may have realized that ggplot doesn’t always display your categorical data as you see it in your view pane. Often times, ggplot will plot categorical variables in alphabetical order. This happens because the categorical variable you are trying to graph (“Month”) is not set as an ordered factor. In R, factors are categorical variables that have values assigned to them. To learn a bit more about factors, I highly recommend referring to <a href="https://r4ds.had.co.nz/factors.html">chapter 15 (Factors)</a> in <a href="https://github.com/hadley">Wickham</a> and <a href="https://github.com/garrettgman">Grolemund’s</a> <em><a href="https://r4ds.had.co.nz/index.html">book R For Data Science</a></em>.</p>
<p><em>If you’d like to follow along within the R project files you can download them from the Tidy Trekker Github Repository. Be sure to fully extract the ZIP. folders for proper access. You can also download the data set used and load it into your own session for practice by clicking the GitHub button below:</em></p>
<center>
<a href="https://github.com/Meghansaha/OrderingMonths/archive/master.zip"><img src="https://www.thetidytrekker.com/post/orderingmonths/images/GHsnip.png" alt="GitHub Download"></a>
<figcaption>
<i>Direct <a href="https://github.com/Meghansaha/OrderingMonths/archive/master.zip">download link</a> for this post’s example data</i>
</figcaption></center>


<section id="loading-in-your-libraries-and-data" class="level2">
<h2 class="anchored" data-anchor-id="loading-in-your-libraries-and-data">Loading in Your Libraries and Data:</h2>
<p>For this example, we’ll use the <a href="https://readr.tidyverse.org/">readr</a>, <a href="https://dplyr.tidyverse.org/">dplyr</a>, <a href="https://tidyr.tidyverse.org/">tidyr</a>, and <a href="https://ggplot2.tidyverse.org/">ggplot2</a> <a href="https://tidyverse.org">tidyverse</a> packages to load in our data, do some wrangling, and get it visualized on to a graph. We’ll load in our libraries and our first dataset named <code>Patients</code>.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Loading in the appropriate libraries===</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(readr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'dplyr'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:stats':

    filter, lag</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union</code></pre>
</div>
<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>(tidyr)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Loading the data into the environment===</span></span>
<span id="cb5-5">Patients <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;">"data/Patients.csv"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Rows: 5 Columns: 2</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): Month
dbl (1): New Patients

ℹ 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>
<center>
<img src="https://www.thetidytrekker.com/post/orderingmonths/images/careful.png" style="width:20%"><br> <em>Pay attention to the file path in the <code>read_csv</code> function. If you copy and paste that command and are not using the project files within the Github link above, you will get an error!</em>
</center>
<p><br></p>
</section>
<section id="plotting-your-data" class="level2">
<h2 class="anchored" data-anchor-id="plotting-your-data">Plotting Your Data:</h2>
<p>So we have our data in, and we decide we want to make a simple bar chart of these counts. We’ll use a basic ggplot framework to do this. We’ll call the resulting plot <code>Patientgraph</code>. If you’re rusty or just learning ggplot, I highly recommend downloading the ggplot cheat sheet to get up to speed!</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Making a ggplot with the data "as-is"====</span></span>
<span id="cb8-2">Patientgraph <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>(Patients, <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> Month, <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;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</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;">"#693ead"</span>)</span>
<span id="cb8-4">  </span>
<span id="cb8-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Let's view the graph===</span></span>
<span id="cb8-6">Patientgraph</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/orderingmonths/ordering-months_files/figure-html/codesnip2-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>We can see that we’ve got our data onto the graph, but the <code>Month</code> categories are displayed alphabetically. This may be fine in some cases, but usually we expect to see months listed chronologically (January - December).</p>
<p>We can already see from the graph and dataset that we have 5 distinct months to work with. We can confirm this by asking R to show us the unique values for the <code>Month</code> variable. We should also take the time to identify the <code>Month</code> variable’s class. (Data Type)</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Viewing the unique values in the "Month" variable===</span></span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(Patients<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Month)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "February"  "May"       "August"    "September" "December" </code></pre>
</div>
<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;"># Identifying the class of the "Month" variable===</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(Patients<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Month)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "character"</code></pre>
</div>
</div>
<p><br></p>
</section>
<section id="refactoring-your-data-for-plotting" class="level2">
<h2 class="anchored" data-anchor-id="refactoring-your-data-for-plotting">Refactoring Your Data for Plotting:</h2>
<p>As I mentioned earlier, in order to adjust these categories on a ggplot, we need to change the variable type, or it’s class, into a factor. Let’s make this change and save the results into a new dataset called “Patients2.”</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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">Patients2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Patients <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Month =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Month, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</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;">"February"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"May"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"August"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"September"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"December"</span>)))</span></code></pre></div>
</div>
<p><br> Using the <a href="https://magrittr.tidyverse.org/">pipe operator</a> <code>%&gt;%</code> with the dplyr verb <a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate</a>, we can alter the existing <code>Month</code> variable into a factor using <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/factor">base R’s “factor” function</a>. In this function, you simply tell R which variable you want to convert and then pass a “levels” argument. This argument manually dictates the order in which categories will be seen on the ggplot. Note that because we are using the pipe operator, we do not need to do a direct subset to this variable in the factor function. <em>ex: (<code>Patients$Month</code>)</em></p>
<center>
<img src="https://www.thetidytrekker.com/post/orderingmonths/images/careful.png" style="width:20%"><br> <em>Although cumbersome, manually setting your factor levels may be required from time to time. Make sure capitalization, spelling, and punctuation match what’s in your data set exactly. Even having whitespace in your data can cause issues. You can tackle whitespace issues with Stringr’s <a href="https://stringr.tidyverse.org/reference/str_trim.html">str_trim function</a>.</em>
</center>
<p><br></p>
<p>Now let’s replot the graph with the <code>Patients2</code> data.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Plotting Patients2===</span></span>
<span id="cb14-2">Patientgraph2 <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>(Patients2, <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> Month, <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;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</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;">"#693ead"</span>)</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Viewing Patients2 graph===</span></span>
<span id="cb14-6">Patientgraph2</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/orderingmonths/ordering-months_files/figure-html/codesnip5-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>Now this is more like it! Now I did say that this method was cumbersome. Thankfully, there are ways to cut some of this work down. We’ll do this by using some constants in R.</p>
</section>
<section id="the-month.name-constant-for-easier-refactoring" class="level2">
<h2 class="anchored" data-anchor-id="the-month.name-constant-for-easier-refactoring">The “month.name” Constant for Easier Refactoring:</h2>
<p>If you don’t know about constants, you can read about them <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/Constants">here</a>! They are basically preset values that come in base R. We can use the <code>month.name</code> constant in this particular example. Let’s look at it first though to get familiar.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Printing out the month.name constant to take a look at it===</span></span>
<span id="cb15-2">month.name</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "January"   "February"  "March"     "April"     "May"       "June"     
 [7] "July"      "August"    "September" "October"   "November"  "December" </code></pre>
</div>
</div>
<p><br></p>
<p>We can see all the months are here in this constant ready for us to use! So how do we apply this to help us out with our coding? By calling on the <code>month.name</code> constant for our <code>levels</code> argument when factoring! Recall that we have five months in our dataset. In order to use the “month.names” constant, it has to only contain those five months (Remember that the levels have to match exactly.) We do this by subsetting out only the months we need. We’ll do this and store the results into a dataset called “Patients3” with the following code:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Refactoring the "easier" way===</span></span>
<span id="cb17-2">    Patients3 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Patients <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-3">        <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;">Month =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Month, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> month.name[month.name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(Month)]))</span></code></pre></div>
</div>
<p><br></p>
<p>You may see that our code looks similar to the last factoring we did. The only thing that has changed is the addition of what’s passed into the <code>levels</code> argument:</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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">month.name[month.name <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unique</span>(Month)]</span></code></pre></div>
</div>
<p><br></p>
<p>The code above tells R to “look” within the “month.name” constant and only return the values that are also uniquely in (<code>%in%</code>) our “Month” variable. If needed, you can learn more about R’s operators like <code>%in%</code> <a href="https://www.tutorialspoint.com/r/r_operators.htm">here</a>. If we plot <code>Patients3</code>, we can see the result is the same as the original refactored graph we made.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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">Patients3</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 2
  Month     `New Patients`
  &lt;fct&gt;              &lt;dbl&gt;
1 February              21
2 May                   16
3 August                33
4 September             40
5 December              11</code></pre>
</div>
</div>
<p><br></p>
<p>The only difference here is not having to do the cumbersome coding of spelling out our months in our factor function.</p>
</section>
<section id="when-all-months-are-present-in-your-dataset" class="level2">
<h2 class="anchored" data-anchor-id="when-all-months-are-present-in-your-dataset">When All Months Are Present in Your Dataset:</h2>
<p>So what if we have a dataset with all the months accounted for in the year? We can do less work! Let’s load in our <code>Patients_complete</code> dataset to take a look.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Loading in the "complete" Patients dataset</span></span>
<span id="cb21-2">Patients_complete <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;">"data/Patients_complete.csv"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Rows: 12 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): Month
dbl (1): New Patients

ℹ 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><br></p>
<center>
<img src="https://www.thetidytrekker.com/post/orderingmonths/images/careful.png" style="width:20%"><br> <em>Pay attention to the file path in the <code>read_csv</code> function. If you copy and paste that command and are not using the project files within the Github link above, you will get an error!</em>
</center>
<p><br></p>
<figure class="figure">
<center>
<img src="https://www.thetidytrekker.com/post/orderingmonths/images/snip2.png" alt="snapshot of patients complete data set. All months of the year are shown with values for each. Jan-5, Feb - 21, Mar - 26, April - 15, May - 16, June - 9, July - 23, August - 33, Sept - 40, Oct - 29, Nov - 37, December 11" style="width:40%" class="figure-img">
<figcaption>
<i>The “Patients Complete” Example Data</i>
</figcaption></center>

</figure>
<p><br> So you might’ve guessed that this dataset would also return a graph in which the months are ordered alphabetically. We’ll make a quick one and store it in <code>Patientgraph4</code>.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Confirming assumptions with the "Patients_complete" dataset===</span></span>
<span id="cb23-2">  Patientgraph4 <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>(Patients_complete, <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> Month, <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;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</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;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</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;">"#693ead"</span>)</span>
<span id="cb23-4">  </span>
<span id="cb23-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Viewing it==</span></span>
<span id="cb23-6">Patientgraph4</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/orderingmonths/ordering-months_files/figure-html/codesnip11-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p><br></p>
<p>This time, because we have all of the months in the dataset, the code for refactoring can be simplified. We’ll store it in a dataset called <code>Patients4</code> then graph it in a ggplot called <code>Patientgraph5</code>.</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Easier refactoring when all months are present ===</span></span>
<span id="cb24-2">Patients4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Patients_complete <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb24-3">  <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;">Month =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Month, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> month.name))</span>
<span id="cb24-4">  </span>
<span id="cb24-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creating the plot ===</span></span>
<span id="cb24-6">Patientgraph5 <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>(Patients4, <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> Month, <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;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>))<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb24-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</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;">"#693ead"</span>)</span>
<span id="cb24-8"></span>
<span id="cb24-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Viewing it ===</span></span>
<span id="cb24-10">Patientgraph5</span></code></pre></div>
<div class="cell-output-display">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.thetidytrekker.com/post/orderingmonths/ordering-months_files/figure-html/codesnip12-1.png" class="img-fluid quarto-figure quarto-figure-center figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="when-you-want-to-fill-in-missing-months-on-a-plot" class="level2">
<h2 class="anchored" data-anchor-id="when-you-want-to-fill-in-missing-months-on-a-plot">When You Want to Fill in Missing Months on a Plot:</h2>
<section id="the-second-example" class="level3">
<h3 class="anchored" data-anchor-id="the-second-example">The Second Example:</h3>
<p>Finally, let’s go back to our original <code>Patients</code> dataset. What do you do if you don’t have all the months of the year, but you want to plot all of the months? To do this, we need to create “placeholder” data points within our dataset. We do this by adding the months we are missing to our dataset and assigning them a numeric value of 0. We’ll do this and store it into a final dataset called <code>Patients_modified</code>. Be sure to remember to refactor this as well!</p>
<p><br></p>
<div class="cell" data-layout-align="center">
<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;"># Adding missing months to our "Patients" data set===</span></span>
<span id="cb25-2"> Patients_modified <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;">left_join</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> month.name),Patients, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Month"</span>) </span>
<span id="cb25-3"> </span>
<span id="cb25-4"> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#Replacing coerced NAs from the previous code to a numeric 0 then refactoring our new dataset===</span></span>
<span id="cb25-5">Patients_modified <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Patients_modified <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</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;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <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;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</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;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>),<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">New Patients</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb25-7">  <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;">Month =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Month, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> month.name))</span></code></pre></div>
</div>
<p><br></p>
<p>Essentially what we’ve done is transformed our vector of <a href="http://month.name/">month.name</a> constants into a data frame, or <a href="https://tibble.tidyverse.org/">tibble</a>. Doing this allows us to complete a <a href="https://dplyr.tidyverse.org/reference/join.html">left join</a> and merge all of the “Month” names in the constant into our dataset. The first mutate function uses the <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/ifelse">ifelse function</a> to replace all the NAs of this merge with numeric values of 0. The second mutate function is just the refactoring method that was previously introduced. Let’s create the graph, call it <code>Patientgraph6</code>, and view it.</p>
<p><br></p>
</section>
</section>
<section id="creating-the-final-plot" class="level1">
<h1>Creating the final plot===</h1>
<p>Patientgraph6 &lt;- ggplot(Patients_modified, aes(x = Month, y = <code>New Patients</code>)) + geom_bar(stat = “identity”, fill= “#693ead”)</p>
<p>#And view it=== Patientgraph6</p>
<p>````</p>
<p><br></p>
<p>And there we are! All of our data is plotted and we can see all of the months in the year! You’ll notice those pesky month labels may be colliding with each other. This may vary based on the machine/browser window you’re working with. If you need to fix cases like these, I’d recommend the <a href="https://stringr.tidyverse.org/reference/str_wrap.html">str_wrap()</a> function from the <a href="https://stringr.tidyverse.org/index.html">stringr</a> package.</p>
<p><em>Have any thoughts or suggestions? Know of a better solution or way to make this process more efficient? Feel free to leave a comment below to share or contact me directly! Respectful discourse towards efficient solutions or new insights is always welcomed!</em></p>
</section></div>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>



 ]]></description>
  <category>data manipulation</category>
  <category>data carpentry</category>
  <category>data visualization</category>
  <category>r</category>
  <category>statistical programming</category>
  <category>ggplot</category>
  <category>r walkthrough</category>
  <guid>https://www.thetidytrekker.com/post/orderingmonths/ordering-months.html</guid>
  <pubDate>Sat, 23 Jan 2021 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/orderingmonths/images/ordering_months.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>I Participated in My First Tidy Tuesday! : Project Recap</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/i-participated-in-my-first-tidy-tuesday-project-recap/i-participated-in-my-first-tidy-tuesday-project-recap.html</link>
  <description><![CDATA[ 






<div id="post" style="overflow: hidden;">
<center>
<img src="https://www.thetidytrekker.com/post/i-participated-in-my-first-tidy-tuesday-project-recap/images/first_tidy_tuesday.png">
</center>
<p><br></p>
<p>To kick off the year, I took the “leap of faith” and participated in my first <a href="https://github.com/rfordatascience/tidytuesday/blob/master/README.md">TidyTuesday</a>! A weekly community activity hosted by the <a href="https://twitter.com/R4DScommunity">R4DSCommunity</a> on Twitter that challenges those of all skill levels and disciplines to explore a data set and make a data visualization of it. This week’s data was from the <a href="https://transitcosts.com/">Transit Costs Project</a>.</p>
<p>This Tidy Tuesday was challenging mainly because I was on a crunch for time. However, I did not want to pass up an opportunity to practice with <a href="https://plotly.com/r/">Plotly (for R)</a>. In my current position, I’m tasked with building some internal dashboards to keep track of a lot of opioid data. Using Plotly can allow for interactive/dynamic graphs (instead of static, non-moving graphs.)</p>
<p>This activity was great for practicing the obvious data cleaning and wrangling skills. I also got to practice conditional formatting (colors of the lollipops), <a href="https://ggplot2.tidyverse.org/">ggplot</a> manipulation, and lookup functions. Drawing conclusions from the data is not recommended due to the nature of the selected data sets. Tidy Tuesday really encourages developing your process as a data scientist to hone in on fundamental skills! That being said, don’t read too much into it! If you’re interested in the code used for this plotly plot, or the process used for the data, you can find it on my Github Repo <a href="https://github.com/Meghansaha/TidyTuesday/tree/master/2021/Transit_Costs%201-5-21">here</a>.</p>
<p><em>Feel free to contact me directly! Respectful discourse towards efficient solutions or new insights is always welcomed!</em></p>
</div>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>



 ]]></description>
  <category>data science</category>
  <category>tidytuesday</category>
  <category>r Programming</category>
  <category>r</category>
  <category>statistical programming</category>
  <category>data carpentry</category>
  <category>ggplot</category>
  <category>plotly</category>
  <category>data cleaning</category>
  <category>data visualization</category>
  <category>project recap</category>
  <guid>https://www.thetidytrekker.com/post/i-participated-in-my-first-tidy-tuesday-project-recap/i-participated-in-my-first-tidy-tuesday-project-recap.html</guid>
  <pubDate>Wed, 06 Jan 2021 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/i-participated-in-my-first-tidy-tuesday-project-recap/images/first_tidy_tuesday.png" medium="image" type="image/png" height="108" width="144"/>
</item>
<item>
  <title>Setting Intentions</title>
  <dc:creator>Meghan Harris</dc:creator>
  <link>https://www.thetidytrekker.com/post/setting-intentions/setting-intentions.html</link>
  <description><![CDATA[ 






<div id="post" style="overflow: hidden;">
<p><br> No doubt, 2020 has been extremely difficult for everyone. (<em>Who would have known it would be so draining to be productive and just live during a global pandemic?</em>) Many people were forced to adapt to a “new normal” (<em>don’t worry, I won’t use the “U” word we’re all tired of hearing.</em>) Many of us in the U.S who were fortunate to remain employed had to adapt to working from home (WFH). This personally has not bothered me as I’m an advocate for WFH even during non-pandemic times due to the <a href="https://www.flexjobs.com/blog/post/benefits-of-remote-work/">benefits it brings</a> I’ve been fortunate enough to continue working during the pandemic and actually switch jobs to take baby steps into my data science career.</p>
<p>Given that the theme of 2020 seemed to be “instability”, I thought it would be wonderful to have just one thing I could consider “stable.” My own little pocket on the web. I hope to document my progression into data science and finally make some contributions to the field. I have my reservations about this. Candidly, I consider myself a “baby data scientist.” My path into data science was by no means “traditional.” I will be the first to say that my weakness is not having a formal computer science or statistics degree. Back in my day, (*<em>shudder</em>* <em>I sound old.</em>) data science wasn’t as well known or prominent as it is today. If you were majoring in computer science, it was because you were going to do hardcore computer programming. Retrospectively, a career in data science/coding made sense for me even in my adolescence. (<em>I proudly spent hours learning CSS and HTML to have the best Myspace page. I have also always loved applied statistics; hindsight truly is 2020. Yes, that is a pun. No, I’m not sorry.</em>)</p>
<p>The goal of this site is to help myself and anyone else while I progress into this field. I hope to offer a different perspective to solutions in data science based on needs/functionality. I am by no means an expert and I want to make that OK. I definitely am affected by imposter syndrome taking this route into data science but I’d like to serve as an example for others on a similar path. (<em>You can learn more about imposter syndrome through a data scientist’s perspective by watching fellow data scientist, <a href="https://twitter.com/KenJee_DS">Ken Jee’s</a> , video on it <a href="https://www.youtube.com/watch?v=Uf0dO-pgOrk">here</a>.</em>) I want to encourage others to continue to push through and learn about this field despite the challenges. On a personal note, I want to learn all that I can so that I can advocate and push for more data science efforts in health research and mental health data analytics.</p>
<p>So, moving forward, I’ll be using the <a href="./../../blog.html">Exploration Corner</a> to share various categories of posts that include:</p>
<p><br></p>
<p><strong>General/Data Musings:</strong> Informal posts like this one that you’re reading right now that are general or my thoughts/opinions on data-related things that I find.</p>
<p><strong>Project Recaps:</strong> These will be posts that briefly describe projects I have completed. Project recaps will differ from full walkthroughs as they will not be a step-by-step guide. It will generally explain a goal, data sources, a quick list of processes/packages used, and a link to the actual project and source code on <a href="https://github.com/Meghansaha">Github</a>.</p>
<p><strong>R Explorations:</strong> These posts will act as a playground for me personally. I’m reserving this space as a way to leave notes for myself that might help others. This can include deeper dives into packages and functions or even cheat sheets.</p>
<p><strong>R Walkthroughs:</strong> These posts will be targeted step-by-step guides on how I’ve done random things in R. I use R daily for my work and find myself doing different things daily. These will also include links to example data sets and project files on <a href="https://github.com/Meghansaha">Github</a> when applicable.</p>
<p>As I trek through my journey, I do plan to learn <a href="https://www.python.org/">python</a> and dive into <a href="https://d3js.org/">D3.js</a>. When I get to that point, I’ll add blog categories accordingly.</p>
<p><em>Where are you on your data science journey? Do you have any suggestions for different blog posts? Feel free to leave a comment below to share your thoughts or contact me directly! Respectful discourse is always welcomed!</em></p>
</div>
<style> 
body {
  overflow: hidden; /* Hide scrollbars */
}

</style>



 ]]></description>
  <category>data science</category>
  <category>imposter syndrome</category>
  <category>intentions</category>
  <category>r</category>
  <category>statistical programming</category>
  <guid>https://www.thetidytrekker.com/post/setting-intentions/setting-intentions.html</guid>
  <pubDate>Thu, 31 Dec 2020 05:00:00 GMT</pubDate>
  <media:content url="https://www.thetidytrekker.com/post/setting-intentions/images/fireworks.png" medium="image" type="image/png" height="63" width="144"/>
</item>
</channel>
</rss>
