SlideShare a Scribd company logo
1 of 54
Data Visualization in R with ggplot2::
Meg Hartwick, PhD
March 3rd , 2021
Workshop Overview
Materials Available at:
https://github.com/meghartwick/ggplot2-Workshop
o PowerPoint Slides
o HTML
o https://rpubs.com/meghartwick/733550
o Notebook and Code
Data Visualization in R with
ggplot2::
Meg Hartwick, PhD
March 3rd , 2021
Flow
I. Foundation - Concept Review
II. Structure – ggplot2:: syntax
III. Application – Sketch to Story
10
0,
00
0.
00
1
0
,
0
0
0
.
0
1
0
,
0
0
0
. 0
01
0
0
. 0
0
1
0
.
0
0
2
0
0
0
2 2
0 0
1 2
0 0
*
*
*
*
*
*
keypoint
here
T
i
m
e
L
o
g
(
V
a
l
u
e
)
Very Catchy Title
ggplot(…)
Workshop Overview
Workshop Overview
Why do you usually make graphics?
1. For your own use.
2. Informal sharing with colleagues.
3. For formal presentations or publication.
Workshop Overview
How do you usually make graphics?
1. Quick and accessible (excel).
2. Statistical software (eg: JMP, SAS).
3. Tableau, base R, some ggplot2.
4. R, Python and/or others
Workshop Overview
What makes for a good graphic?
1. Really busy with every detail in text…
2. Flashy plotting…
3. Aclear message that tells a story…
Workshop Overview
What makes for a good graphic?
1. Really busy with every detail in text…
2. Flashy plotting…
3. Aclear message that tells a story…
Workshop Overview
What makes for a good graphic?
1. Really busy with every detail in text…
2. Flashy plotting…
3. Aclear message that tells a story… 100,000.0
0
10,000.
00
1,000.0
0
100.0
0
10.00
2000 2020
**
*
***
key point
here
2010
Time
Log(Value)
Very Catchy Title
Workshop Overview
10
0,
00
0.
00
1
0
,
0
0
0
.
0
0
1
,
0
0
0
.
0
0
1
0
0
.
0
0
1
0
.
0
0
2
0
0
0
2
0
2
0
*
*
*
*
*
*
keypoint
here
2
0
1
0
T
i
m
e
Log(Va
lue)
Very Catchy Title
ggplot(…)
Today’s Goal
o Understand how to develop graphics that:
• Effectively tell a story
• In ggplot2 (and some tidyverse)
• Are refined or highly refined.
Foundations and Concepts Refresher
1. Data
• Structure
- Continuous
- Discrete
• Format
- Wide
- Long
2. GraphAttributes
• Dimension
• Axes scale
• Symbols, color schemes etc.
3. Key Elements
• Title
• Axes labels
• Embedded comments
1. Concepts
2. Tidyverse
3. ggplot2::
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
**
*
***
key point here
2010
Time
Log(Value)
Very Catchy Title
I. Foundation
I. Foundation
1. Concepts
2. Tidyverse
3. ggplot2::
Tidyverse
o Originally ‘Hadleyverse’
o Collection of R packages with shared:
• Philosophy
• Structure
• Syntax
o Package ‘piping’(%>%)
• Functions act as verbs
o Over 27 packages including:
• readr – importing data
• dplyr – data cleaning
• ggplot2 – data visualization
• lubridate – working with dates
• stringr – working with strings
I. Foundation
1. Concepts
2. Tidyverse
3. ggplot2::
ggplot2::
o Developed by Hadley Wickham
o 10+ years old
o Readability > base R plot
o Over 84 extensions
• ggtext
• ggthemes
• gganimate
• esquisse
o ‘Grammer of Graphics’
o 8 main class of functions
• align with key elements and attributes
of graphics communication
ggplot2:: Syntax
Foundations
1. Data
• Structure
- Continuous
- Discrete
• Format
- Wide
- Long
2. Graph attributes
• Dimension
• Axes scale
• Symbols, colors etc.
3. Key elements
• Title
• Axes labels
• Embedded comments
II. Structure
1. Data
2. Function
3. Coordinates
4. Mapping
5. Geometries
6. Scales
7. Facets
8. Themes
II. Structure
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
ggplot2::
o Slides
• Code - How the element is called
• Considerations – some assumptions to keep in mind
• Arguments – How, when, what to use
o HTML
• Follow along with the code and graphics
o Notebook and Code
• Run code as source code or chunks
8. Themes
II. Structure
1. Data
2. Function
4. Mapping
5. Geometries
6. Scales
7. Facets
df %>%
3. Coordinates
8. Themes
II. Structure
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
o Discrete or Continuous data for coordinates
o Discrete or Continuous metadata for mapping
o Missing Data etc.
o Tidyverse can be really useful here
o data transformations
o df reshaping
o Structure check
- skimr::skim()
• Arguments
df %>%
• Considerations
o Date can be piped to function or called within
skimr::skim(df)
df %>% dplyr::filter() %>% dplyr::pivot()
8. Themes
II. Structure
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
o Discrete or Continuous data for coordinates
o Discrete or Continuous metadata for mapping
o Missing Data etc.
o Tidyverse can be really useful here
o data transformations
- skimr::skim()
• Arguments
df %>%
• Considerations
o df reshaping Foundation: Graph Data
o Structure check
o Date can be piped to function or called within
skimr::skim(df)
df %>% dplyr::filter() %>% dplyr::pivot()
8. Themes
II. Structure
df %>%
ggplot(…) +
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
8. Themes
II. Structure
o Data =
o Coordinates =
o Mapping =
ggplot(df, aes()) + …
ggplot(df) +
ggplot() +
df %>% ggplot(., aes()) +
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
ggplot(…) +
• Consideration
o Calls the plot object
o Best placement of df for plot development
• Arguments
8. Themes
II. Structure
1. Data
2. Function
3. Coordinates
4. Mapping
5. Geometries
6. Scales
7. Facets
df %>%
ggplot(., aes(x = x, y = y, …)) +
8. Themes
II. Structure
o Data positions for plot
o Not necessary to specify here, but must be supplied in plot
layers
o Continuous Data
- Coordinate according to the data
o Discrete Data
- At 1, 2, 3 etc. on axis
- Alphabetical for character class
- Level for factors class
4. Mapping
6. Scales
7. Facets
1. Data
2. Function
3. Coordinates
5. Geometries
ggplot(., aes(x = x, y = y, …)) +
• Considerations
8. Themes
II. Structure
o x =
o y =
o positional, not necessary to specify
o for geometries that use count, y not accepted
4. Mapping
6. Scales
7. Facets
1. Data
2. Function
8. Themes
3. Coordinates
5. Geometries
ggplot(., aes(x = x, y = y, …)) +
• Arguments
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Log(Value)
II. Structure
df %>%
ggplot(., aes(x = x, y = y, *= var1) +
1. Data
2. Function
3. Coordinates
4. Mapping
5. Geometries
6. Scales
7. Facets
8. Themes
II. Structure
o Variables are mapped to visual properties (aesthetics)
o Choosing the aesthetic (*)
- Color
- Fill
- Size
- Shape
- Linetype
- Transparency
o Is the Continuous or Discrete?
o What are you mapping to?
4. Mapping
6. Scales
7. Facets
1. Data
2. Function
3. Coordinates
5. Geometries
ggplot(., aes(x = x, y = y, * = var1) +
• Considerations
8. Themes
II. Structure
4. Mapping
6. Scales
7. Facets
1. Data
2. Function
8. Themes
3. Coordinates
5. Geometries
ggplot(., aes(x = x, y = y, * = var1) +
• Arguments
o Mappings can be set in ggplot()
- ggplot(.,aes(color = var1))
o Mappings can be set in individual layers
- geom_point(.,aes(color = var1))
o What is outside of the mapping will be interpreted literally
- geom_point(.,aes(color = ‘var1’))
o Levels of the mapping are set in scales, else:
- (.,aes(), color = ‘black’)
II. Structure
1. Data
2. Function
3. Coordinates
4. Mapping
5. Geometries
6. Scales
7. Facets
8. Themes
df %>%
ggplot(., aes(x = x, y = y, * = var1))+
geom_*(…) +
II. Structure
o What kind of visual representations (*) are the best approach?
o Types of Geometry
- Reference Lines
- Barcharts
- Dots and points
- Boxplots
- Heatmaps
- Maps
- Density
- Polygons
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
geom_*(…) +
• Considerations
**
-
-
-
Jitters
Error Bars
Text and Labels
8. Themes
*
***
key point here
II. Structure
o What kind of visual representations (*) are the best approach?
o Types of Geometry
- Reference Lines
- Barcharts
- Dots and points
- Boxplots
- Heatmaps
- Maps
- Density
- Polygons
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
geom_*(…) +
• Considerations
**
*
***
key point here
Foundation: Graph Attributes
-
-
-
Jitters
Error Bars
Text and Labels
8. Themes
II. Structure
o Arguments
• Over 40 individual geoms
• geom_*()
- geom_point()
- geom_hist()
- geom_bar()
- geom_col()
- geom_tile()
• Can build individual mappings within geoms_*()
• Can add multiple geometries as annotation layers
• Know the defaults
geom_*(mapping =, data =, stat = , position = )
4. Mapping
6. Scales
7. Facets
1. Data
2. Function
8. Themes
3. Coordinates
5. Geometries
geom_*(…) +
II. Structure
1. Data
4. Mapping
2. Function
3. Coordinates
5. Geometries
6. Scales
7. Facets
df %>%
ggplot(., aes(x = x, y = y, * = var1))+
geom_*(…) +
scale_*_*(…) +
8. Themes
II. Structure
o Specify Data and Mappings
o Set arguments for coordinates (*)
- x
- y
o Set arguments for mappings (*)
- color
- fill
- alpha
- linetype
- … and many more
o Different calls for discrete and continuous data types
o Commonly used defaults are prebuilt
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
scale_*_*(…) +
• Concept
8. Themes
II. Structure
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
scale_*_*(…) +
• Argument
s
scale_*_*(
name = ,
breaks = ,
values = ,
labels = ,
limits = ,
trans = ,
guide = ,
position = ,
….)
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Log(Value)
II. Structure
o scale_x_continuous(…)
o scale_y_continuous(…)
o scale_x_discrete(…)
o scale_y_discrete(…)
• Pre-Built CustomAxis Scales
o scale_*_log10(...)
o scale_*_reverse(...)
o scale_*_sqrt(...)
o scale_*_datetime(…)
o … and many more
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
scale_*_*(…) +
• Standard Axis Scales
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Log(Value)
II. Structure
o scale_alpha_continuous(…)
o scale_alpha_discrete(…)
• Standard Shape Scales
o scale_shape_continuous(…)
o scale_shape_discrete(…)
• Standard Linetype Scales
o scale_linetype_continuous(…)
o scale_linetype_discrete(…)
• … and many more Standard
• … and many more Pre-Built
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
scale_*_*(…) +
• StandardAlpha Scales
II. Structure
o scale_color_continuous(…)
o scale_fill_continuous(…)
o scale_color_manual(…)
o scale_fill_manual(…)
• Pre-Built Custom Color Scales
o scale_*_brewer(...)
o scale_*_gradient(...)
o scale_*_gradientn(...)
o scale_*_viridis(…)
o … and many more
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
scale_*_*(…) +
• Standard Color Scales
II. Structure
• Standard Scale – specify colors
o scale_color_manual(values = c(‘red’, ‘green’, ‘blue’)
• Pre-Built Custom Color Scales
o scale_*_brewer(
type =,
palette = ,
direction = ,
aesthetics = )
o scale_color_brewer(palete = ‘set2’)
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
5. Geometries
scale_*_*(…) +
3. Coordinates
8. Themes
II. Structure
df %>%
ggplot(., aes(x = x, y = y, * = var1) +
geom_*(….) +
scale_*_*(…) +
facet_*(…)
1. Data
4. Mapping
2. Function
3. Coordinates
5. Geometries
6. Scales
7. Facets
8. Themes
II. Structure
o Highlight levels in data through multiple panels
o facet_wrap(…)
• creates a ribbon of levels
o facet_grid(…)
• creates a matrix of rows and columns of variable combinations
• Arguments
o nrow =,
o ncol =,
o scales = ,
o shrink =,
o labeller = ,
o strip.position = ,
o …)
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
facet_*(…) +
• Concept
II. Structure
df %>%
ggplot(., aes(x = x, y = y, * = var1) +
geom_*(….) +
scale_*_*(…) +
facet_*(…) +
theme(…)
1. Data
4. Mapping
2. Function
3. Coordinates
5. Geometries
6. Scales
7. Facets
8. Themes
II. Structure
o Encompasses ALL the options of ggplot2:: plot ‘Elements’
o 4 main modifiers
- line: all line elements
- rect: all rectangular elements
- text: all text elements
- title: all title elements (including: plot, axes, legends..)
o Pre-built themes available
- theme_bw(…)
- theme_grey(…)
o ggthemes::
- a package with a wider selection of Pre-Built themes
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
3. Coordinates
5. Geometries
theme(…)
• Concept
8. Themes
II. Structure
o Encompasses ALL the options of ggplot2:: plot ‘Elements’
o 4 main modifiers
- line: all line elements
- rect: all rectangular elements
- text: all text elements
- title: all title elements (including: plot, axes, legends..)
o Pre-built themes available
- theme_bw(…)
- theme_grey(…)
o ggthemes::
- a package with a wider selection of Pre-Built themes
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
theme(…)
• Concept
Foundation: Graph Elements
II. Structure
• Argument
o The main modifiers:
- theme(
text* = element_text(),
panel* = element_rect(),
axis* = element_line(),
title* = element_text())
o where * = …
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
theme(…) +
II. Structure
1. Data
4. Mapping
6. Scales
7. Facets
2. Function
8. Themes
3. Coordinates
5. Geometries
theme(…) +
theme(
aspect.ratio,
axis.title,
axis.title.x,
axis.title.x.top,
axis.title.x.bottom,
axis.title.y,
axis.title.y.left,
axis.title.y.right,
axis.text,
axis.text.x,
axis.text.x.top,
axis.text.x.bottom,
axis.text.y,
axis.text.y.left,
axis.text.y.right,
axis.ticks,
axis.ticks.x,
axis.ticks.x.top,
axis.ticks.x.bottom,
axis.ticks.y,
axis.ticks.y.left,
axis.ticks.y.right,
axis.ticks.length,
axis.ticks.length.x,
axis.ticks.length.x.top,
axis.ticks.length.x.bottom,
axis.ticks.length.y,
axis.ticks.length.y.left,
axis.ticks.length.y.right,
axis.line,
axis.line.x,
axis.line.x.top,
axis.line.x.bottom,
axis.line.y,
axis.line.y.left,
axis.line.y.right,
legend.background,
legend.margin,
legend.spacing,
legend.spacing.x,
legend.spacing.y,
legend.key,
legend.key.size,
legend.key.height,
legend.key.width,
legend.text,
legend.text.align,
legend.title,
legend.title.align,
legend.position,
legend.direction,
legend.justification,
legend.box,
legend.box.just,
legend.box.margin,
legend.box.background,
legend.box.spacing,
panel.background,
panel.border,
panel.spacing,
panel.spacing.x,
panel.spacing.y,
panel.grid,
panel.grid.major,
panel.grid.minor,
panel.grid.major.x,
panel.grid.major.y,
panel.grid.minor.x,
panel.grid.minor.y,
panel.ontop,
plot.background,
plot.title,
plot.title.position,
plot.subtitle,
plot.caption,
plot.caption.position,
plot.tag,
plot.tag.position,
plot.margin,
strip.background,
strip.background.x,
axis.ticks.length.x.bottom,
strip.background.x,
strip.background.y,
strip.placement,
strip.text,
strip.text.x,
strip.text.y,
strip.switch.pad.grid,
strip.switch.pad.wrap,
...,)
bar graph
sample
measurements
color is species
dodged
facetted width and length
III. Application
Sketch Story
bar graph
sample
measurements
color is species
dodged
facetted width and length
III. Application
• Facetted Width and Length
• Bargraph
• Trial axis breaks as ‘2010’, 2015’, ‘2020’
• Y axis at 2, 4, 6, 8
• Choose a different palette for species
• Ditch the points, and accompanying
scale
• Dodge position
• Species in italics
• No grey in facet
• New font
Sketch Story
III. Application
• Facetted Width and Length
• Bargraph
• Trial axis breaks as ‘2010’, 2015’, ‘2020’
• Y axis at 2, 4, 6, 8
• Choose a different palette for species
• Ditch the points, and accompanying
scale
• Dodge position
• Species in italics
• No grey in facet
• New font
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Value
Petal Length
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Log(Value)
Petal Width
Sketch Story
III. Application
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Value
Petal Length
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Log(Value)
Petal Width
Sketch Story
III. Application
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Value
Petal Length
100,000.00
10,000.00
1,000.00
100.00
10.00
2000 2020
2010
Time
Log(Value)
Petal Width
Polished Story
III. Application
This is just one round of editing!
What else would you change???
Polished
Troubleshooting
o Class of data: counts or continuous data?
o Layer, layer, layer, order matters
o What are the arguments within the function
• Change the default or consider an alternate
geom/function
o Are you invoking a discrete call on continuous data?
o Did you set color when you meant fill?
o Did you specify the multiple arguments for the same
item?
• The last one will be what is seen, check your code.
o Is the default of the function to use a count transform?
Conclusions
Resources for working in R
o ggplot2.tidyverse/org
o ggplot2-book.org/
o www.r-graph-gallery.com/
o tidytuesday podcast and webpage
o Esquisse and Colors Add-Ins
o Thomas lin Pedersen – ggplot2 – two-part series
o Stackoverflow
Conclusions
10
0,
00
0.
00
1
0
,
0
0
0
.
0
0
1
,
0
0
0
.
0
0
1
0
0
.
0
0
1
0
.
0
0
2
0
0
0
2
0
2
0
*
*
*
*
*
*
keypoint
here
2
0
1
0
T
i
m
e
Log(Va
lue)
Very Catchy Title
ggplot(…)
Today’s Goal
o Understand how to develop graphics that:
• Effectively tell a story
• In ggplot2 (and some tidyverse)
• Are refined or highly refined.
Conclusions
o Github: github.com/meghartwick/
o LinkedIn: /meghan-hartwick-
83291551/
o Twitter: @HartwickMeghan
‘The question in R is not if it can be done, but how.’
Questions?

More Related Content

Similar to ggplotcourse.pptx

SQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLIT
SQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLITSQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLIT
SQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLITchaitalidarode1
 
Web-Scale Graph Analytics with Apache® Spark™
Web-Scale Graph Analytics with Apache® Spark™Web-Scale Graph Analytics with Apache® Spark™
Web-Scale Graph Analytics with Apache® Spark™Databricks
 
Lecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learningLecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learningmy6305874
 
30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature Selection30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature SelectionJames Huang
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to pythonActiveState
 
Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15
Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15
Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15MLconf
 
Lecture_2_Stats.pdf
Lecture_2_Stats.pdfLecture_2_Stats.pdf
Lecture_2_Stats.pdfpaijitk
 
Petrel course Module_1: Import data and management, make simple surfaces
Petrel course Module_1: Import data and management, make simple surfacesPetrel course Module_1: Import data and management, make simple surfaces
Petrel course Module_1: Import data and management, make simple surfacesMarc Diviu Franco
 
NAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing BasicsNAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing Basicspdituri
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
Pandas data transformational data structure patterns and challenges final
Pandas   data transformational data structure patterns and challenges  finalPandas   data transformational data structure patterns and challenges  final
Pandas data transformational data structure patterns and challenges finalRajesh M
 
30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature Selection30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature SelectionJames Huang
 

Similar to ggplotcourse.pptx (20)

Ch1
Ch1Ch1
Ch1
 
SQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLIT
SQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLITSQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLIT
SQL SCIPY STREAMLIT_Introduction to the basic of SQL SCIPY STREAMLIT
 
Web-Scale Graph Analytics with Apache® Spark™
Web-Scale Graph Analytics with Apache® Spark™Web-Scale Graph Analytics with Apache® Spark™
Web-Scale Graph Analytics with Apache® Spark™
 
Lecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learningLecture 1 Pandas Basics.pptx machine learning
Lecture 1 Pandas Basics.pptx machine learning
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
01_intro-cpp.ppt
01_intro-cpp.ppt01_intro-cpp.ppt
01_intro-cpp.ppt
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
 
30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature Selection30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature Selection
 
Migrating from matlab to python
Migrating from matlab to pythonMigrating from matlab to python
Migrating from matlab to python
 
Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15
Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15
Misha Bilenko, Principal Researcher, Microsoft at MLconf SEA - 5/01/15
 
Lecture_2_Stats.pdf
Lecture_2_Stats.pdfLecture_2_Stats.pdf
Lecture_2_Stats.pdf
 
Petrel course Module_1: Import data and management, make simple surfaces
Petrel course Module_1: Import data and management, make simple surfacesPetrel course Module_1: Import data and management, make simple surfaces
Petrel course Module_1: Import data and management, make simple surfaces
 
NAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing BasicsNAPSG 2010 Fire/EMS Conference - Data Sharing Basics
NAPSG 2010 Fire/EMS Conference - Data Sharing Basics
 
2014 july use_r
2014 july use_r2014 july use_r
2014 july use_r
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
Pandas data transformational data structure patterns and challenges final
Pandas   data transformational data structure patterns and challenges  finalPandas   data transformational data structure patterns and challenges  final
Pandas data transformational data structure patterns and challenges final
 
30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature Selection30 分鐘學會實作 Python Feature Selection
30 分鐘學會實作 Python Feature Selection
 
Essentials of R
Essentials of REssentials of R
Essentials of R
 
Data herding
Data herdingData herding
Data herding
 
Data herding
Data herdingData herding
Data herding
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

ggplotcourse.pptx

  • 1. Data Visualization in R with ggplot2:: Meg Hartwick, PhD March 3rd , 2021
  • 2. Workshop Overview Materials Available at: https://github.com/meghartwick/ggplot2-Workshop o PowerPoint Slides o HTML o https://rpubs.com/meghartwick/733550 o Notebook and Code Data Visualization in R with ggplot2:: Meg Hartwick, PhD March 3rd , 2021
  • 3. Flow I. Foundation - Concept Review II. Structure – ggplot2:: syntax III. Application – Sketch to Story 10 0, 00 0. 00 1 0 , 0 0 0 . 0 1 0 , 0 0 0 . 0 01 0 0 . 0 0 1 0 . 0 0 2 0 0 0 2 2 0 0 1 2 0 0 * * * * * * keypoint here T i m e L o g ( V a l u e ) Very Catchy Title ggplot(…) Workshop Overview
  • 4. Workshop Overview Why do you usually make graphics? 1. For your own use. 2. Informal sharing with colleagues. 3. For formal presentations or publication.
  • 5. Workshop Overview How do you usually make graphics? 1. Quick and accessible (excel). 2. Statistical software (eg: JMP, SAS). 3. Tableau, base R, some ggplot2. 4. R, Python and/or others
  • 6. Workshop Overview What makes for a good graphic? 1. Really busy with every detail in text… 2. Flashy plotting… 3. Aclear message that tells a story…
  • 7. Workshop Overview What makes for a good graphic? 1. Really busy with every detail in text… 2. Flashy plotting… 3. Aclear message that tells a story…
  • 8. Workshop Overview What makes for a good graphic? 1. Really busy with every detail in text… 2. Flashy plotting… 3. Aclear message that tells a story… 100,000.0 0 10,000. 00 1,000.0 0 100.0 0 10.00 2000 2020 ** * *** key point here 2010 Time Log(Value) Very Catchy Title
  • 9. Workshop Overview 10 0, 00 0. 00 1 0 , 0 0 0 . 0 0 1 , 0 0 0 . 0 0 1 0 0 . 0 0 1 0 . 0 0 2 0 0 0 2 0 2 0 * * * * * * keypoint here 2 0 1 0 T i m e Log(Va lue) Very Catchy Title ggplot(…) Today’s Goal o Understand how to develop graphics that: • Effectively tell a story • In ggplot2 (and some tidyverse) • Are refined or highly refined.
  • 10. Foundations and Concepts Refresher 1. Data • Structure - Continuous - Discrete • Format - Wide - Long 2. GraphAttributes • Dimension • Axes scale • Symbols, color schemes etc. 3. Key Elements • Title • Axes labels • Embedded comments 1. Concepts 2. Tidyverse 3. ggplot2:: 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 ** * *** key point here 2010 Time Log(Value) Very Catchy Title I. Foundation
  • 11. I. Foundation 1. Concepts 2. Tidyverse 3. ggplot2:: Tidyverse o Originally ‘Hadleyverse’ o Collection of R packages with shared: • Philosophy • Structure • Syntax o Package ‘piping’(%>%) • Functions act as verbs o Over 27 packages including: • readr – importing data • dplyr – data cleaning • ggplot2 – data visualization • lubridate – working with dates • stringr – working with strings
  • 12. I. Foundation 1. Concepts 2. Tidyverse 3. ggplot2:: ggplot2:: o Developed by Hadley Wickham o 10+ years old o Readability > base R plot o Over 84 extensions • ggtext • ggthemes • gganimate • esquisse o ‘Grammer of Graphics’ o 8 main class of functions • align with key elements and attributes of graphics communication
  • 13. ggplot2:: Syntax Foundations 1. Data • Structure - Continuous - Discrete • Format - Wide - Long 2. Graph attributes • Dimension • Axes scale • Symbols, colors etc. 3. Key elements • Title • Axes labels • Embedded comments II. Structure 1. Data 2. Function 3. Coordinates 4. Mapping 5. Geometries 6. Scales 7. Facets 8. Themes
  • 14. II. Structure 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries ggplot2:: o Slides • Code - How the element is called • Considerations – some assumptions to keep in mind • Arguments – How, when, what to use o HTML • Follow along with the code and graphics o Notebook and Code • Run code as source code or chunks 8. Themes
  • 15. II. Structure 1. Data 2. Function 4. Mapping 5. Geometries 6. Scales 7. Facets df %>% 3. Coordinates 8. Themes
  • 16. II. Structure 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries o Discrete or Continuous data for coordinates o Discrete or Continuous metadata for mapping o Missing Data etc. o Tidyverse can be really useful here o data transformations o df reshaping o Structure check - skimr::skim() • Arguments df %>% • Considerations o Date can be piped to function or called within skimr::skim(df) df %>% dplyr::filter() %>% dplyr::pivot() 8. Themes
  • 17. II. Structure 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries o Discrete or Continuous data for coordinates o Discrete or Continuous metadata for mapping o Missing Data etc. o Tidyverse can be really useful here o data transformations - skimr::skim() • Arguments df %>% • Considerations o df reshaping Foundation: Graph Data o Structure check o Date can be piped to function or called within skimr::skim(df) df %>% dplyr::filter() %>% dplyr::pivot() 8. Themes
  • 18. II. Structure df %>% ggplot(…) + 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries 8. Themes
  • 19. II. Structure o Data = o Coordinates = o Mapping = ggplot(df, aes()) + … ggplot(df) + ggplot() + df %>% ggplot(., aes()) + 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries ggplot(…) + • Consideration o Calls the plot object o Best placement of df for plot development • Arguments 8. Themes
  • 20. II. Structure 1. Data 2. Function 3. Coordinates 4. Mapping 5. Geometries 6. Scales 7. Facets df %>% ggplot(., aes(x = x, y = y, …)) + 8. Themes
  • 21. II. Structure o Data positions for plot o Not necessary to specify here, but must be supplied in plot layers o Continuous Data - Coordinate according to the data o Discrete Data - At 1, 2, 3 etc. on axis - Alphabetical for character class - Level for factors class 4. Mapping 6. Scales 7. Facets 1. Data 2. Function 3. Coordinates 5. Geometries ggplot(., aes(x = x, y = y, …)) + • Considerations 8. Themes
  • 22. II. Structure o x = o y = o positional, not necessary to specify o for geometries that use count, y not accepted 4. Mapping 6. Scales 7. Facets 1. Data 2. Function 8. Themes 3. Coordinates 5. Geometries ggplot(., aes(x = x, y = y, …)) + • Arguments 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Log(Value)
  • 23. II. Structure df %>% ggplot(., aes(x = x, y = y, *= var1) + 1. Data 2. Function 3. Coordinates 4. Mapping 5. Geometries 6. Scales 7. Facets 8. Themes
  • 24. II. Structure o Variables are mapped to visual properties (aesthetics) o Choosing the aesthetic (*) - Color - Fill - Size - Shape - Linetype - Transparency o Is the Continuous or Discrete? o What are you mapping to? 4. Mapping 6. Scales 7. Facets 1. Data 2. Function 3. Coordinates 5. Geometries ggplot(., aes(x = x, y = y, * = var1) + • Considerations 8. Themes
  • 25. II. Structure 4. Mapping 6. Scales 7. Facets 1. Data 2. Function 8. Themes 3. Coordinates 5. Geometries ggplot(., aes(x = x, y = y, * = var1) + • Arguments o Mappings can be set in ggplot() - ggplot(.,aes(color = var1)) o Mappings can be set in individual layers - geom_point(.,aes(color = var1)) o What is outside of the mapping will be interpreted literally - geom_point(.,aes(color = ‘var1’)) o Levels of the mapping are set in scales, else: - (.,aes(), color = ‘black’)
  • 26. II. Structure 1. Data 2. Function 3. Coordinates 4. Mapping 5. Geometries 6. Scales 7. Facets 8. Themes df %>% ggplot(., aes(x = x, y = y, * = var1))+ geom_*(…) +
  • 27. II. Structure o What kind of visual representations (*) are the best approach? o Types of Geometry - Reference Lines - Barcharts - Dots and points - Boxplots - Heatmaps - Maps - Density - Polygons 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries geom_*(…) + • Considerations ** - - - Jitters Error Bars Text and Labels 8. Themes * *** key point here
  • 28. II. Structure o What kind of visual representations (*) are the best approach? o Types of Geometry - Reference Lines - Barcharts - Dots and points - Boxplots - Heatmaps - Maps - Density - Polygons 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries geom_*(…) + • Considerations ** * *** key point here Foundation: Graph Attributes - - - Jitters Error Bars Text and Labels 8. Themes
  • 29. II. Structure o Arguments • Over 40 individual geoms • geom_*() - geom_point() - geom_hist() - geom_bar() - geom_col() - geom_tile() • Can build individual mappings within geoms_*() • Can add multiple geometries as annotation layers • Know the defaults geom_*(mapping =, data =, stat = , position = ) 4. Mapping 6. Scales 7. Facets 1. Data 2. Function 8. Themes 3. Coordinates 5. Geometries geom_*(…) +
  • 30. II. Structure 1. Data 4. Mapping 2. Function 3. Coordinates 5. Geometries 6. Scales 7. Facets df %>% ggplot(., aes(x = x, y = y, * = var1))+ geom_*(…) + scale_*_*(…) + 8. Themes
  • 31. II. Structure o Specify Data and Mappings o Set arguments for coordinates (*) - x - y o Set arguments for mappings (*) - color - fill - alpha - linetype - … and many more o Different calls for discrete and continuous data types o Commonly used defaults are prebuilt 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries scale_*_*(…) + • Concept 8. Themes
  • 32. II. Structure 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries scale_*_*(…) + • Argument s scale_*_*( name = , breaks = , values = , labels = , limits = , trans = , guide = , position = , ….) 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Log(Value)
  • 33. II. Structure o scale_x_continuous(…) o scale_y_continuous(…) o scale_x_discrete(…) o scale_y_discrete(…) • Pre-Built CustomAxis Scales o scale_*_log10(...) o scale_*_reverse(...) o scale_*_sqrt(...) o scale_*_datetime(…) o … and many more 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries scale_*_*(…) + • Standard Axis Scales 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Log(Value)
  • 34. II. Structure o scale_alpha_continuous(…) o scale_alpha_discrete(…) • Standard Shape Scales o scale_shape_continuous(…) o scale_shape_discrete(…) • Standard Linetype Scales o scale_linetype_continuous(…) o scale_linetype_discrete(…) • … and many more Standard • … and many more Pre-Built 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries scale_*_*(…) + • StandardAlpha Scales
  • 35. II. Structure o scale_color_continuous(…) o scale_fill_continuous(…) o scale_color_manual(…) o scale_fill_manual(…) • Pre-Built Custom Color Scales o scale_*_brewer(...) o scale_*_gradient(...) o scale_*_gradientn(...) o scale_*_viridis(…) o … and many more 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries scale_*_*(…) + • Standard Color Scales
  • 36. II. Structure • Standard Scale – specify colors o scale_color_manual(values = c(‘red’, ‘green’, ‘blue’) • Pre-Built Custom Color Scales o scale_*_brewer( type =, palette = , direction = , aesthetics = ) o scale_color_brewer(palete = ‘set2’) 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 5. Geometries scale_*_*(…) + 3. Coordinates 8. Themes
  • 37. II. Structure df %>% ggplot(., aes(x = x, y = y, * = var1) + geom_*(….) + scale_*_*(…) + facet_*(…) 1. Data 4. Mapping 2. Function 3. Coordinates 5. Geometries 6. Scales 7. Facets 8. Themes
  • 38. II. Structure o Highlight levels in data through multiple panels o facet_wrap(…) • creates a ribbon of levels o facet_grid(…) • creates a matrix of rows and columns of variable combinations • Arguments o nrow =, o ncol =, o scales = , o shrink =, o labeller = , o strip.position = , o …) 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries facet_*(…) + • Concept
  • 39. II. Structure df %>% ggplot(., aes(x = x, y = y, * = var1) + geom_*(….) + scale_*_*(…) + facet_*(…) + theme(…) 1. Data 4. Mapping 2. Function 3. Coordinates 5. Geometries 6. Scales 7. Facets 8. Themes
  • 40. II. Structure o Encompasses ALL the options of ggplot2:: plot ‘Elements’ o 4 main modifiers - line: all line elements - rect: all rectangular elements - text: all text elements - title: all title elements (including: plot, axes, legends..) o Pre-built themes available - theme_bw(…) - theme_grey(…) o ggthemes:: - a package with a wider selection of Pre-Built themes 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 3. Coordinates 5. Geometries theme(…) • Concept 8. Themes
  • 41. II. Structure o Encompasses ALL the options of ggplot2:: plot ‘Elements’ o 4 main modifiers - line: all line elements - rect: all rectangular elements - text: all text elements - title: all title elements (including: plot, axes, legends..) o Pre-built themes available - theme_bw(…) - theme_grey(…) o ggthemes:: - a package with a wider selection of Pre-Built themes 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries theme(…) • Concept Foundation: Graph Elements
  • 42. II. Structure • Argument o The main modifiers: - theme( text* = element_text(), panel* = element_rect(), axis* = element_line(), title* = element_text()) o where * = … 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries theme(…) +
  • 43. II. Structure 1. Data 4. Mapping 6. Scales 7. Facets 2. Function 8. Themes 3. Coordinates 5. Geometries theme(…) + theme( aspect.ratio, axis.title, axis.title.x, axis.title.x.top, axis.title.x.bottom, axis.title.y, axis.title.y.left, axis.title.y.right, axis.text, axis.text.x, axis.text.x.top, axis.text.x.bottom, axis.text.y, axis.text.y.left, axis.text.y.right, axis.ticks, axis.ticks.x, axis.ticks.x.top, axis.ticks.x.bottom, axis.ticks.y, axis.ticks.y.left, axis.ticks.y.right, axis.ticks.length, axis.ticks.length.x, axis.ticks.length.x.top, axis.ticks.length.x.bottom, axis.ticks.length.y, axis.ticks.length.y.left, axis.ticks.length.y.right, axis.line, axis.line.x, axis.line.x.top, axis.line.x.bottom, axis.line.y, axis.line.y.left, axis.line.y.right, legend.background, legend.margin, legend.spacing, legend.spacing.x, legend.spacing.y, legend.key, legend.key.size, legend.key.height, legend.key.width, legend.text, legend.text.align, legend.title, legend.title.align, legend.position, legend.direction, legend.justification, legend.box, legend.box.just, legend.box.margin, legend.box.background, legend.box.spacing, panel.background, panel.border, panel.spacing, panel.spacing.x, panel.spacing.y, panel.grid, panel.grid.major, panel.grid.minor, panel.grid.major.x, panel.grid.major.y, panel.grid.minor.x, panel.grid.minor.y, panel.ontop, plot.background, plot.title, plot.title.position, plot.subtitle, plot.caption, plot.caption.position, plot.tag, plot.tag.position, plot.margin, strip.background, strip.background.x, axis.ticks.length.x.bottom, strip.background.x, strip.background.y, strip.placement, strip.text, strip.text.x, strip.text.y, strip.switch.pad.grid, strip.switch.pad.wrap, ...,)
  • 44. bar graph sample measurements color is species dodged facetted width and length III. Application Sketch Story
  • 45. bar graph sample measurements color is species dodged facetted width and length III. Application • Facetted Width and Length • Bargraph • Trial axis breaks as ‘2010’, 2015’, ‘2020’ • Y axis at 2, 4, 6, 8 • Choose a different palette for species • Ditch the points, and accompanying scale • Dodge position • Species in italics • No grey in facet • New font Sketch Story
  • 46. III. Application • Facetted Width and Length • Bargraph • Trial axis breaks as ‘2010’, 2015’, ‘2020’ • Y axis at 2, 4, 6, 8 • Choose a different palette for species • Ditch the points, and accompanying scale • Dodge position • Species in italics • No grey in facet • New font 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Value Petal Length 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Log(Value) Petal Width Sketch Story
  • 47. III. Application 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Value Petal Length 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Log(Value) Petal Width Sketch Story
  • 48. III. Application 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Value Petal Length 100,000.00 10,000.00 1,000.00 100.00 10.00 2000 2020 2010 Time Log(Value) Petal Width Polished Story
  • 49. III. Application This is just one round of editing! What else would you change??? Polished
  • 50. Troubleshooting o Class of data: counts or continuous data? o Layer, layer, layer, order matters o What are the arguments within the function • Change the default or consider an alternate geom/function o Are you invoking a discrete call on continuous data? o Did you set color when you meant fill? o Did you specify the multiple arguments for the same item? • The last one will be what is seen, check your code. o Is the default of the function to use a count transform? Conclusions
  • 51. Resources for working in R o ggplot2.tidyverse/org o ggplot2-book.org/ o www.r-graph-gallery.com/ o tidytuesday podcast and webpage o Esquisse and Colors Add-Ins o Thomas lin Pedersen – ggplot2 – two-part series o Stackoverflow Conclusions
  • 52. 10 0, 00 0. 00 1 0 , 0 0 0 . 0 0 1 , 0 0 0 . 0 0 1 0 0 . 0 0 1 0 . 0 0 2 0 0 0 2 0 2 0 * * * * * * keypoint here 2 0 1 0 T i m e Log(Va lue) Very Catchy Title ggplot(…) Today’s Goal o Understand how to develop graphics that: • Effectively tell a story • In ggplot2 (and some tidyverse) • Are refined or highly refined. Conclusions
  • 53. o Github: github.com/meghartwick/ o LinkedIn: /meghan-hartwick- 83291551/ o Twitter: @HartwickMeghan
  • 54. ‘The question in R is not if it can be done, but how.’ Questions?