Visualizing
Data with
ggplot2
CREATING THE LAYERS
RAVI PRAKASH JHA
BIOSTATISTICS FACULT Y
DEPARTMENT OF COMMUNIT Y MEDICINE
DR BSA MEDICAL COLLEGE AND HOSPITAL
Outline
Effective data visualization
transforms numbers into a
story, revealing patterns and
insights that words alone
cannot convey.
 What is ggplot2
 Key Concepts
 Basic Structure of a ggplot2 Plot
 Important Functions in ggplot2
 Aesthetic Mappings
 Common Geometric Layers
 Faceting or Sub-setting
 Themes and Customization
 Univariate Graphs
 Hands-On Exercise
 Key Takeaways for the Next Talk
What is ggplot2?
ggplot2 is a data visualization package in R that allows the creation of complex multi-
layered visualizations using a coherent grammar.
Advantages:
• Highly customizable
• Elegant and intuitive syntax
• Wide variety of plot types
Why Use ggplot2?
• Consistent and powerful framework
• Facilitates easy exploratory data analysis (EDA)
Key Concepts
Grammar of Graphics: ggplot2 uses a layered grammar to build plots step
by step.
Core Components:
• Data: The dataset being visualized.
• Aesthetic Mappings (aes): Define how variables map to visual properties like position, color,
or size.
• Geometric Objects (geoms): Visual representations of data, like points, lines, or bars.
• Statistical Transformations (stats): Summarize or transform data for plotting (e.g., counts
for bar charts).
• Faceting: Splits the data into subsets and creates separate plots for each subset.
• Themes: Control the appearance of non-data elements.
Basic Structure of a ggplot2 Plot
• Load the data
• Convert to
dataframe
Data
Frame
• Initialize plot
• Mention data
ggplot()
• Define
aesthetic
mapping
• Map axis to
variables
aes()
• Geometric
objects-point,
line, bar etc
• Add layers,
facets,
themes, labels
with “+”
geom_
<layer
name>()
Important Functions in ggplot2
• ggplot(): Initialize the plot.
• aes(): Map variables to aesthetics.
• geom_*(): Add geometric layers (e.g., points,
lines, bars).
• facet_*(): Split data into panels.
• scale_*(): Modify scales for colors, sizes, or
axes.
• theme_*(): Customize non-data elements like
background and text.
Core
Functions
:
Aesthetic Mappings
•What Can Be Mapped?
• Position: X and Y axes
• Color, size, shape, and fill
•Dynamic Mappings: Map data variables to visual properties for
deeper insights.
Common Geometric Layers
•Overview of Common Layers:
•geom_point(): Scatterplots
•geom_line(): Line graphs
•geom_bar(): Bar charts
•geom_histogram(): Histograms
•geom_boxplot(): Boxplots
•Geom_smooth(): Best Fit
Faceting or Sub-setting
•Purpose: Break down data by categories into separate plots.
•Faceting Functions:
•facet_wrap(): Wrap panels in a grid.
•facet_grid(): Arrange panels by row and column.
Themes and Customization
•Available Themes:
•theme_gray(): Default theme.
•theme_minimal(): Simple and clean.
•theme_classic(): Traditional appearance.
•Customization Options:
•Titles and axis labels
•Legend placement and styling
•Background colors and grid lines
Univariate Graphs
Univariate graphs plot the
distribution of data from a
single variable. The variable
can be categorical (e.g.,
race, sex, political
affiliation) or quantitative
(e.g., age, weight, income).
Qualitative/ Categorical
1) Bar Chart
2) Pie Chart
3) Tree Map
4) Waffle Chart
Quantitative
1) Histogram
2) Kernel Density Plot
3) Dot Chart
Hands-On
Exercise
in
R Studio
Key Takeaways for the Next Talk
What We Have Learnt?
• Hands-on session using ggplot2
• Building layered plots with ggplot2.
• Customizing themes and aesthetics.
• Exploring advanced visualizations (e.g., faceted plots, annotations).
Preparation for the Next Talk:
• Multivariate Graphs.

Visualization_Data with ggplot2_Day 2.pptx

  • 1.
    Visualizing Data with ggplot2 CREATING THELAYERS RAVI PRAKASH JHA BIOSTATISTICS FACULT Y DEPARTMENT OF COMMUNIT Y MEDICINE DR BSA MEDICAL COLLEGE AND HOSPITAL
  • 2.
    Outline Effective data visualization transformsnumbers into a story, revealing patterns and insights that words alone cannot convey.  What is ggplot2  Key Concepts  Basic Structure of a ggplot2 Plot  Important Functions in ggplot2  Aesthetic Mappings  Common Geometric Layers  Faceting or Sub-setting  Themes and Customization  Univariate Graphs  Hands-On Exercise  Key Takeaways for the Next Talk
  • 3.
    What is ggplot2? ggplot2is a data visualization package in R that allows the creation of complex multi- layered visualizations using a coherent grammar. Advantages: • Highly customizable • Elegant and intuitive syntax • Wide variety of plot types Why Use ggplot2? • Consistent and powerful framework • Facilitates easy exploratory data analysis (EDA)
  • 4.
    Key Concepts Grammar ofGraphics: ggplot2 uses a layered grammar to build plots step by step. Core Components: • Data: The dataset being visualized. • Aesthetic Mappings (aes): Define how variables map to visual properties like position, color, or size. • Geometric Objects (geoms): Visual representations of data, like points, lines, or bars. • Statistical Transformations (stats): Summarize or transform data for plotting (e.g., counts for bar charts). • Faceting: Splits the data into subsets and creates separate plots for each subset. • Themes: Control the appearance of non-data elements.
  • 5.
    Basic Structure ofa ggplot2 Plot • Load the data • Convert to dataframe Data Frame • Initialize plot • Mention data ggplot() • Define aesthetic mapping • Map axis to variables aes() • Geometric objects-point, line, bar etc • Add layers, facets, themes, labels with “+” geom_ <layer name>()
  • 6.
    Important Functions inggplot2 • ggplot(): Initialize the plot. • aes(): Map variables to aesthetics. • geom_*(): Add geometric layers (e.g., points, lines, bars). • facet_*(): Split data into panels. • scale_*(): Modify scales for colors, sizes, or axes. • theme_*(): Customize non-data elements like background and text. Core Functions :
  • 7.
    Aesthetic Mappings •What CanBe Mapped? • Position: X and Y axes • Color, size, shape, and fill •Dynamic Mappings: Map data variables to visual properties for deeper insights.
  • 8.
    Common Geometric Layers •Overviewof Common Layers: •geom_point(): Scatterplots •geom_line(): Line graphs •geom_bar(): Bar charts •geom_histogram(): Histograms •geom_boxplot(): Boxplots •Geom_smooth(): Best Fit
  • 9.
    Faceting or Sub-setting •Purpose:Break down data by categories into separate plots. •Faceting Functions: •facet_wrap(): Wrap panels in a grid. •facet_grid(): Arrange panels by row and column.
  • 10.
    Themes and Customization •AvailableThemes: •theme_gray(): Default theme. •theme_minimal(): Simple and clean. •theme_classic(): Traditional appearance. •Customization Options: •Titles and axis labels •Legend placement and styling •Background colors and grid lines
  • 11.
    Univariate Graphs Univariate graphsplot the distribution of data from a single variable. The variable can be categorical (e.g., race, sex, political affiliation) or quantitative (e.g., age, weight, income). Qualitative/ Categorical 1) Bar Chart 2) Pie Chart 3) Tree Map 4) Waffle Chart Quantitative 1) Histogram 2) Kernel Density Plot 3) Dot Chart
  • 12.
  • 13.
    Key Takeaways forthe Next Talk What We Have Learnt? • Hands-on session using ggplot2 • Building layered plots with ggplot2. • Customizing themes and aesthetics. • Exploring advanced visualizations (e.g., faceted plots, annotations). Preparation for the Next Talk: • Multivariate Graphs.