ggplot2 Basics | Dataplexa

ggplot2 Basics

In this lesson, you will learn the fundamentals of ggplot2, one of the most powerful and widely used data visualization packages in R.

Unlike base R plots, ggplot2 follows a structured and layered approach to building visualizations, making plots easier to understand, customize, and scale.


What Is ggplot2?

ggplot2 is a visualization package based on the concept of the Grammar of Graphics.

Instead of creating plots in one step, ggplot2 builds visualizations by combining different layers such as data, aesthetics, and geometric objects.

This approach makes plots more readable and flexible.


Installing and Loading ggplot2

Before using ggplot2, it must be installed and loaded into the R session.

install.packages("ggplot2")
library(ggplot2)

Basic Structure of a ggplot

A basic ggplot consists of three main components:

  • Data – the dataset being used
  • Aesthetics – how variables map to visual properties
  • Geometries – the type of plot (points, bars, lines, etc.)

These components are combined using the + operator.


Sample Dataset

We will use a simple dataset to understand ggplot2 concepts.

data <- data.frame(
  month = c("Jan", "Feb", "Mar", "Apr", "May"),
  sales = c(200, 250, 300, 280, 350)
)

Creating a Basic Scatter Plot

A scatter plot shows the relationship between two variables.

In ggplot2, this is done using geom_point().

ggplot(data, aes(x = month, y = sales)) +
  geom_point()

Creating a Line Plot

Line plots are useful for showing trends over time.

They are created using geom_line().

ggplot(data, aes(x = month, y = sales)) +
  geom_line()

Creating a Bar Chart

Bar charts are commonly used to compare values across categories.

In ggplot2, bar charts are created using geom_col() when values are already calculated.

ggplot(data, aes(x = month, y = sales)) +
  geom_col()

Adding Titles and Labels

Clear titles and labels improve the readability of plots.

You can add them using the labs() function.

ggplot(data, aes(x = month, y = sales)) +
  geom_col() +
  labs(
    title = "Monthly Sales",
    x = "Month",
    y = "Sales Amount"
  )

Changing Colors

Colors can be customized to improve visual appeal.

You can set colors directly inside the geometry function.

ggplot(data, aes(x = month, y = sales)) +
  geom_col(fill = "steelblue")

Why ggplot2 Is Preferred

  • Clean and professional-looking visuals
  • Highly customizable
  • Works well with large datasets
  • Standard tool in data science and analytics

📝 Practice Exercises


Exercise 1

Create a scatter plot using ggplot2.

Exercise 2

Create a bar chart showing monthly sales.

Exercise 3

Add a title and axis labels to a plot.

Exercise 4

Change the color of bars in a bar chart.


✅ Practice Answers


Answer 1

ggplot(data, aes(x = month, y = sales)) +
  geom_point()

Answer 2

ggplot(data, aes(x = month, y = sales)) +
  geom_col()

Answer 3

ggplot(data, aes(x = month, y = sales)) +
  geom_col() +
  labs(title = "Monthly Sales")

Answer 4

ggplot(data, aes(x = month, y = sales)) +
  geom_col(fill = "orange")

What’s Next?

In the next lesson, you will learn about Advanced ggplot2, where you will explore themes, facets, and advanced customization.