R - function not found - "could not find function "..."

I have installed the ggplot2 and ggExtra packages and done the library function on these but when trying to do a ggplot function code (Sorry if my lingo is confusing, R noob in a uni stats class) in Rmarkdown I continually get an error saying could not find function "ggplot". Have tried updating all my packages but that has not helped.

A few things to try... which will either solve your problem, or at least eliminate some possibilities:

  1. Did you actually run the code in the chunk that contains your calls to library()? This is sort of the “is it plugged in” question, but I wanted to check because since those calls are in a different chunk, they aren’t going to run when you run the chunk with the plotting code.

  2. If you look in the Packages pane, do you see ggplot2 in the list? If not, then it would be helpful to see what output you get if you type in install.packages("ggplot2", dependencies = TRUE) at the console. (If you paste in console output here, please have pity on your helpers and format it as code — use the </> button at the top of the box where you type in your post).

  3. If you do see ggplot2 in the list, is its checkbox checked? (checked means the package is currently loaded) If not, try checking the checkbox (which will just run library(ggplot2) in the console) and trying your plotting code again.

Let me know how you go!


Checked through the different chunks and rerun them just in case, still getting the same problem.

ggMarginal(p+theme_gray(),type="histogram", fill="steelblue",col="darkblue")
Have included screenshots of all the areas and included the code chunks I'm using. I copied the code from the textbook we are using for the subject and double checked that my code is the same

Sorry the images didn't upload

Could you please try running your code as a reprex (short for minimal reproducible example)?

It's self-contained, so we can be sure that we're all looking at the same thing, and will help us/you isolate the problem.

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

Right now the best way to install reprex is:

# install.packages("devtools")
Troubleshooting clipboard access

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

Were you able to find ggplot2 in the Packages pane?

Alternatively, what happens if you run this line of code?:

ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()

To explain:

I’m working from the hypothesis that despite your efforts, the ggplot2 package isn’t actually getting loaded. The most likely reason for that is that the package hasn’t successfully installed. That’s why I wanted to see the console output from running the install.packages line.

If ggplot2 has installed successfully, then running the line above should work even if the package isn’t loaded.

If ggplot2 hasn’t installed successfully, you’re going to have to examine the console output from install.packages() to figure out why. For debugging installation issues, it may be easiest if you can find in-person help — since you’re taking a class, are there TAs or a peer support group you can ask for help?

1 Like

Thanks a lot! advice number 3 helped me out when stuck with the same issue!

1 Like