To fix the R no package called ggplot2 error in RStudio or Jupyter, proceed as following:
- Save your R Script or Jupyter Notebook.
- Type the following command: install.packages(‘ggplot2’)
- Invoke the ggplot library using the following command: library(ggplot2)
Read on for a more detailed explanation on how to reproduce and fix the error.
Reproducing the no package ggplot error
In order to reproduce the error, proceed as following:
- Open your RStudio script or Jupyter notebook.
- Call the ggplot2 library using the following command: library (ggplot2)
- You’ll see the following error message
Error in library(ggplot2) : there is no package called ‘ggplot2’
Here is a screenshot take from RStudio on Windows – similar message in macOS or Linux distributions).
Fix the Error in library ggplot2 exception
The reason for the error is the fact that the ggplot2 library is not installed in your computer. You have to add ggplot2 to your environment before invoking it. You might consider installing the entire tidyverse library that includes many useful R libraries such as: dplyr, lubridate, stringr, purr, readr, highr and obviously the ggplot2 plotting capabilities.
Solving the error is very simple – just go ahead and install gglot2 or tidyverse:
- In your RStudio script or Jupyter notebook type the following:
install.packages('tidyverse') #or install.packages('ggplot2')
- Make sure to use quotation marks around the library name as highlighted above.
- Next, RStudio / Jupyter will install the relevant package and its dependencies.
- Now, go ahead and invoke ggplot2 as needed. For example, assuming that you have an R DataFrame named my_df with two columns: x_col and y_col, you can use the following command to write a line plot.
library(ggplot2) ggplot(my_df, aes= (x = x_col, y=y_col)) + geom_line()