Master Data Visualization With ggplot2
Last Updated :
24 Apr, 2025
In this article, we are going to see the master data visualization with ggplot2 in R Programming Language. Generally, data visualization is the pictorial representation of a dataset in a visual format like charts, plots, etc.Â
These are the important graphs in data visualization with ggplot2,
Bar chart in R
A bar chart is a representation of the dataset in the format of a rectangular bar. Respectively, its height depends on the values of variables in a dataset. You can use geom_bar() to create bar charts with ggplot2.
R
library(ggplot2)
data <- data.frame(
fruit = c("Apple", "Banana", "Orange", "Mango"),
quantity = c(300, 450, 280, 800),
color = c("red", "yellow", "orange", "green")
)
bar_chart <- ggplot(data, aes(x = fruit,
y = quantity,
fill = fruit,
color = fruit)) +
geom_bar(stat = "identity") +
labs(title = "Fruit Quantity Chart",
x = "Fruit", y = "Quantity (in units)") +
scale_fill_manual(values = data$color) +
scale_color_manual(values = data$color) +
theme_minimal()
bar_chart
Output: Â Â Â
Bar chart Violin Plots in R
Violin plots are similar to box plots. It also shows the probability density at various values. In the violin plots, Denser areas indicate a higher probability at that value. The geom_violin() function is used to create violin plots in ggplot2.
R
library(ggplot2)
data <- data.frame(
category = rep(c("Category 1", "Category 2", "Category 3"), each = 100),
value = c(rnorm(100, mean = 0, sd = 1),
rnorm(100, mean = 2, sd = 1.5),
rnorm(100, mean = -1, sd = 0.5))
)
violin_plot <- ggplot(data, aes(x = category, y = value)) +
geom_violin(fill = "lightblue", color = "black") +
labs(title = "Violin Plot", x = "Category", y = "Value") +
theme_minimal()
print(violin_plot)
Output: Â Â Â
violin plots in RDensity Plots in R
It is used to compute the density estimation. Also, It is a smoothed version of a histogram. The geom_density() function is used to create density plots in ggplot2.Â
R
library(ggplot2)
# Load mtcars dataset
data(mtcars)
# Select the variable for the density plot
data <- mtcars$mpg
# Create the density plot
density_plot <- ggplot() +
geom_density(aes(x = data), fill = "lightblue", color = "black") +
labs(title = "Density Plot of MPG", x = "MPG", y = "Density") +
theme_minimal()
print(density_plot)
Output: Â Â Â
Density Plot in RBox Plots in R
It is a visual representation of the spread of data for a variable and displays the range of values along with the median and quartiles. The geom_boxplot() function is used to create box plots in ggplot2.Â
R
library(ggplot2)
set.seed(20000)
data <- data.frame(A = rpois(900, 3),
B = rnorm(900),
C = runif(900))
# Creating the boxplot using ggplot2
boxplot_plot <- ggplot(data_long, aes(x = variable, y = value)) +
geom_boxplot() +
labs(title = "Boxplot of Data values", x = "Variable", y = "Value") +
theme_minimal()
print(boxplot_plot)
Output:
Box plot in RPie Chart in R
It is a circular statistical graphic, which is divided into slices to illustrate numerical proportion. Each slice of the pie chart represents a proportion of the whole. In R, pie charts can be created using the pie function from the ggplot2 library.
R
library(ggplot2)
data <- data.frame(categories = c("Mango", "Apple", "Orange"),
values = c(520, 358, 405))
pie_chart <- ggplot(data, aes(x = "", y = values, fill = categories)) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0) +
labs(title = "Sybudheen Shop", x = "Fruits", y = "total") +
theme_void()
pie_chart
Output:
Pie ChartStacked Bar Chart in R
It is a bar chart where the bars are divided into segments to represent the contribution of different categories to the total. In other words, each bar in a stacked bar chart represents the total value of multiple categories. The height of each segment of the bar represents the value of a particular category, and the segments are stacked on top of each other to represent the total value of the bar.
R
# creating data frame
fruits <- c(rep("Apple", 3), rep("Mango", 3),
rep("Banana", 3), rep("Orange", 3))
quantity <- sample.int(50, 12)
Shop <- rep(c('A', 'B', 'C'),4)
data <- data.frame(fruits, Shop, quantity)
# plotting graph
ggplot(data, aes(fill = Shop, y = quantity, x = fruits))+
geom_bar(position = "stack", stat = "identity")+
ggtitle("Different fruit sells in different shops")+
theme(plot.title = element_text(hjust = 0.5))
Output:
Stacked Bar chart in R - GeeksforgeeksScatter plots in R
In R, a scatter plot is a graphical representation of two sets of data on a Cartesian plane. It displays individual data points as dots, with the x-axis and y-axis representing two variables. The position of each data point on the x-axis and y-axis represents the values of the corresponding x and y variables. Scatter plots can be used to visualize the relationship between two variables and identify trends, patterns, and outliers in the data. To create a scatter plot in R, you can use the plot() or ggplot function.
R
trains <- c(10, 20, 30, 40, 50, 34, 23, 49, 21, 13)
passengers <- c(100, 200, 300, 400, 500,
229, 346, 432, 198, 235)
plot(trains, passengers,
xlab = "Number of Trains",
ylab = "Number of Passengers",
main = "Scatter Plot of Trains vs Passengers")
abline(lm(passengers~trains), col = "red")
Output:
Scatter PlotFrequency plots in R
In R, a frequency plot, also known as a histogram, is a graphical representation of the distribution of a set of continuous or discrete data. It shows how frequently each data value or range of values occurs in the data set. The frequency plot is created by dividing the range of the data into equal intervals, called bins, and counting the number of data points that fall into each bin. The height of each bar in the histogram represents the frequency of the corresponding bin. To create a frequency plot in R, you can use the hist function. The hist function takes the data as input and generates the histogram plot.
R
x <- rnorm(2000)
hist(x, main = "Frequency Plot",
xlab = "Values",
ylab = "Frequency",
col = "gray",
border = "black")
Output:
Frequency PlotIn R, ggplot2 to visualize time series data and its components like seasonality, trends, and others.
ggplot2 is a powerful data visualization library in R that allows you to create various types of plots, including time series plots. Time series data refers to data that is collected and recorded over time, such as daily stock prices or monthly sales data. When visualizing time series data in R using ggplot2, you can use various techniques to identify and highlight the different components of the time series, such as trends, seasonality, and residuals. These components are important in understanding the behavior of the time series and making predictions.
R
library(ggplot2)
library(dplyr)
data(AirPassengers)
ts_data <- as.data.frame(AirPassengers)
ts_data$date <- time(AirPassengers)
ggplot(ts_data, aes(x = date, y = AirPassengers)) +
geom_line() +
xlab("Year") +
ylab("Passengers") +
ggtitle("Air Passengers")
library(forecast)
ts_decomposed <- decompose(AirPassengers)
ts_decomposed_df <- as.data.frame(ts_decomposed)
ggplot(ts_decomposed_df, aes(x = index(ts_decomposed_df$trend),
y = ts_decomposed_df$trend)) +
geom_line() +
xlab("Year") +
ylab("Trend") +
ggtitle("Trend Component")
ggplot(ts_decomposed_df, aes(x = index(ts_decomposed_df$seasonal),
y = ts_decomposed_df$seasonal)) +
geom_line() +
xlab("Year") +
ylab("Seasonality") +
ggtitle("Seasonality Component")
ggplot(ts_decomposed_df, aes(x = index(ts_decomposed_df$random),
y = ts_decomposed_df$random)) +
geom_line() +
xlab("Year") +
ylab("Residuals") +
ggtitle("Residual Component")
Output:
Time Series Data- The AirPassengers dataset from the datasets package is loaded in this section. The data is then transformed into a data frame, and the time() function is used to construct the date column. The date is plotted on the x-axis and the total number of passengers is plotted on the y-axis using the ggplot() function. To depict the time series as a line graph, the geom_line() layer is added. Labelling the x-axis, y-axis, and adding a plot title are done with the ggtitle(), xlab(), and ylab() methods, respectively.
 - The forecasting package is loaded here. The time series AirPassengers is broken down into its trend, seasonal, and random components using the decompose() function. Then a data frame is created using the deconstructed parts. With the x-axis representing the index of the trend component and the y-axis indicating the values of the trend, the trend component is drawn using ggplot() and geom_line(). The plot's title and x- and y-axis labels have been established.
 - Similarly, ggplot() and geom_line() are used to plot the seasonality component. The values of the seasonal component are represented on the y-axis, and their index is represented on the x-axis. The plot's title and x- and y-axis labels have been established.
Â
Finally, ggplot() and geom_line() are used to plot the residual component. The index of the residual component is shown by the x-axis, while its values are represented by the y-axis. The plot's title and x- and y-axis labels have been established.
Save and export R plots:
Saving plots in R
click on export on right side .
Saving plots in R
Here we can save plots in image and pdf format.
Similar Reads
Data visualization with R and ggplot2
The ggplot2 ( Grammar of Graphics ) is a free, open-source visualization package widely used in R Programming Language. It includes several layers on which it is governed. The layers are as follows: Layers with the grammar of graphicsData: The element is the data set itself.Aesthetics: The data is t
7 min read
Working with External Data
Basic Plotting with ggplot2
Plot Only One Variable in ggplot2 Plot in R
In this article, we will be looking at the two different methods to plot only one variable in the ggplot2 plot in the R programming language. Draw ggplot2 Plot Based On Only One Variable Using ggplot & nrow Functions In this approach to drawing a ggplot2 plot based on the only one variable, firs
5 min read
How to create a plot using ggplot2 with Multiple Lines in R ?
In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then
3 min read
Plot Lines from a List of DataFrames using ggplot2 in R
For data visualization, the ggplot2 package is frequently used because it allows us to create a wide range of plots. To effectively display trends or patterns, we can combine multiple data frames to create a combined plot. Syntax: ggplot(data = NULL, mapping = aes(), colour()) Parameters: data - Def
3 min read
How to plot a subset of a dataframe using ggplot2 in R ?
In this article, we will discuss plotting a subset of a data frame using ggplot2 in the R programming language. Dataframe in use: AgeScoreEnrollNo117700521880103177915419752051885256199630717903581971409188345 To get a complete picture, let us first draw a complete data frame. Example: C/C++ Code #
8 min read
Change Theme Color in ggplot2 Plot in R
A theme in ggplot2 is a collection of settings that control the non-data elements of the plot. These settings include things like background colors, grid lines, axis labels, and text sizes. we can use various theme-related functions to customize the appearance of your plots, including changing theme
4 min read
Modify axis, legend, and plot labels using ggplot2 in R
In this article, we are going to see how to modify the axis labels, legend, and plot labels using ggplot2 bar plot in R programming language. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : Set the stat parameter to
5 min read
Common Geometric Objects (Geoms)
Comprehensive Guide to Scatter Plot using ggplot2 in R
Scatter plot uses dots to represent values for two different numeric variables and is used to observe relationships between those variables. To plot the Scatter plot we will use we will be using the geom_point() function. This function is available in ggplot2 package which is a free and open-source
7 min read
Line Plot using ggplot2 in R
In a line graph, we have the horizontal axis value through which the line will be ordered and connected using the vertical axis values. We are going to use the R package ggplot2 which has several layers in it. First, you need to install the ggplot2 package if it is not previously installed in R Stu
6 min read
R - Bar Charts
Bar charts provide an easy method of representing categorical data in the form of bars. The length or height of each bar represents the value of the category it represents. In R, bar charts are created using the function barplot(), and it can be applied both for vertical and horizontal charts. Synta
4 min read
Histogram in R using ggplot2
A histogram is an approximate representation of the distribution of numerical data. In a histogram, each bar groups numbers into ranges. Taller bars show that more data falls in that range. It is used to display the shape and spread of continuous sample data. Plotting Histogram using ggplot2 in RWe
5 min read
Box plot in R using ggplot2
A box plot is a graphical display of a data set which indicates its distribution and highlights potential outliers It displays the range of the data, the median, and the quartiles, making it easy to observe the spread and skewness of the data. In ggplot2, the geom_boxplot() function is used to creat
5 min read
geom_area plot with areas and outlines in ggplot2 in R
An Area Plot helps us to visualize the variation in quantitative quantity with respect to some other quantity. It is simply a line chart where the area under the plot is colored/shaded. It is best used to study the trends of variation over a period of time, where we want to analyze the value of one
3 min read
Advanced Data Visualization Techniques
Combine two ggplot2 plots from different DataFrame in R
In this article, we are going to learn how to Combine two ggplot2 plots from different DataFrame in R Programming Language. Here in this article we are using a scatter plot, but it can be applied to any other plot. Let us first individually draw two ggplot2 Scatter Plots by different DataFrames then
2 min read
Annotating text on individual facet in ggplot2 in R
In this article, we will discuss how to annotate a text on the Individual facet in ggplot2 in R Programming Language. To plot facet in R programming language, we use the facet_grid() function from the ggplot2 library. The facet_grid() is used to form a matrix of panels defined by row and column face
5 min read
How to annotate a plot in ggplot2 in R ?
In this article, we will discuss how to annotate functions in R Programming Language in ggplot2 and also read the use cases of annotate. What is annotate?An annotate function in R can help the readability of a plot. It allows adding text to a plot or highlighting a specific portion of the curve. Th
4 min read
Annotate Text Outside of ggplot2 Plot in R
Ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geomsâvisual marks that represent data points, and a coordinate system. There are many scenarios where we need to annotate outside the plot area or specific area as
2 min read
How to put text on different lines to ggplot2 plot in R?
ggplot2 is a plotting package in R programming language that is used to create complex plots from data specified in a data frame. It provides a more programmatic interface for specifying which variables to plot onto the graphical device, how they are displayed, and general visual properties. In this
3 min read
How to Connect Paired Points with Lines in Scatterplot in ggplot2 in R?
In this article, we will discuss how to connect paired points in scatter plot in ggplot2 in R Programming Language. Scatter plots help us to visualize the change in two more categorical clusters of data. Sometimes, we need to work with paired quantitative variables and try to visualize their relatio
2 min read
How to highlight text inside a plot created by ggplot2 using a box in R?
In this article, we will discuss how to highlight text inside a plot created by ggplot2 using a box in R programming language. There are many ways to do this, but we will be focusing on one of the ways. We will be using the geom_label function present in the ggplot2 package in R. This function allow
3 min read
Adding labels, titles, and legends in r
Working with Legends in R using ggplot2
A legend in a plot helps us to understand which groups belong to each bar, line, or box based on its type, color, etc. We can add a legend box in R using the legend() function. These work as guides. The keys can be determined by scale breaks. In this article, we will be working with legends and asso
7 min read
How to Add Labels Directly in ggplot2 in R
Labels are textual entities that have information about the data point they are attached to which helps in determining the context of those data points. In this article, we will discuss how to directly add labels to ggplot2 in R programming language. To put labels directly in the ggplot2 plot we add
5 min read
How to change legend title in ggplot2 in R?
In this article, we will see how to change the legend title using ggplot2 in R Programming. We will use ScatterPlot. For the Data of Scatter Plot, we will pick some 20 random values for the X and Y axis both using rnorm() function which can generate random normal values, and here we have one more pa
3 min read
How to change legend title in R using ggplot ?
A legend helps understand what the different plots on the same graph indicate. They basically provide labels or names for useful data depicted by graphs. In this article, we will discuss how legend names can be changed in R Programming Language. Let us first see what legend title appears by default.
2 min read
Customizing Visual Appearance
Handling Data Subsets: Faceting
Grouping Data: Dodge and Position Adjustments