Theme Song



1 Setting

1.1 SCSS Setup

# install.packages("remotes")
library('BBmisc', 'rmsfuns')
#remotes::install_github("rstudio/sass")
lib('sass')
## sass 
## TRUE
/* https://stackoverflow.com/a/66029010/3806250 */
h1 { color: #002C54; }
h2 { color: #2F496E; }
h3 { color: #375E97; }
h4 { color: #556DAC; }
h5 { color: #92AAC7; }

/* ----------------------------------------------------------------- */
/* https://gist.github.com/himynameisdave/c7a7ed14500d29e58149#file-broken-gradient-animation-less */
.hover01 {
  /* color: #FFD64D; */
  background: linear-gradient(155deg, #EDAE01 0%, #FFEB94 100%);
  transition: all 0.45s;
  &:hover{
    background: linear-gradient(155deg, #EDAE01 20%, #FFEB94 80%);
    }
  }

.hover02 {
  color: #FFD64D;
  background: linear-gradient(155deg, #002C54 0%, #4CB5F5 100%);
  transition: all 0.45s;
  &:hover{
    background: linear-gradient(155deg, #002C54 20%, #4CB5F5 80%);
    }
  }

.hover03 {
  color: #FFD64D;
  background: linear-gradient(155deg, #A10115 0%, #FF3C5C 100%);
  transition: all 0.45s;
  &:hover{
    background: linear-gradient(155deg, #A10115 20%, #FF3C5C 80%);
    }
  }
## https://stackoverflow.com/a/36846793/3806250
options(width = 999)
knitr::opts_chunk$set(class.source = 'hover01', class.output = 'hover02', class.error = 'hover03')



1.2 Setup

suppressPackageStartupMessages(library('BBmisc'))

pkgs <- c('knitr', 'kableExtra', 'devtools', 'lubridate', 'data.table', 'tidyquant', 'stringr', 'magrittr', 'tidyverse', 'plyr', 'dplyr', 'broom', 'highcharter', 'formattable', 'DT', 'httr', 'openxlsx')
suppressAll(lib(pkgs))

#funs <- c('')
#l_ply(funs, function(x) source(paste0('./function/', x)))

Sys.setenv(TZ='Asia/Tokyo')
options(warn=-1)
rm(pkgs)



2 Introduction

2.2 Syllabus

  • Week 1 : Welcome to Building Data Visualization Tools
    • Welcome
    • Video:Welcome to Building Data Visualization Tools
    • Reading:Textbook Mastering Software Development in R
    • Reading:syllabus
    • Basic plotting with ggplot2
      • Reading: Introduction
      • Reading: Initializing a ggplot Object
      • Reading: Plot Aesthetics
      • Reading: Creating a Basic ggplot plot
      • Reading: Geoms
      • Reading: Using Multiple geoms
      • Reading: Constant aesthetics
      • Reading: Example plots
      • Reading: Extentions of ggplot2
    • Customizing ggplot2 Plots
      • Reading: Introduction
      • Reading: Guideline for Good Plots
      • Reading: Scales and Color
      • Reading: To Find Out More
    • ggplot Basic
      • Quiz: Plotting with ggplot2
  • Week 2 : Mapping and interactive plots
    • Mapping
      • Reading: Introduction
      • Reading: ReadingBasics of Mapping
      • Reading: Readingggmap, Google Maps API
      • Reading: ReadingMapping US counties and states
      • Reading: ReadingMore advanced mapping– Spatial objects
      • Reading: ReadingWhere to find more on mapping with R
    • htmlWidgets
      • Reading: Overview of htmlWidgets
      • Reading: plotly Package
      • Reading: Creating Your Own Widget
    • Mapping Quiz
      • Mapping and interactive plots
  • Week 3 : The grid Package
    • The grid package
      • Reading: Introduction
      • Reading: Overview of grid graphics
      • Reading: Grobs
      • Reading: Viewports
      • Reading: Grid Graphics Coordinate Systems
      • Reading: The gridExtra package
      • Reading: Where to Find More About grid Graphics
    • Grid Package quiz
      • Quiz: Basics of grid graphics
  • Week 4 : Building New Graphical Elements
    • Building a New Theme
      • Reading: Introduction
      • Reading: Why Build a New Theme?
      • Reading: Default Theme
      • Reading: Building a New Theme
      • Reading: Summary
    • Build New Graphical Elements
      • Build New Graphical Elements
      • Reading: Introduction
      • Reading: Building a Geom
      • Reading: Example: An Automatic Transparency Geom
      • Reading: Building a Stat
      • Reading: Example: Normal Confidence Intervals
      • Reading: Combining Geoms and Stats
      • Reading: Summary
    • Build a New Geom
      • Peer-graded Assignment: Build a New Geom
      • Review Your Peers: Build a New Geom

Kindly refer to Mastering Software Development in R Specialization to know the whole courses for the R specialization.

# 1:  <- ''
# 2:  <- ''
# 3:  <- ''

2.3 Lesson

2.3.1 Week 1 : Welcome to Building Data Visualization Tools

2.3.1.1 Welcome

2.3.1.2 Video:Welcome to Building Data Visualization Tools

2.3.1.3 Reading:Textbook Mastering Software Development in R

2.3.1.4 Reading:syllabus

2.3.1.5 Basic plotting with ggplot2

Reading: Introduction

Reading: Initializing a ggplot Object

Reading: Plot Aesthetics

Reading: Creating a Basic ggplot plot

Reading: Geoms

Reading: Using Multiple geoms

Reading: Constant aesthetics

Reading: Example plots

Reading: Extentions of ggplot2

2.3.1.6 Customizing ggplot2 Plots

Reading: Introduction

Reading: Guideline for Good Plots

Reading: Scales and Color

Reading: To Find Out More

2.3.1.7 ggplot Basic

Quiz: Plotting with ggplot2

2.3.2 Week 2 : Mapping and interactive plots

2.3.2.1 Mapping

Reading: Introduction

Reading: ReadingBasics of Mapping

Reading: Readingggmap, Google Maps API

Reading: ReadingMapping US counties and states

Reading: ReadingMore advanced mapping– Spatial objects

Reading: ReadingWhere to find more on mapping with R

2.3.2.2 htmlWidgets

Reading: Overview of htmlWidgets

Reading: plotly Package

Reading: Creating Your Own Widget

2.3.2.3 Mapping Quiz

Mapping and interactive plots

2.3.3 Week 3 : The grid Package

2.3.3.1 The grid package

Reading: Introduction

Reading: Overview of grid graphics

Reading: Grobs

Reading: Viewports

Reading: Grid Graphics Coordinate Systems

Reading: The gridExtra package

Reading: Where to Find More About grid Graphics

2.3.4 Grid Package quiz

Quiz: Basics of grid graphics

2.3.5 Week 4 : Building New Graphical Elements

2.4 Building a New Theme

Reading: Introduction

Reading: Why Build a New Theme?

Reading: Default Theme

Reading: Building a New Theme

Reading: Summary

2.5 Build New Graphical Elements

2.5.1 Build New Graphical Elements

Reading: Introduction

Reading: Building a Geom

Reading: Example: An Automatic Transparency Geom

Reading: Building a Stat

Reading: Example: Normal Confidence Intervals

Reading: Combining Geoms and Stats

Reading: Summary

3 Quiz

  1. The function ggplot() requires that the value supplied to the ‘data’ argument be a
  • character vector
  • list
  • data frame
  • geom


  1. You run the following code and get the error printed directly after the code:
library(ggplot2) 
library(faraway) 
## Error in library(faraway): there is no package called 'faraway'
data(nepali) 
head(nepali, 3) 
## Error in head(nepali, 3): object 'nepali' not found
## id sex wt ht mage lit died alive age 
## 1 120011 1 12.8 91.2 35 0 2 5 41 
## 2 120011 1 12.8 93.9 35 0 2 5 45 
## 3 120011 1 13.1 95.2 35 0 2 5 

ggplot(nepali, aes(x = ht, y = wt)) 
## Error in ggplot(nepali, aes(x = ht, y = wt)): object 'nepali' not found
+ geom_point()
## Error: Cannot use `+.gg()` with a single argument. Did you accidentally put + on a new line?
## Error in +geom_point(): invalid argument to unary operator

What happened?

Because you put the + at the start of the second line, instead of at the end of the first line, R thought the call was over and tried to run the first line by itself, and then tried to run the second line by itself, without an associated ggplot object.

  • The nepali dataset lacks one or both of the two columns (ht, wt) that you - used in the aes statement of the call.

  • You are trying to create a scatterplot for a categorical variable.

  • You have not yet loaded the nepali data frame.

    ans: 3 ans: 2 ans: 1 ans: 4


  1. You have a dataset of observations on study subjects which has height and weight for each subject measured at multiple time points. You want to create a scatterplot of height (x-axis) by weight (y-axis). Since there are multiple measurements per subject, you would like to plot the measurements for each subject in a separate color. There is a column in the data called id that gives the unique id of each study subject; this column currently has the class “numeric”.

Which of the following choices is a good strategy for creating a plot where data points use color to identify the subject?

  • Convert the id column of the data frame to a factor, so a discrete color palette will be used, then use ggplot to create a scatterplot and specify color = id as the sole argument in the geom_point() call.

Example code:

df %>% 
  mutate(id = factor(id)) %>%
  ggplot(aes(x = height, y = weight)) + 
  geom_point(color = id)
## Error in mutate(., id = factor(id)): is.data.frame(.data) || is.list(.data) || is.environment(.data) is not TRUE
  • Leave the id column of the data frame as a numeric, so a continuous color palette will be used, and use ggplot to create a scatterplot, then use + to add the element color(aes(id)).

Example code:

df %>%
  ggplot(aes(x = height, y = weight)) + 
  geom_point() + 
  color(aes(id))
## Error:   You're passing a function as global data.
##   Have you misspelled the `data` argument in `ggplot()`
  • Leave the id column of the data frame as a numeric, so a continuous color palette will be used, then use ggplot2 to create a scatterplot and specify color = id in the aes() section of the ggplot() call.

Example code:

df %>% 
  mutate(id = factor(id)) %>%
  ggplot(aes(x = height, y = weight, color = id)) + 
  geom_point()
## Error in mutate(., id = factor(id)): is.data.frame(.data) || is.list(.data) || is.environment(.data) is not TRUE
  • Convert the id column of the data frame to a factor, so a discrete color palette will be used, then use ggplot2 to create a scatterplot and specify color = id in the aes() section of the ggplot() call.

Example code:

df %>% 
  mutate(id = factor(id)) %>%
  ggplot(aes(x = height, y = weight, color = id)) + 
  geom_point()
## Error in mutate(., id = factor(id)): is.data.frame(.data) || is.list(.data) || is.environment(.data) is not TRUE

ans: 1

ans: 2


  1. You want to save a figure from R as a pdf file named “MyFig.pdf”. Which of the following describes the proper steps to take?

First run all your code to create the figure, then run pdf("MyFig.pdf") to open a pdf device, then use pdf.off() to close the device.

Example code:

ggplot(df, aes(x = x, y = y)) + 
  geom_point()
## Error:   You're passing a function as global data.
##   Have you misspelled the `data` argument in `ggplot()`
pdf("MyFig.pdf")
dev.off()
## png 
##   2

Wrap all your code to create the figure in a pdf statement.

Example code:

ggplot(df, aes(x = x, y = y)) + 
  geom_point()
## Error:   You're passing a function as global data.
##   Have you misspelled the `data` argument in `ggplot()`
pdf("MyFig.pdf")
dev.off()
## png 
##   2

First run dev.on("MyFig.pdf") to open a pdf device, then run all your code to create the figure, then use dev.off() to close the device.

Example code:

dev.on("MyFig.pdf")
## Error in dev.on("MyFig.pdf"): could not find function "dev.on"
ggplot(df, aes(x = x, y = y)) + 
  geom_point()
## Error:   You're passing a function as global data.
##   Have you misspelled the `data` argument in `ggplot()`

First run pdf("MyFig.pdf") to open a pdf device, then run all your code to create the figure, then use dev.off() to close the device.

Example code:

dev.on("MyFig.pdf")
## Error in dev.on("MyFig.pdf"): could not find function "dev.on"
ggplot(df, aes(x = x, y = y)) + 
  geom_point()
## Error:   You're passing a function as global data.
##   Have you misspelled the `data` argument in `ggplot()`
dev.off()
## null device 
##           1

ans: 1

ans: 2


  1. Consider the following dataset.
library(lattice)
data(barley)
head(barley)
##      yield   variety year            site
## 1 27.00000 Manchuria 1931 University Farm
## 2 48.86667 Manchuria 1931          Waseca
## 3 27.43334 Manchuria 1931          Morris
## 4 39.93333 Manchuria 1931       Crookston
## 5 32.96667 Manchuria 1931    Grand Rapids
## 6 28.96667 Manchuria 1931          Duluth
#     yield   variety year            site
# 1 27.00000 Manchuria 1931 University Farm
# 2 48.86667 Manchuria 1931          Waseca
# 3 27.43334 Manchuria 1931          Morris

Which ggplot2 expression would make a scatterplot of year and yield while coloring each point according to its variety?

  • ggplot(data = barley, aes(year, yield)) + geom_point(aes(color = variety))
  • ggplot(data = barley, aes(year, yield)) + geom_point()
  • ggplot(data = barley, aes(year, yield, color = variety))
  • ggplot(data = barley, aes(year, yield)) + geom_point(color = variety)

ans: 2

ans: 4

ans: 3


  1. Consider the following dataset.

You want to create a scatter plot showing Girth on the x-axis, Height on the y-axis, and Volume with point size.

Which of the following code would create that plot?

trees %>%
  ggplot() +
  geom_point(aes(x = Girth, y = Height, size = Volume))

trees %>%
  ggplot(aes(x = Girth, y = Height)) +
  geom_point(size = Volume)
## Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomPoint, : object 'Volume' not found
trees %>%
  ggplot(aes(x = Girth, y = Height, size = Volume))

trees %>%
  ggplot(aes(x = Girth, y = Height, size = Volume)) +
  geom_point()

ggplot() +
  geom_point(trees,
             aes(x = Girth, y = Height, size = Volume))
## Error: `mapping` must be created by `aes()`
trees %>%
  ggplot(aes(x = Girth, y = Height)) +
  geom_point(aes(size = Volume))

trees %>%
  ggplot(aes(x = Girth, y = Height)) +
  geom_point(aes(size = Volume))

ggplot() +
  geom_point(data = trees,
             aes(x = Girth, y = Height, size = Volume))

ans:2,3,4,5 (0.13/1 point)

ans:2,4,5 (0.25/1 point)


  1. How can you create small multiples using ggplot2?
  • Add multiple geoms to the plot using the + syntax
  • Add facet_grid to the geom, specifying one or two columns to use for faceting
  • Add a scales element to decrease the range of the plot scales
  • Add facet_wrap to the geom, specifying the formula to use for faceting

ans:2,3 (0.50 / 1point)

ans:1,2,3 (0.25/1 point)


  1. What does geom_smooth(method = "lm") do when it is added to a ggplot object?
  • It adds a palette of colors for plotting the points in a scatterplot
  • It adds transparency to the points to avoid over plotting
  • It adds a nonlinear smoother to the plot
  • It adds a fitted linear regression line to the plot


  1. When plotting multiple panels using faceting for a single variable, what controls the order in which the panels are plotted?
  • Panels are ordered by the number of data points in each panel
  • Panels are ordered by the alphabetical order or by the order of the levels of the faceting variable
  • Panels are ordered randomly
  • The order of panels must be specified by the user


  1. What function would you use to limit the range of the x-axis in a scatterplot?
  • scale_x_continuous()
  • scale()
  • geom_scale()
  • scale_y_continuous()

4 Assignment

Kindly refer to GitHub wk4package(previous The farsdata R Package) which is my R package repo for the assignment.



5 Conclusion

Awarded on 06 Aug 2021

Final Scores : 85.26/100

Above png file and feel free to download Coursera 04 - Building Data Visualization Tools.pdf.



6 Appendix

6.1 Documenting File Creation

  • File creation date: 2021-07-27
  • File latest updated date: 2021-12-08
  • R version 4.1.2 (2021-11-01)
  • rmarkdown package version: 2.11
  • File version: 0.2.1
  • Author Profile: ®γσ, ξηg Lιαη Ημ
  • GitHub: Source Code
  • Additional session information:
suppressMessages(require('formattable', quietly = TRUE))
suppressMessages(require('knitr', quietly = TRUE))
suppressMessages(require('kableExtra', quietly = TRUE))

sys1 <- devtools::session_info()$platform |> 
    unlist() |> 
    {\(.) data.frame(row.names = 1:length(.), 
                     Category = names(.), session_info = .)}()

sys2 <- data.frame(Sys.info()) |> 
    {\(.) data.frame(Category = row.names(.), Sys.info = .[,1])}()

#remarks, dim(sys1), dim(sys2)
if (nrow(sys1) == 11 & nrow(sys2) == 8) {
  sys2 <- sys2 |> 
    {\(.) rbind(., data.frame(
    Category = c('rmarkdown', 'rsconnect', 'Current time'), 
    Sys.info = c(as.character(getwd()), 
                 as.character(packageVersion('rsconnect')), 
                 paste(as.character(lubridate::now('Asia/Tokyo')), 'JST 🌏'))))}()
  
} else if (nrow(sys1) == 10 & nrow(sys2) == 8) {
  sys1 %<>% rbind(., data.frame(Category = '', session_info = ''))
  
  sys2 <- sys2 |> 
    {\(.) rbind(., data.frame(
    Category = c('rmarkdown', 'rsconnect', 'Current time'), 
    Sys.info = c(as.character(getwd()), 
                 as.character(packageVersion('rsconnect')), 
                 paste(as.character(lubridate::now('Asia/Tokyo')), 'JST 🌏'))))}()
}

sys <- cbind(sys1, sys2) |> 
  {\(.) 
    kbl(., caption = 'Additional session information:')}() |> 
  {\(.) 
    kable_styling(., bootstrap_options = c('striped', 'hover', 'condensed', 'responsive'))}() |> 
  {\(.) 
    row_spec(., 0, background = 'DimGrey', color = 'yellow')}() |> 
  {\(.) 
    column_spec(., 1, background = 'CornflowerBlue', color = 'red')}() |> 
  {\(.) 
    column_spec(., 2, background = 'grey', color = 'black')}() |> 
  {\(.) 
    column_spec(., 3, background = 'CornflowerBlue', color = 'blue')}() |> 
  {\(.) 
    column_spec(., 4, background = 'grey', color = 'white')}() |> 
  {\(.) 
    row_spec(., 11, bold = TRUE, color = 'yellow', background = '#D7261E')}()

rm(sys1, sys2)
sys
Additional session information:
Category session_info Category Sys.info
version R version 4.1.2 (2021-11-01) sysname Linux
os Ubuntu 20.04.3 LTS release 5.4.0-1037-aws
system x86_64, linux-gnu version #39~18.04.1-Ubuntu SMP Fri Jan 15 02:48:42 UTC 2021
ui X11 nodename application-3100168-deployment-10747036-p7mhs
language (EN) machine x86_64
collate C.UTF-8 login unknown
ctype C.UTF-8 user rstudio-user
tz Asia/Tokyo effective_user rstudio-user
date 2021-12-08 rmarkdown /cloud/project/04 Building Data Visualization Tools
pandoc 2.14.0.3 @ /usr/lib/rstudio-server/bin/pandoc/ (via rmarkdown) rsconnect 0.8.25
Current time 2021-12-08 18:34:04 JST 🌏