Skip to Content
Course content

3: R Package Ecosystem Overview

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I was messing around with some data this morning and I realized I wanted to create a scatter plot of penguin flipper lengths versus bill depths. I've got the data in a dataframe, but I'm tired of fighting with the base R plot() arguments to make it look professional. I want to use ggplot2 because it's the industry standard for a reason.

The "Could not find function" wall

I'll start by trying to call the plotting function. Here is where I usually trip up if I've just opened a fresh R session:

ggplot(penguins, aes(x = flipper_length_mm, y = bill_depth_mm)) + 
  geom_point()

R immediately throws an error: Error in ggplot(...) : could not find function "ggplot". Right. I forgot that just because a package exists in the R universe doesn't mean it's currently loaded into my brain—or my workspace. I try to load it:

library(ggplot2)

And there it is: Error in library(ggplot2) : there is no package called ‘ggplot2’. This is the crucial distinction in R that confuses a lot of people. install.packages() puts the code on your hard drive (like downloading an app), but library() puts it into your current session's memory (like opening the app). Since this is a fresh environment, I haven't even downloaded it yet.

Tapping into CRAN

I need to get ggplot2. I'll run the installation command:

install.packages("ggplot2")

While that's running, notice what's happening in the console. R isn't just grabbing one file; it's downloading a handful of other packages too. You'll see things like rlang, lifecycle, and gtable flying by. This is the "dependency" system. ggplot2 doesn't reinvent the wheel; it relies on these smaller, specialized packages to handle low-level logic. They all come from CRAN (The Comprehensive R Archive Network), which is essentially the "App Store" for R. It's strictly curated, meaning if a package is on CRAN, it's passed a set of quality and compatibility tests.

The Tidyverse shortcut

Now, I actually need the penguin data itself. I remember there's a package called palmerpenguins. I could install that individually, but as I'm working, I realize I'll probably need dplyr for filtering and tidyr for cleaning. I could run install.packages() three more times, or I can use a metapackage.

install.packages("tidyverse")
library(tidyverse)

The tidyverse isn't really a single package; it's a collection. By loading it, I've just brought in ggplot2, dplyr, readr, and several others in one go. It's a huge time-saver, though in a production script, I'd eventually narrow this down to only the specific libraries I need to keep the memory footprint small. I'm in exploration mode, so "load everything" works fine for now.

Going off-road to GitHub

Here is where it gets interesting. I heard about a niche package that adds some fancy custom themes to ggplot, but it's not on CRAN yet because the author is still tweaking it. If I try install.packages("fancy-themes"), it'll fail because CRAN doesn't know it exists.

To get this, I have to go straight to the source: GitHub. I'll use the remotes package to handle this:

install.packages("remotes")
remotes::install_github("username/fancy-themes")

This is the "wild west" of the ecosystem. Unlike CRAN, there's no gatekeeper here. You're installing code directly from a developer's repository. It's incredibly powerful for getting the latest features, but it's also how you accidentally break your environment if the developer pushed a buggy commit ten minutes ago. I generally reserve GitHub installs for specific tools or when I'm contributing to the package development myself.




📋 Practical Task

Building a Penguin Morphometrics Visualization

Your task is to move from a blank R session to a completed visualization using the package ecosystem. Follow these requirements:

  • Install and load the palmerpenguins package to access the dataset.
  • Install and load the ggplot2 package.
  • Create a scatter plot using the penguins dataset where:
    • The x-axis is bill_length_mm.
    • The y-axis is bill_depth_mm.
    • The points are colored by the species column.
  • Add a title to your plot: "Penguin Bill Dimensions by Species".

Goal: Demonstrate that you can successfully navigate the install $\rightarrow$ load $\rightarrow$ execute workflow across different packages.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.