.

Outline

  • The grammar of graphics

  • Datasets and mapping

  • Geometries

  • Statistical transformation and plotting distribution

  • Position adjustment and scales

  • Coordinates and themes

  • Facets and custom plots

The grammar of graphics

Why ggplot2?

  • Most requested programming languages for data scientists are R and Python.

  • ggplot2 as a visualization package for R, is becoming an industry standard for visualization.

Why grammar?

  • You can create new sentences if you know about the grammar.

  • In ggplot2 context, you can create new graphics or tailored plot that suits your needs or preferences.

The idea of grammar of graphics


The idea of grammar of graphics

ggplot(data = data1)

The idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat))

The idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat, color = clarity)) +
       geom_point()

The idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat, color = clarity)) +
       geom_point() +
       stat_smooth()

The idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat, color = clarity)) +
       geom_point() +
       stat_smooth() +
       scale_x_log10()

The idea idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat, color = clarity)) +
       geom_point() +
       stat_smooth() +
       scale_x_log10() +
       coord_flip()

The idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat, color = clarity)) +
       geom_point() +
       stat_smooth() +
       scale_x_log10() +
       coord_flip() +
       facet_wrap(~ cut)

The idea of grammar of graphics

ggplot(data = data1, 
       mapping = aes(x = price, y = carat, color = clarity)) +
       geom_point() +
       stat_smooth() +
       scale_x_log10() +
       coord_flip() +
       facet_wrap(~ cut) +
       theme_light()

Data and mapping

Data

  • syntax
ggplot(data = <dataset>)
  • For ggplot graphs, data are usually wrangled.

  • Tidy data

    • Each variable is a column

    • Each observation is a row

    • Each value is a cell

Mapping

  • syntax
ggplot(data = <dataset>,
       mapping = aes(x = <var1>, y = <var2>, ...))
  • Variables are mapped to graphic’s visual properties with aesthetics mapping

  • Usually we map:

    • one variable on x axis

    • one variable on y axis

    • mapped to color, shape, fill, group, etc.

Data and mapping

  • Can specify data and mappings in the plot category
ggplot(data = mydata, mapping = aes(x = varX, y = varY))
  • Or specify for each layer
ggplot() +
  geom_point(data = mydata, mapping = aes(x = varX, y =varY))

Data and mapping

ggplot(data = mpg, 
       mapping = aes(x = displ, y = hwy, color = class)) +
       geom_point() +
       geom_smooth(se = FALSE)

ggplot(data = mpg, 
       mapping = aes(x = displ, y = hwy)) +
       geom_point(aes(color = class)) +
       geom_smooth(se = FALSE)

Aesthetic mapping

mapping

ggplot(data = mpg, 
       mapping = aes(x = displ, y = hwy, color = class)) +
       geom_point()

setting

ggplot(data = mpg, 
       mapping = aes(x = displ, y = hwy, color = class)) +
       geom_point(color  = "red")

Geometries

Geometries

  • syntax
ggplot(data = <dataset>,
       mapping = aes(x = <varX>, y = <varY>, ...)) +
  geom_<function>(...)
  • Geometry stands for geom function

  • Tell R how to render each data point on a given figure.

Geometries

Geometries

  • Each geom can display certain aesthetics.

  • Some of them are required.

Geometries

Line plots

Aesthetics of geom_path, geom_line, geom_step:

  • x
  • y
  • alpha
  • colour/ color
  • linetype
  • size
  • group

We will use the babynames data from the babynames package for demonstration.

library(babynames)
glimpse(babynames)
Rows: 1,924,665
Columns: 5
$ year <dbl> 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880,…
$ sex  <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", …
$ name <chr> "Mary", "Anna", "Emma", "Elizabeth", "Minnie", "Margaret", "Ida",…
$ n    <int> 7065, 2604, 2003, 1939, 1746, 1578, 1472, 1414, 1320, 1288, 1258,…
$ prop <dbl> 0.07238359, 0.02667896, 0.02052149, 0.01986579, 0.01788843, 0.016…

Geometries

Line plots: practice exercise

  • Recreate the plot shown on the right.

Geometries

Scatterplots

We can derived plots like:

  • Connected scatter plot (if geom_line is added)
  • Bubble plot (mapping size to a variable)

To avoid overlappping

  • alpha aesthetic
  • “jitter” or geom_jitter for position
geom_point(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Geometries

Scatterplots

Colors

  • continuous data

    • scale_color_gradient
    • scale_fill_gradient
  • discrete data

    • scale_color_manual
    • scale_fill_manual
geom_point(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Geometries

Scatter plot: practice exercise

Use the mpg data to recreate the plot shown on the right.

Statistical transformation and plotting distribution

Statistics

  • syntax
ggplot(data = <dataset>,
       mapping = aes(x = <varX>, y = <varY>, ...)) +
      geom_<function>(..., stat = <stat>, position = <position>) +
      geom_<stat>(...)
  • Every layer has a statistical transformation associated to it.

  • geoms control the way the plot looks

  • stats control the way the data is transformed

Statistics

Geoms and stats

Every geometry has a default stat.

  • geom_line default stat is stat_identity
  • geom_point default stat is stat_identity
  • geom_smooth default stat is stat_smooth

Each stat has a default geom

  • stat_smooth default geom is geom_smooth
  • stat_count default geom is geom_bar
  • stat_sum default geom is geom_point

Statistics

Interesting stats

  • stat_smooth(geom_smooth)
  • stat_unique(geom_point)
  • stat_summary(geom_pointrange
  • stat_count(geom_bar)
  • stat_bin(geom_histogram)
  • stat_density(geom_density)
  • stat_boxplot(geom_boxplot)
  • stat_ydensity(geom_violin)

Statistics

Computed aesthetics

When a stat perform a transformation, new variables are created.

e.g., in geom_histogram computed variables are:

  • count - number of points in bin
  • density - density of points in bins, scaled to integrate to 1 ncount.
  • ncount - count, scaled to maximum of 1
  • ndensity - density, scaled to maximum of 1

To access: + old way: ..<stat name>.. + new way: stat(name)

ggplot(data = mpg, mapping = aes(x = displ)) +
      geom_histogram(aes(y = ..count..))

Statistics

Computed aesthetics

When a stat perform a transformation, new variables are created.

e.g., in geom_histogram computed variables are:

  • count - number of points in bin
  • density - density of points in bins, scaled to integrate to 1 ncount.
  • ncount - count, scaled to maximum of 1
  • ndensity - density, scaled to maximum of 1

To access: + old way: ..<stat name>.. + new way: stat(name)

ggplot(data = mpg, mapping = aes(x = displ)) +
  geom_histogram(aes(y = stat(density)))

Displaying distribution

Ways to look at distributions:

  • Histograms

  • Frequency polygons

  • Density plots

  • Boxplots

  • Violin plots

Displaying distribution

Histogram and freq polygon

geom_histogram

  • display counts with bars

  • require continuous data

ggplot(data = data1, mapping = aes(x = price)) + 
  geom_histogram()

Displaying distribution

Histogram and freq polygon

geom_histogram

  • display counts with bars
  • require continuous data

geom_freqpoly

  • use lines instead of bars
  • same parameters can be applied
    • bindwith
    • bins
ggplot(data = data1, mapping = aes(x = price)) + 
  geom_freqpoly()

Displaying distribution

Histogram and freq polygon

geom_histogram

  • display counts with bars

  • require continuous data

geom_freqpoly

  • use lines instead of bars

  • same parameters can be applied

    • bindwith

    • bins

ggplot(data = data1, mapping = aes(x = price)) + 
  geom_freqpoly() +
  geom_histogram(alpha = 0.4)

Displaying distribution

Density plots

geom_density

  • a smoothed version of the frequency polygon

  • different from geom_area where aesthetic y is needed

ggplot(data = data1, mapping = aes(x = depth)) +
  geom_density()

Displaying distribution

Density plots

geom_density

  • a smoothed version of the frequency polygon

  • different from geom_area where aesthetic y is needed

ggplot(data = data1, mapping = aes(x = depth, color = cut, fill = cut)) +
      geom_density(alpha = 0.4)

Displaying distribution

Density plots

geom_density

  • a smoothed version of the frequency polygon

  • different from geom_area where aesthetic y is needed

geom_density_ridges

  • available in ggridges package

  • create a ridgeline plots

install.packages("ggridges")
library(ggridges)

ggplot(data = data1, mapping = aes(x = depth, color = cut, fill = cut)) +
  geom_density_ridges(aes(y = cut))

Displaying distribution

Practice exercise

Use the mpg data to recreate the plot shown on the right. You may use the following parameters:

  • bins = 10

  • fill = "cadetblue3"

  • alpha = 0.5

Displaying distribution

Boxplot

geom_boxplot

  • Interesting parameters

    • width and varwidth

    • show.legend

    • outlier.alpha

    • outlier.shape

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot()
  

Displaying distribution

Violin plot

geom_violin

  • Interesting parameters

    • trim

    • scale

    • draw_quantiles

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_violin()
  

Displaying distribution

Practice exercise

Use the mpg data to recreate the plot shown on the right.

Scale and position adjustments

Scales and position adjustments

  • syntax
ggplot(data = <dataset>,
       mapping = aes(x = <varX>, y = <varY>, ...)) +
  geom_<function>(..., stat = <stat>, position = <position>) +0
  geom_<stat>(...) + 
  scale_<aesthetic>_<type> #<<
  • Scales control how data values are translated to visual properties

  • Can overide default scales like axis,legend, and transformation of data to aesthetics.

Scale

Scales belong to one these types:

  • continuous scale
  • discrete scale
  • binned scale

Naming scheme:

  • scale + aesthetic + name of scale

    • scale_*_continuous()
    • scale_*_discrete()
    • scale_*_manual()

Scale

Scale

p <- ggplot(iris_data, aes(x = sepal_length, y = sepal_width, color = species)) + geom_point(size = 2)
p

p + scale_color_manual(name = "Manual",
                       values = c("#5BC0EB","#FDE74C","#9BC53D"))

Scale

Position scales

Continuous

  • scale_x_continuous | scale_y_continuous
  • scale_x_log10 | scale_y_log10
  • scale_x_reverse | scale_y_reverse
  • scale_x_sqrt | scale_x_sqrt
scale_x_continuous(
  name = waiver(),
  breaks = waiver(),
  minor_breaks = waiver(),
  n.breaks = NULL,
  labels = waiver(),
  limits = NULL,
  expand = waiver()
  oob = censor,
  na.value = NA_real_,
  trans = "identity",
  position = "bottom",
  guide = waiver,
  )

Scale

Position scales

Binned

  • scale_x_binned | scale_y_binned
scale_x_binned(
  name = waiver(),
  breaks = waiver(),
  labels = waiver(),
  limits = NULL,
  exapand = waiver()
  oob = censor,
  na.value = NA_real_,
  trans = "identity",
  position = "bottom",
  )

Scale

Position scales

Discrete

  • scale_x_discrete | scale_y_discrete
scale_x_discrete(
  name = waiver(),
  breaks = waiver(),
  labels = waiver(),
  limits = NULL,
  exapand = waiver()
  oob = censor,
  na.value = NA_real_,
  trans = "identity",
  position = "bottom",
  )

Scale

Color scales

Continuous

  • scale_color_continuous | scale_fill_continuous
  • scale_color_gradient | scale_fill_gradient

Binned

  • scale_color_binned | scale_fill_binned
  • scale_color_steps | scale_fill_steps

Discrete

  • scale_color_discrete | scale_fill_discrete
  • scale_color_hue | scale_fill_hue
  • scale_color_grey | scale_color_grey
ggplot(mtcars, aes(mpg, wt, color = cyl)) +
  geom_point(size = 5) +
  scale_color_viridis_c()

Scale

Viridis family

Continuous

  • scale_color_viridis_c
  • scale_fill_viridis_c

Binned

  • scale_color_viridis_b
  • scale_fill_viridis_b

Discrete

  • scale_color_viridis_d
  • scale_fill_viridis_d
ggplot(mtcars, aes(mpg, wt, color = cyl)) +
  geom_point(size = 5) +
  scale_color_viridis_c()

Scale

Colorbrewer family

Continuous

  • scale_color_distiller | scale_fill_distiller

Binned

  • scale_color_fermenter | scale_fill_fermenter

Discrete

  • scale_color_brewer | scale_fill_brewer
# useful parameters
type = "seq"(sequential, the default), "div"(diverging), "qual"(qualitative)
direction = 1 (default), -1 (reverse order)
palette = name of pallete or index

Scale

scale_*_manual

  • available only for discrete scales
  • useful if you want to specify your own set of mappings from levels in the data to aesthetic values.

For example

  • choosing set of colors in discrete color scale
  • specifying your own set of alpha
  • specifying your own set of shapes
  • specifying your own set of linetypes
ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +
  geom_point(size = 4) +
  scale_color_manual(values = c("blue", "black", "orange"))

Scale

scale_*_manual

  • available only for discrete scales
  • useful if you want to specify your own set of mappings from levels in the data to aesthetic values.

For example

  • choosing set of colors in discrete color scale
  • specifying your own set of alpha
  • specifying your own set of shapes
  • specifying your own set of linetypes
ggplot(mtcars, aes(x = mpg, y = wt, alpha = factor(cyl))) +
  geom_point(size = 4) +
  scale_alpha_manual(values = c(0.3, 0.6, 1))

Scale

scale_*_manual

  • available only for discrete scales
  • useful if you want to specify your own set of mappings from levels in the data to aesthetic values.

For example

  • choosing set of colors in discrete color scale
  • specifying your own set of alpha
  • specifying your own set of shapes
  • specifying your own set of linetypes
ggplot(mtcars, aes(x = mpg, y = wt, shape = factor(cyl))) +
  geom_point(size = 4) +
  scale_shape_manual(values = c(0.3, 0.6, 1))

Scales

Shortcuts

Labs

  • modify axis, legend, and plot labels
  • xlabs, ylabs: modify x and y axis label names
  • use labs with arguments:
    • title
    • x
    • subtitle
    • caption
p + 
  labs(title = "Title of my plot",
       x = "Miles per gallon",
       y = "Weight", #<<
       subtitle = "This would be a subtitle",
       caption = "This is my caption")

Scales

Shortcuts

Lims

  • modify limits of the plot
  • use xlim and ylim
xlim(0,50)
ylim(NA, 40)
  • or lims specifying vectors
lims(x = c(0, 50))
lims(y = y(NA, 40))
p +
  xlim(10, 25)

Scale

Shortcuts

Lims

  • modify limits of the plot
  • use xlim and ylim
xlim(0,50)
ylim(NA, 40)
  • or lims specifying vectors
lims(x = c(0, 50))
lims(y = y(NA, 40))
p + 
  lims(x = c(0, 20),
       y = c(1, 4))

Position adjustments

  • All layers have a position that resolves overlapping geoms

  • Overrides default using position argument to geom_ or stat_ function.

Position adjustments

position_jitter

  • ?position_jitter

  • Adds random noise to the data points to avoid overlaps

  • Useful for scatterplots

    • wrapper: geom_jitter

Parameters

seeds = random seeds to make jitter reproducible
width = amount of jitter horizontally
height = amount of jitter vertically
ggplot(mpg, aes(x = class, y = hwy)) + 
  geom_point(size = 3,
             position = position_jitter(width = 0.2,
                                        seed = 143 ))

Position adjustments

position_stack()

  • ?position_stack

  • Stacks geoms on top of each other.

position_fill()

  • ?position_fill

  • Stacks geoms on top of each other and standardizes the height.

Parameters

reverse - default = FALSE, if TRUE will reverse the default stacking order.
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) + 
  geom_bar(alpha = 0.5,
           position = "identity")

Position adjustments

position_stack()

  • ?position_stack

  • Stacks geoms on top of each other.

position_fill()

  • ?position_fill

  • Stacks geoms on top of each other and standardizes the height.

Parameters

reverse - default = FALSE, if TRUE will reverse the default stacking order.
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) + 
  geom_bar(alpha = 0.5,
           position = "stack")

Position adjustments

position_stack()

  • ?position_stack

  • Stacks geoms on top of each other.

position_fill()

  • ?position_fill

  • Stacks geoms on top of each other and standardizes the height.

Parameters

reverse - default = FALSE, if TRUE will reverse the default stacking order.
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) + 
  geom_bar(alpha = 0.5,
           position = "fill")

Position adjustment

position_dodge

  • ?position_dodge

  • preserves the vertical position of a geom while adjusting the horizontal position.

ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) + 
  geom_bar(alpha = 0.5,
           position = "dodge") #<<

Position adjustment

position_dodge

  • ?position_dodge

  • preserves the vertical position of a geom while adjusting the horizontal position.

Parameters

width(default = 0.9) refers to dodging width
preserve = "single" / "total" (defaul = "total")
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) + 
  geom_bar(alpha = 0.5,
           position = position_dodge(width = 1)) #<<

Position adjustment

position_dodge

  • ?position_dodge

  • preserves the vertical position of a geom while adjusting the horizontal position.

Parameters

width(default = 0.9) refers to dodging width
preserve = "single" / "total" (defaul = "total")
ggplot(mtcars, aes(x = factor(cyl), fill = factor(vs))) + 
  geom_bar(alpha = 0.5,
           position = position_dodge(width = 1,
                                     preserve = "single")) #<<

Scales and position adjustments

Practice exercise

Use the code below to generate a hypothetical dataset and recreate the barplot on the right.

# Create some data
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                dose=rep(c("0.5", "1", "2"),2),
                len=c(6.8, 15, 33, 4.2, 10, 29.5))

Coordinates

syntax

  • Coordinate are sets that locate points in space

  • coord_cartesian()

  • coord_flip()

  • coord_polar()

Coordinates

coord_cartesian()

  • default coordinate system

Zooming into plots

  • setting limits using scale

    • eliminates data outside the specified range
  • setting limits using coordinate system

    • proper way to zoom

    • does not eliminate data outside the plot

Parameters

xlim, ylim, expand, clip

Coordinates

coord_cartesian()

  • default coordinate system

Zooming into plots

  • setting limits using scale

    • eliminates data outside the specified range
  • setting limits using coordinate system

    • proper way to zoom

    • does not eliminate data outside the plot

Parameters

xlim, ylim, expand, clip
p + 
  scale_x_continuous(limits = c(15, 20))

Coordinates

coord_cartesian()

  • default coordinate system

Zooming into plots

  • setting limits using scale

    • eliminates data outside the specified range
  • setting limits using coordinate system

    • proper way to zoom

    • does not eliminate data outside the plot

Parameters

xlim, ylim, expand, clip
p + 
  coord_cartesian(xlim = c(15, 20))

Coordinates

coord_cartesian()

  • default coordinate system

Zooming into plots

  • setting limits using scale

    • eliminates data outside the specified range
  • setting limits using coordinate system

    • proper way to zoom

    • does not eliminate data outside the plot

Parameters

xlim, ylim, expand, clip
p + 
  coord_cartesian(expand = FALSE)

Coordinates

coord_cartesian()

  • default coordinate system

Zooming into plots

  • setting limits using scale

    • eliminates data outside the specified range
  • setting limits using coordinate system

    • proper way to zoom

    • does not eliminate data outside the plot

Parameters

xlim, ylim, expand, clip
p + 
  coord_cartesian(expand = FALSE,
                  clip = "off")

Coordinates

coord_flip()

  • Flips cartesian coordinates (i.e., horizontal axis becomes vertical axis).

  • Useful to draw plots in horizontal mode without having to change the aesthetic mappings.

p + 
  coord_flip()

Coordinates

coord_polar()

  • Apply a polar coordinate system to the plot

Parameters

theta : map angle to x or y
direction: 1 (clockwise) -1 (anticlockwise)
start: offset of starting point in radian
ggplot(mpg, aes(x = displ)) + 
  geom_bar()

Coordinates

coord_polar()

  • Apply a pola coordinate system to the plot

Parameters

theta : map angle to x or y
direction: 1 (clockwise) -1 (anticlockwise)
start: offset of starting point in radian
ggplot(mpg, aes(x = displ)) + geom_bar() +
  coord_polar()

Coordinates

coord_polar()

  • Apply a pola coordinate system to the plot

Parameters

theta : map angle to x or y
direction: 1 (clockwise) -1 (anticlockwise)
start: offset of starting point in radian
ggplot(mpg, aes(x = displ)) + geom_bar() +
  coord_polar(theta = "y")

Coordinates

coord_polar()

  • Apply a pola coordinate system to the plot

Parameters

theta : map angle to x or y
direction: 1 (clockwise) -1 (anticlockwise)
start: offset of starting point in radian
ggplot(mpg, aes(x = displ)) + geom_bar() +
  coord_polar(theta = "y",
              direction = -1)

Coordinates

coord_polar()

  • Apply a pola coordinate system to the plot

Parameters

theta : map angle to x or y
direction: 1 (clockwise) -1 (anticlockwise)
start: offset of starting point in radian
ggplot(mpg, aes(x = displ)) + geom_bar() +
  coord_polar(theta = "y",
              start = 0.5)

Facets and Themes

Facets

syntax

  • Facets divide plot into subplots based on the values of one or more discrete variables.

  • facet_wrap()

  • facet_grid()

Facets

facet_wrap

  • “wraps” a 1d ribbon of panels into 2d
  • useful if you have a variable with many levels

Parameters

ncol
nrow
scales
ggplot(mpg, aes(x = displ, hwy)) + 
  geom_blank() + 
  xlab(NULL)

Facets

facet_wrap

  • “wraps” a 1d ribbon of panels into 2d
  • useful if you have a variable with many levels
ncol
nrow
scales
ggplot(mpg, aes(x = displ, hwy)) + 
  geom_blank() + 
  xlab(NULL) +
  facet_wrap(~ class) #<<

Facets

facet_grid

  • produces a 2d grid of panels defined by variables which form the rows and columns

  • .~ a spreads the values across columns

  • b ~ . spreads the values of b down the ro ws

  • a ~ b spreads a across columns and b down rows

ggplot(mpg, aes(x = displ, hwy)) + 
  geom_blank() + 
  xlab(NULL) +
  facet_grid(. ~ cyl) #<<

Facets

facet_grid

  • produces a 2d grid of panels defined by variables which form the rows and columns

  • .~ a spreads the values across columns

  • b ~ . spreads the values of b down the rows

  • a ~ b spreads a across columns and b down rows

ggplot(mpg, aes(x = displ, hwy)) + 
  geom_blank() + 
  xlab(NULL) +
  facet_grid(drv ~ .) #<<

Facets

facet_grid

  • produces a 2d grid of panels defined by variables which form the rows and columns

  • .~ a spreads the values across columns

  • b ~ . spreads the values of b down the rows

  • a ~ b spreads a across columns and b down rows

ggplot(mpg, aes(x = displ, hwy)) + 
  geom_blank() + 
  xlab(NULL) +
  facet_grid(drv ~ cyl) #<<

Themes

  • Controlling all non-data elements
  • title appearance
  • axis labels
  • axis ticks
  • strips
  • ….

Themes

Options

  • Using the built-in-theme from ggplot2 library
    • theme_gray()
    • theme_bw()
    • theme_light()
    • theme_classic()
    • ...
  • Using other package e.g., ggthemes

Themes

Options

  • Using the built-in-theme from ggplot2 library
    • theme_gray()
    • theme_bw()
    • theme_light()
    • theme_classic()
    • ...
  • Using other package e.g., ggthemes

Themes

Options

  • Using the built-in-theme from ggplot2 library
    • theme_gray()
    • theme_bw()
    • theme_light()
    • theme_classic()
    • ...
  • Using other package e.g., ggthemes

Themes

Options

  • Using the built-in-theme from ggplot2 library
    • theme_gray()
    • theme_bw()
    • theme_light()
    • theme_classic()
    • ...
  • Using other package e.g., ggthemes

Themes

Options

  • Using the built-in-theme from ggplot2 library

  • Using other package e.g., ggthemes

    • theme_economist_white()
    • theme_fivethirtyeight()
    • theme_stata()
    • theme_tufte()

Themes

Options

  • Using the built-in-theme from ggplot2 library

  • Using other package e.g., ggthemes

    • theme_economist_white()
    • theme_fivethirtyeight()
    • theme_stata()
    • theme_tufte()

Themes

Options

  • Using the built-in-theme from ggplot2 library

  • Using other package e.g., ggthemes

    • theme_economist_white()
    • theme_fivethirtyeight()
    • theme_stata()
    • theme_tufte()

Themes

Options

  • Using the built-in-theme from ggplot2 library

  • Using other package e.g., ggthemes

    • theme_economist_white()
    • theme_fivethirtyeight()
    • theme_stata()
    • theme_tufte()

Practice exercise

Let’s apply what we have covered!

  • Use the mpg dataset to recreate the plot.

  • But first, we need to do some data wrangling!

  • Use the updated mpg data to mimic the plot.