What is ggplot2? - Basic packages in R Visualization Basics

When you're analyzing data, you'll often want to present your results visually. But it can seem complicated at first, and you may get frustrated when you can't get the look you want. That's okay! The R language provides the ggplot2 Packagesallows anyone to breathe life and story into their data.

In this post, we'll use ggplot2 to plot a simple boxplotand barplot (bar graph)and learn the basics of visualization one by one. I'll explain the code step by step, so feel free to follow along!

What is ggplot2?

ggplot2 is R's most widely used Visualization packages. It makes it easy to create complex graphs and allows you to see the structure and patterns in your data at a glance, which is very useful when you want to understand the distribution of data or compare two groups, for example. Today we'll use ggplot2 to create a boxplotand barplotto get the basics down.

As a side note, the ggplot2 package was developed by Hadley Wickham and colleagues, and the main idea behind it is based on an approach called the "Grammar of Graphics". You can see why the package name starts with gg.

1. plot a boxplot with ggplot2

boxplot is a plot of the data against the Median, Distribution, Minimum and Maximumat a glance. It's easy to see how the data is distributed by group. Now let's write some code together.

📌 Code example

# Load ggplot2 library
library(ggplot2)

# Create example data: Data for three groups, A, B, and C
data <- data.frame(
    group = rep(c("A", "B", "C"), each = 50),
    value = c(rnorm(50, mean = 5), rnorm(50, mean = 7), rnorm(50, mean = 9))
)

# Create boxplot
ggplot(data, aes(x = group, y = value)) +
    geom_boxplot() +
    labs(title = "Distribution of Values by Group", x = "Group", y = "Value")

✨ Code description

  • library(ggplot2): Load the ggplot2 package.
  • data.frame(): Create an example data frame. Here, we've created categories (A, B, C) called group and a random value called value.
  • ggplot(): The function that starts the graph. Put the desired data into ggplot to start the visualization.
  • aes(x = group, y = value): Specifies the axes of the graph. The x-axis is the group and the y-axis is the value.
  • geom_boxplot(): This function draws a boxplot, which shows the median and distribution of the data.
  • labs(): Add a title and axis labels for the graph. To make it pretty, I gave it a title and named each axis.

Now, if you run the code you wrote, you can see the distribution of groups A, B, and C at a glance, as shown below. Boxplotwill be finished!

ggplot2 - boxplot pic
ggplot2 - boxplot

2. plot a barplot with ggplot2

Now let's create a barplot, which is often used to visualize the frequency (counts) of categorical data. It's an easy way to see how many data points belong to each group.

📌 Code example

# Load the ggplot2 library
library(ggplot2)

Generate the # example data: Three groups of data, A, B, and C
data <- data.frame(
  group = rep(c("A", "B", "C"), each = 50),
  value = c(rnorm(50, mean = 5), rnorm(50, mean = 7), rnorm(50, mean = 9))
)

Generate a # barplot
ggplot(data, aes(x = group)) +
  geom_bar() +
  labs(title = "Frequency by Group", x = "Group", y = "Frequency")

✨ Code description

  • aes(x = group): Set the group variable on the x-axis to represent the frequency of each group.
  • geom_bar(): Function to draw a bar graph. By default, it automatically calculates the frequency of each group.
  • labs(): Adds a title and axis labels to the graph to make it easier to understand.

Now, when you run this code, you'll see the frequency of the data for each group as shown below Bar graphson the page!

ggplot2 - barplot pic
ggplot2 - barplot

Wrap-up: Welcome to the world of ggplot2!

Now you've learned how to draw simple boxplots and barplots using ggplot2. It can be a little daunting at first because you're not used to it, but ggplot2 can visualize so many different aspects of your data, and the more you learn, the more interesting it gets!

If you are curious to see a bar graph with error bars using ggplot2, you can use the A look at the big event the world is watching in 2024: the US presidential election - R Coding Visualization post to get started. And in the next post, we'll introduce some advanced options in ggplot2, so stay tuned!

As a side note, the page that introduces ggplot2 talks about a good way to learn the package, which is as follows

  1. R for Data Sciencein the Data Visualization and Communication chapterThis book provides a comprehensive introduction to the tidyverse and helps you quickly master the core elements of ggplot2. It breaks down difficult concepts in an easy-to-understand way, which is great for those just starting out.
  2. Online lecturesIf you'd prefer to learn through an online course, check out Kara Woo's "Data Visualization in R With ggplot2" course, where you'll learn the basics of visualization from the ground up with real-world examples.
  3. Webinars: You might also want to join Thomas Lin Pedersen's "Plotting Anything with ggplot2" webinar, which is a great way to get some hands-on experience with the real thing.
  4. The R Graphics Cookbook: If you want to focus on quickly creating common graphs, check out Winston Chang's "The R Graphics Cookbook", which is organized in the form of recipes that provide solutions to common graphics problems.
  5. If you're an advanced userIf you've mastered the basics and want to go deeper, "ggplot2: Elegant Graphics for Data Analysis", which explains the theoretical underpinnings of ggplot2 and shows how all the pieces connect. Once you understand the theory behind ggplot2, you'll be well on your way to creating new kinds of graphics to suit your needs.

Similar Posts