Exploring Data Visualization Using the ggplot2 Package in R

 

Experiment

Exploring Data Visualization Using the ggplot2 Package in R

Aim

To explore the ggplot2 package in R for creating, customizing, and interpreting various statistical graphs using sample data.


Objectives

After completing this experiment, the student will be able to:

  1. Install and load the ggplot2 package.
  2. Understand the Grammar of Graphics.
  3. Create different types of plots using ggplot2.
  4. Customize graphs using colors, labels, themes and facets.
  5. Compare multiple variables graphically.
  6. Interpret the graphical representation of data.

Software Requirements

  • R
  • RStudio
  • ggplot2 package

Theory

Introduction

ggplot2 is one of the most powerful visualization libraries available in R.

It was developed by Hadley Wickham based on the Grammar of Graphics proposed by Leland Wilkinson.

Unlike the traditional plotting functions in R, ggplot2 builds a graph layer by layer.

A graph generally consists of

  • Data
  • Aesthetic mappings
  • Geometric objects
  • Statistical transformations
  • Coordinate systems
  • Facets
  • Themes

This layered approach makes ggplot2 highly flexible and easy to customize.


Grammar of Graphics

Every ggplot graph has the following structure.

ggplot(data,aes(...))+
geom_xxx()

where

  • data → Dataset
  • aes() → Maps variables to visual properties
  • geom_xxx() → Type of graph

Common Geometric Objects

FunctionGraph Produced
geom_point()Scatter Plot
geom_line()Line Graph
geom_bar()Bar Chart
geom_col()Column Chart
geom_histogram()Histogram
geom_boxplot()Box Plot
geom_density()Density Plot
geom_smooth()Regression/Trend Line
geom_area()Area Plot
geom_violin()Violin Plot

Common Aesthetic Mappings

AestheticPurpose
xX-axis
yY-axis
colorColor of objects
fillFill color
shapeShape of points
sizeSize of points
alphaTransparency

Common Themes

ThemeDescription
theme_gray()Default
theme_bw()Black & White
theme_light()Light
theme_dark()Dark
theme_classic()Classic
theme_minimal()Minimal

Installation

install.packages("ggplot2")

Load Package

library(ggplot2)

Dataset

Use the built-in iris dataset.

data(iris)

head(iris)

Sample Data

Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2Setosa
4.93.01.40.2Setosa
4.73.21.30.2Setosa
...............

Algorithm

  1. Load ggplot2 package.
  2. Load iris dataset.
  3. Create scatter plot.
  4. Create colored scatter plot.
  5. Create histogram.
  6. Create bar chart.
  7. Create box plot.
  8. Create density plot.
  9. Create line graph.
  10. Add regression line.
  11. Apply themes.
  12. Create faceted plots.
  13. Interpret the graphs.

Program

1. Load Library

library(ggplot2)

data(iris)

head(iris)

2. Simple Scatter Plot

ggplot(iris,
aes(x=Sepal.Length,
y=Petal.Length))+

geom_point()



3. Scatter Plot with Colors

ggplot(iris,
aes(x=Sepal.Length,
y=Petal.Length,
color=Species))+

geom_point(size=3)



4. Scatter Plot with Shapes

ggplot(iris,
aes(x=Sepal.Length,
y=Petal.Length,
shape=Species,
color=Species))+

geom_point(size=4)



5. Scatter Plot with Regression Line

ggplot(iris,
aes(Sepal.Length,
Petal.Length))+

geom_point(color="blue")+

geom_smooth(method="lm",
color="red")



6. Histogram

ggplot(iris,
aes(x=Sepal.Length))+

geom_histogram(
fill="skyblue",
color="black",
bins=10)



7. Bar Chart

ggplot(iris,
aes(x=Species,
fill=Species))+

geom_bar()



8. Box Plot

ggplot(iris,
aes(x=Species,
y=Sepal.Length,
fill=Species))+

geom_boxplot()



9. Density Plot

ggplot(iris,
aes(x=Sepal.Length,
fill=Species))+

geom_density(alpha=0.4)



10. Violin Plot

ggplot(iris,
aes(x=Species,
y=Sepal.Length,
fill=Species))+

geom_violin()



11. Line Graph

ggplot(iris,
aes(x=Sepal.Length,
y=Petal.Length))+

geom_line()



12. Area Plot

ggplot(iris,
aes(x=Sepal.Length,
y=Petal.Length))+

geom_area(fill="lightgreen")



13. Faceting

ggplot(iris,
aes(Sepal.Length,
Petal.Length))+

geom_point()+

facet_wrap(~Species)



14. Customized Graph

ggplot(iris,
aes(Sepal.Length,
Petal.Length,
color=Species))+

geom_point(size=4)+

labs(
title="Iris Dataset Visualization",
subtitle="Sepal Length vs Petal Length",
x="Sepal Length (cm)",
y="Petal Length (cm)",
caption="Using ggplot2")+

theme_bw()




15. Different Themes

Classic Theme

ggplot(iris,
aes(Sepal.Length,
Petal.Length))+

geom_point()+

theme_classic()



Minimal Theme

ggplot(iris,
aes(Sepal.Length,
Petal.Length))+

geom_point()+

theme_minimal()



Dark Theme

ggplot(iris,
aes(Sepal.Length,
Petal.Length))+

geom_point()+

theme_dark()



16. Flip Coordinates

ggplot(iris,
aes(Species,
Sepal.Length,
fill=Species))+

geom_boxplot()+

coord_flip()



17. Save Graph

ggsave("irisplot.png")

Important ggplot2 Functions

FunctionPurpose
ggplot()Initializes the graph
aes()Maps variables
geom_point()Scatter plot
geom_line()Line graph
geom_bar()Bar chart
geom_histogram()Histogram
geom_boxplot()Box plot
geom_density()Density plot
geom_violin()Violin plot
geom_area()Area chart
geom_smooth()Regression line
facet_wrap()Multiple panels
labs()Add titles and labels
theme_bw()Black & white theme
theme_classic()Classic theme
theme_minimal()Minimal theme
coord_flip()Flip axes
ggsave()Save plot

Result

The ggplot2 package was successfully explored to create and customize a variety of statistical graphs. Different geometric objects, aesthetic mappings, themes, faceting techniques, and plot customizations were implemented. The experiment demonstrated the flexibility of ggplot2 in producing high-quality visualizations for exploratory data analysis.

Comments

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1