0% found this document useful (0 votes)
3 views17 pages

Data Visualization and Beautifying The Charts and Plots Using Ggplot

The document outlines an experiment on data visualization using R and ggplot2, focusing on creating various types of plots such as bar charts, histograms, pie charts, and scatter plots. It explains the theory behind ggplot2, the use of R libraries for enhancing visualizations, and provides code examples for different types of plots. The document also includes tasks for loading datasets, aggregating data, and visualizing student marks with appropriate plots.

Uploaded by

chotaimanav46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views17 pages

Data Visualization and Beautifying The Charts and Plots Using Ggplot

The document outlines an experiment on data visualization using R and ggplot2, focusing on creating various types of plots such as bar charts, histograms, pie charts, and scatter plots. It explains the theory behind ggplot2, the use of R libraries for enhancing visualizations, and provides code examples for different types of plots. The document also includes tasks for loading datasets, aggregating data, and visualizing student marks with appropriate plots.

Uploaded by

chotaimanav46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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

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:

1. How R libraries are used in beautification process of Data Visualization?

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.

 ggplot2 is best for static visualizations with deep customization.

 plotly is ideal for adding interactivity to plots, converting static ggplot2 charts into interactive ones.

 leaflet is perfect for geospatial visualizations and creating interactive maps.


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

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.

3. Mention the syntax to load data set in R?

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()

3. read_excel() (from readxl package)

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:-

# Example data for Bar Chart


Category <- c("A", "B", "C", "D", "E")
Values <- c(23, 45, 56, 78, 34)

# Bar Chart in base R


barplot(Values, names.arg = Category, col = "skyblue",
main = "Bar Chart Example", xlab = "Category", ylab =

"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:-

# Example data for Histogram


set.seed(123)
data <- rnorm(100, mean = 50, sd = 10)

# 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:-

# Example data for Pie Chart


Category <- c("A", "B", "C", "D")
Values <- c(15, 30, 45, 10)

# Pie Chart in base R


pie(Values, labels = Category, col = rainbow(length(Values)),
main = "Pie Chart Example")

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:-

# Example data for Scatter Plot


x <- rnorm(100)
y <- rnorm(100)

# Scatter Plot in base R


plot(x, y, col = "blue", pch = 16,
main = "Scatter Plot Example", xlab = "X Value", ylab = "Y Value")

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.

TASK-I : Load the dataset


TASK-II: Aggregate and determine the total marks for each component, i.e., ESE, CSE, IA, TW, and
Viva. TASK-III: Map each of the components into 50,20,30,25 and 25 marks, respectively
TASK-IV: Draw the scatterplot of each component(ESE, CSE, IA, TW, and Viva)individually
TASK-V: Draw the box plot for each component (ESE, CSE, IA, TW, and Viva)individually
TASK-VI: Draw Bar chart for each component (ESE, CSE, IA, TW, and Viva) individually
TASK-VII: Create Pie chart for any two component
TASK-VIII: Absolute Grading to be given to each student, as 90%+-->"O" Grade, 80%-90%--> "A+" Grade,
70%-80%--> "A" grade, etc.Plot the bar chart showing the number of students for each grade obtained in
Q.13

Code:-

# Load necessary libraries for data visualization and manipulation

library(ggplot2) # For creating elegant data visualizations

library(dplyr) # For data manipulation

# TASK-I: Load the dataset

# Assuming the dataset is in CSV format and named "student_marks.csv"


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

data <- read.csv("student_marks.csv") # Replace with your dataset file path

head(data) # View the first few rows of the dataset

# 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

data$total_marks <- data$ESE + data$CSE + data$IA + data$TW + data$Viva

# Display the summary of the total marks

summary(data$total_marks)

# TASK-III: Map each of the components into specific 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)

# Scatter plot for ESE vs CSE

ggplot(data, aes(x = ESE, y = CSE)) +

geom_point(color = 'blue') +

ggtitle("Scatter Plot: ESE vs CSE") +


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("ESE Marks") +

ylab("CSE Marks") +

theme_minimal()

# Repeat for IA, TW, and Viva

ggplot(data, aes(x = ESE, y = IA)) +

geom_point(color = 'green') +

ggtitle("Scatter Plot: ESE vs IA") +

xlab("ESE Marks") +

ylab("IA Marks") +

theme_minimal()

ggplot(data, aes(x = ESE, y = TW)) +

geom_point(color = 'red') +

ggtitle("Scatter Plot: ESE vs TW") +

xlab("ESE Marks") +

ylab("TW Marks") +

theme_minimal()

ggplot(data, aes(x = ESE, y = 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

geom_point(color = 'purple') +

ggtitle("Scatter Plot: ESE vs Viva") +

xlab("ESE Marks") +

ylab("Viva Marks") +

theme_minimal()

# TASK-V: Draw the Box Plot for each component (ESE, CSE, IA, TW, Viva)

# Box plot for ESE

ggplot(data, aes(x = factor(1), y = ESE)) +

geom_boxplot(fill = 'lightblue') +

ggtitle("Box Plot: ESE Marks") +

ylab("ESE Marks") +

theme_minimal()

# Repeat for other components (CSE, IA, TW, Viva)

ggplot(data, aes(x = factor(1), y = CSE)) +

geom_boxplot(fill = 'lightgreen') +

ggtitle("Box Plot: CSE Marks") +

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

ggplot(data, aes(x = factor(1), y = IA)) +

geom_boxplot(fill = 'lightyellow') +

ggtitle("Box Plot: IA Marks") +

ylab("IA Marks") +

theme_minimal()

ggplot(data, aes(x = factor(1), y = TW)) +

geom_boxplot(fill = 'lightcoral') +

ggtitle("Box Plot: TW Marks") +

ylab("TW Marks") +

theme_minimal()

ggplot(data, aes(x = factor(1), y = Viva)) +

geom_boxplot(fill = 'lightpink') +

ggtitle("Box Plot: Viva Marks") +

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

# Bar chart for ESE

ggplot(data, aes(x = factor(Student_ID), y = ESE)) +

geom_bar(stat = 'identity', fill = 'steelblue') +

ggtitle("Bar Chart: ESE Marks by Student") +

xlab("Student ID") +

ylab("ESE Marks") +

theme_minimal()

# Repeat for other components (CSE, IA, TW, Viva)

ggplot(data, aes(x = factor(Student_ID), y = CSE)) +

geom_bar(stat = 'identity', fill = 'lightgreen') +

ggtitle("Bar Chart: CSE Marks by Student") +

xlab("Student ID") +

ylab("CSE Marks") +

theme_minimal()

ggplot(data, aes(x = factor(Student_ID), y = IA)) +

geom_bar(stat = 'identity', fill = 'lightyellow') +

ggtitle("Bar Chart: IA Marks by Student") +

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()

ggplot(data, aes(x = factor(Student_ID), y = TW)) +

geom_bar(stat = 'identity', fill = 'lightcoral') +

ggtitle("Bar Chart: TW Marks by Student") +

xlab("Student ID") +

ylab("TW Marks") +

theme_minimal()

ggplot(data, aes(x = factor(Student_ID), y = Viva)) +

geom_bar(stat = 'identity', fill = 'lightpink') +

ggtitle("Bar Chart: Viva Marks by Student") +

xlab("Student ID") +

ylab("Viva Marks") +

theme_minimal()

# TASK-VII: Create a Pie Chart for any two components

# Pie chart for distribution of ESE and CSE marks (example)

marks_summary <- table(c(sum(data$ESE), sum(data$CSE)))


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

pie(marks_summary, labels = c("ESE", "CSE"), col = c("lightblue",

"lightgreen"), main = "Pie Chart: ESE vs CSE")

# TASK-VIII: Absolute Grading Based on Total Marks

data$percentage <- (data$total_marks / 150) * 100 # Assuming total max marks are 150
(50+20+30+25+25)

data$Grade <- cut(data$percentage,

breaks = c(0, 70, 80, 90, 100),

labels = c("B", "A", "A+", "O"),

right = FALSE)

# Display the grades assigned to each student

table(data$Grade)

# Bar chart for grades

ggplot(data, aes(x = Grade)) +

geom_bar(fill = 'lightcoral') +

ggtitle("Bar Chart: Number of Students by Grade") +

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

You might also like