Open In App

How to Install ggplot2 in Anaconda

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

ggplot2 is a powerful library in R programming language that helps plot high-quality graphs. It is based on the Grammar of Graphics, making it easy to produce complex multi-layered graphics. R is a popular statistical programming language used for its packages making analysis of data easier, one such package is ggplot2. This article will discuss ways of installing ggplot2 in Anaconda.

Prerequisites

Before dealing with anaconda, R, and ggplot2 we must know the basics of these three things.

  • Make sure you have Anaconda installed in your working system.
  • Ensure you have Rstudio installed on your pc and know the ggplot2 package and how it works.
  • A basic understanding of the command line interface (CLI) is required.

Environments in Anaconda

Using environments in Anaconda is crucial for managing dependencies and avoiding conflicts. Here’s why:

  • Isolation of Dependencies: Each environment in Anaconda is isolated therefore avoiding the possibility of conflicts between the packages. This isolation ensures that different projects do not clash with each other and make an effect.
  • Reproducibility: It is shareable therefore it increases the reproducibility.
  • Conflict Avoidance: Dependency conflicts occur when two packages require different versions of the same dependency. Isolated environments prevent such conflicts, ensuring stable and reliable workflows.
  • Version Control: We can manage the versions required for the specific project in Anaconda.

Click on the start menu/Search bar and type "Anaconda Prompt"

anaconda_navigation_gfg
Install ggplot2 in Anaconda

Create a new environment

This is an optional step but recommended as it helps avoid conflicts between the packages. It also helps in dealing with any dependencies. Open the anaconda prompt as discussed before and follow the given commands. There are several ways of creating a new environments

1. To create a new environment named ggplot_env, use the following command:

conda create --name ggplot_env

Output:

locationgfg
Install ggplot2 in Anaconda

We can also use Anaconda Navigator to create a new environment. This is much easier way.

1. Go to the Environment section in Anaconda Navigator and select it.

environmentgfg
Install ggplot2 in Anaconda

Installing ggplot2

To install ggplot2 in the anaconda prompt we need to follow the below-mentioned syntax.

You can install ggplot2 using the conda command. This will also handle any dependencies required by ggplot2.

Install ggplot2:

conda install -c r r-ggplot2
Collecting package metadata (current_repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
current version: 23.1.0
latest version: 24.5.0

Please update conda by running.

$ conda update -n base -c defaults conda

Or to minimize the number of packages updated during conda update use

conda install conda=24.5.0



# All requested packages already installed.

Since I already had the packages installed this output was shown. For people installing it the first time it will take a while to download all the packages after getting your permission.

We can also install Packages in Anaconda Navigator. It is an easier way to install package.

1. Select the Environment: Choose the environment where you want to install new packages from the list on the left. In the "Search Packages" box, type the name of the package you want to install (e.g., ggplot2).

ggplot2GFG
Install ggplot2 in Anaconda

Check the box next to the package name and click the "Apply" button to install it.

installapplyGFG
Install ggplot2 in Anaconda

Verify Installation

It is important to verify the successful installation of the ggpot2.

For this, we can load the R and the library in Anaconda. If the library is loaded without any errors then it means we have successfully installed it.
Open R in Anaconda and load the library ggplot2.

R
library(ggplot2)

Output:

R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x6_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

Registered S3 methods overwritten by 'ggplot2':
method from
[.quosures rlang
c.quosures rlang
print.quosures rlang

If there are no errors, ggplot2 is installed successfully.

Visualization using the mtcars dataset

We can create a simple plot in Anaconda using R's powerful package ggplot2 on a basic built-in dataset "mtcars". This dataset has information about different cars and their features. You have to run the below-mentioned code in Anaconda.

R
library(ggplot2)

# Load the mtcars dataset
data(mtcars)

# Create a scatter plot
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = "Scatter plot of Weight vs. Miles per Gallon",
       x = "Weight (1000 lbs)",
       y = "Miles per Gallon")

Output:

ANACONDA-GFG
Install ggplot2 in Anaconda

Conclusion

In this article, we discussed how we can use R language in Anaconda as well as how we can use ggplot2 to plot meaningful graphs that help us in data visualization and understanding. We discussed the ways of installing and working in Anaconda.


Next Article
Article Tags :

Similar Reads