Data Visualization and Beautifying The Charts and Plots Using Ggplot
Data Visualization and Beautifying The Charts and Plots Using Ggplot
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
Aim:Data Visualization and beautifying the charts and plots using ggplot
IDE:R Studio
Theory:
ggplot2 package in R Programming Language also termed as Grammar of Graphics is a free, open-source,
and easy-to-use visualization package widely used in R. It is the most powerful visualization package written
by Hadley Wickham.
It includes several layers on which it is governed. The layers are as follows:
Building Blocks of layers with the grammar of graphics
Data: The element is the data set itself
Aesthetics: The data is to map onto the Aesthetics attributes such as x-axis, y-axis, color, fill, size,
labels, alpha, shape, line width, line type
Geometrics: How our data being displayed using point, line, histogram, bar, boxplot
Facets: It displays the subset of the data using Columns and rows
Statistics: Binning, smoothing, descriptive, intermediate
Coordinates: the space between data and display using Cartesian, fixed, polar, limits
Themes: Non-data link
Pre Lab:
R libraries play a crucial role in enhancing the aesthetics and functionality of data visualizations. They provide
powerful tools and techniques to create visually appealing, interactive, and informative plots. R’s extensive
ecosystem of libraries allows users to customize almost every aspect of their visualizations, such as colors,
fonts, shapes, and interactive elements.
2. Explore three libraries in R which is used for Data visualization purpose, Explain it in short.
plotly is ideal for adding interactivity to plots, converting static ggplot2 charts into interactive ones.
Each of these libraries plays a crucial role in data visualization, depending on the nature of the data and the type
of interactivity you need.
In R, there are several ways to load datasets, depending on the format of the data (e.g., CSV, Excel, or built-in
datasets). Below are the most common methods for loading datasets in R:
1. data()
2. read.csv()
4. read.table()
5. readRDS()
Program:
Write the program (R script) that beautifies the charts and plots using ggplot for the given visuals:
1. Bar Chart
Code:-
"Values") output:-
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
2. Histogram
Code:-
# Histogram in base R
hist(data, breaks = 10, col = "lightblue", border = "black",
main = "Histogram Example", xlab = "Value", ylab =
"Frequency") output:-
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
3. Pie Chart
Code:-
output:-
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
4. Scatter Plot
Code:-
output:-
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
Observation :
General Observations:
Customization: Each plot allows customization of colors, titles, labels, and other aesthetic elements,
making the visualizations clear and easy to understand.
Simplicity: These visualizations are created using base R functions, ensuring simplicity and
accessibility without requiring external libraries.
Clarity: Proper use of labels, colors, and titles enhances the clarity of each visualization, allowing users
to easily interpret the data presented.
Conclusion:
The base R functions for data visualization—barplot(), hist(), pie(), and plot()—are effective
tools for creating simple, clear, and informative visualizations.
1. Bar Chart: Useful for visualizing categorical data and comparing quantities
across categories. It allows easy comparison of different categories using bars.
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
2. Histogram: Ideal for showing the distribution of a continuous variable. It helps identify
patterns like normal distribution, skewness, or outliers.
3. Pie Chart: Best for showing proportions of categories within a whole. However, pie
charts can become less effective with too many categories and should be used
sparingly.
4. Scatter Plot: Valuable for examining relationships between two continuous variables.
It helps identify correlations, clusters, or trends in data.
Post Lab:(Write code for each program and mention output screenshot here)
Write R script that demonstrates the working of R libraries for Data visualization Pupose:
Load the Attached Data set in R studio ,Complete following TASK for the same.
Code:-
# TASK-II: Aggregate and Determine the Total Marks for each component
# Add a new column 'total_marks' to calculate the sum of ESE, CSE, IA, TW, and Viva
summary(data$total_marks)
# Map the columns into 50, 20, 30, 25, and 25 marks respectively
marks_mapping <- c(ESE = 50, CSE = 20, IA = 30, TW = 25, Viva = 25)
print(marks_mapping)
# TASK-IV: Draw the Scatter Plot for each component (ESE, CSE, IA, TW, and Viva)
geom_point(color = 'blue') +
xlab("ESE Marks") +
ylab("CSE Marks") +
theme_minimal()
geom_point(color = 'green') +
xlab("ESE Marks") +
ylab("IA Marks") +
theme_minimal()
geom_point(color = 'red') +
xlab("ESE Marks") +
ylab("TW Marks") +
theme_minimal()
geom_point(color = 'purple') +
xlab("ESE Marks") +
ylab("Viva Marks") +
theme_minimal()
# TASK-V: Draw the Box Plot for each component (ESE, CSE, IA, TW, Viva)
geom_boxplot(fill = 'lightblue') +
ylab("ESE Marks") +
theme_minimal()
geom_boxplot(fill = 'lightgreen') +
ylab("CSE Marks") +
theme_minimal()
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
geom_boxplot(fill = 'lightyellow') +
ylab("IA Marks") +
theme_minimal()
geom_boxplot(fill = 'lightcoral') +
ylab("TW Marks") +
theme_minimal()
geom_boxplot(fill = 'lightpink') +
ylab("Viva Marks") +
theme_minimal()
# TASK-VI: Draw Bar Chart for each component (ESE, CSE, IA, TW, Viva)
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
xlab("Student ID") +
ylab("ESE Marks") +
theme_minimal()
xlab("Student ID") +
ylab("CSE Marks") +
theme_minimal()
xlab("Student ID") +
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
ylab("IA Marks") +
theme_minimal()
xlab("Student ID") +
ylab("TW Marks") +
theme_minimal()
xlab("Student ID") +
ylab("Viva Marks") +
theme_minimal()
data$percentage <- (data$total_marks / 150) * 100 # Assuming total max marks are 150
(50+20+30+25+25)
right = FALSE)
table(data$Grade)
geom_bar(fill = 'lightcoral') +
xlab("Grade") +
ylab("Number of Students") +
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
theme_minimal()
output:-
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195
Marwadi University
Faculty of Technology
Department of Information and Communication Technology
Subject: Introduction to R Aim:Data Visualizationand beautifying the charts and plots using ggplot
and R Studio(01CT0106)
Experiment: 09 Date:29/03/25 Enrollment No:92400133195