Theme Song
# 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')
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)
Kindly refer to manual Mastering Software Development in R (web-base). (or Mastering Software Development in R.pdf)
Kindly refer to Mastering Software Development in R Specialization to know the whole courses for the R specialization.
# 1: <- ''
# 2: <- ''
# 3: <- ''
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
Reading: Introduction
Reading: Guideline for Good Plots
Reading: Scales and Color
Reading: To Find Out More
Quiz: Plotting with ggplot2
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
Reading: Overview of htmlWidgets
Reading: plotly Package
Reading: Creating Your Own Widget
Mapping and interactive plots
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
Quiz: Basics of grid graphics
Reading: Introduction
Reading: Why Build a New Theme?
Reading: Default Theme
Reading: Building a New Theme
Reading: Summary
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
ggplot()
requires that the value supplied to the ‘data’ argument be alibrary(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
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?
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
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()`
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
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
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
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
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)
ggplot2
?ans:2,3 (0.50 / 1point)
ans:1,2,3 (0.25/1 point)
geom_smooth(method = "lm")
do when it is added to a ggplot
object?x-axis
in a scatterplot?scale_x_continuous()
scale()
geom_scale()
scale_y_continuous()
Kindly refer to GitHub wk4package
(previous The farsdata
R Package) which is my R package repo for the assignment.
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.
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
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 🌏 |