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:
- Install and load the ggplot2 package.
- Understand the Grammar of Graphics.
- Create different types of plots using ggplot2.
- Customize graphs using colors, labels, themes and facets.
- Compare multiple variables graphically.
- 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
| Function | Graph 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
| Aesthetic | Purpose |
|---|---|
| x | X-axis |
| y | Y-axis |
| color | Color of objects |
| fill | Fill color |
| shape | Shape of points |
| size | Size of points |
| alpha | Transparency |
Common Themes
| Theme | Description |
|---|---|
| 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.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
|---|---|---|---|---|
| 5.1 | 3.5 | 1.4 | 0.2 | Setosa |
| 4.9 | 3.0 | 1.4 | 0.2 | Setosa |
| 4.7 | 3.2 | 1.3 | 0.2 | Setosa |
| ... | ... | ... | ... | ... |
Algorithm
- Load ggplot2 package.
- Load iris dataset.
- Create scatter plot.
- Create colored scatter plot.
- Create histogram.
- Create bar chart.
- Create box plot.
- Create density plot.
- Create line graph.
- Add regression line.
- Apply themes.
- Create faceted plots.
- Interpret the graphs.
Program
1. Load Library
library(ggplot2)
data(iris)
head(iris)
2. Simple Scatter Plot
3. Scatter Plot with Colors
4. Scatter Plot with Shapes
5. Scatter Plot with Regression Line
6. Histogram
7. Bar Chart
8. Box Plot
9. Density Plot
10. Violin Plot
11. Line Graph
12. Area Plot
13. Faceting
14. Customized Graph
15. Different Themes
Classic Theme
Minimal Theme
Dark Theme
16. Flip Coordinates
17. Save Graph
ggsave("irisplot.png")
Important ggplot2 Functions
| Function | Purpose |
|---|---|
| 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
Post a Comment