Could not find ggplot() function despite being loaded in?

Hi community,

So I'm going through Hadley Wickham's "R for Data Science" book, and I'm very early on. I did the first 5 exercises that start on page 6. I'm a decently experienced R user, but I've never really received an error like this before (see screenshot).

Before posting, I searched for similar questions/answers such as:

  1. could not find function "ggplot" -Rstudio cloud - #7 by gflegg
  2. could not find function "ggplot" - #9 by flik
  3. ggplot2 - Using ggplot function in R error : could not find function ggplot - Stack Overflow

For those posts, the problem either seemed to be that the ggplot2 package wasn't loaded into the environment, or a possible workspace image directory thing being saved but the packages not.

I tried closing R Studio and not saving the workspace image, then reopening the markdown file I've created storing all the code. All I'm trying to do is knit the HTML file and see what I've got so far. I have also tried making sure that the packages are loaded in. When I quit out of R and didn't save the workspace image, upon reopening R Studio, the first things I did were type "library(ggplot2)" and "library(tidyverse)" (without the quote marks obviously) but that still didn't fix my issue. Is there something I'm missing or overlooking in the above linked posts? In case it matters (because apparently there are situations where it sometimes does I guess?), I am on macbook pro with Big Sur. Ultimately, I am asking the questions, "Why am I getting this error and what is the fix?"

Thanks for taking the time to read my question!


You need to load the libraries you are using within the Rmarkdown file (in a code chunk) rather than loading it before. So just create a code chunk at the beginning in which you load all the packages that you need


Ah, that makes sense. I assumed that if I have the packages already loaded into the working global R environment/session (not sure what the proper terminology is here...) that it would suffice, but I guess not. I learned something today. Thanks! That definitely solved it.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.