geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white",
size = 0.4
)
# Adjust bins
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white",
#  size = 0.4
)
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white" #,
#  size = 0.4
)
# Adjust bins
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
)
# Add labels
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
) +
labs(
title = "Distribution of Fuel Efficiency",
subtitle = "Data source: Motor Trend Car Road Tests (mtcars)",
x = "Miles Per Gallon (MPG)",
y = "Frequency",
caption = "Binwidth = 2"
)
# Adjust label style
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
) +
labs(
title = "Distribution of Fuel Efficiency",
subtitle = "Data source: Motor Trend Car Road Tests (mtcars)",
x = "Miles Per Gallon (MPG)",
y = "Frequency",
caption = "Binwidth = 2"
) +
theme(
plot.title = element_text(face = "bold", size = 16, color = "#222222"),
plot.subtitle = element_text(size = 11, color = "#666666", margin = margin(b = 15)),
axis.title.x = element_text(margin = margin(t = 10), size = 11, face = "bold"),
axis.title.y = element_text(margin = margin(r = 10), size = 11, face = "bold"),
panel.grid.major.y = element_line(color = "#eaeaea"),
plot.margin = margin(20, 20, 20, 20)
)
# Add density line
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
) +
labs(
title = "Distribution of Fuel Efficiency",
subtitle = "Data source: Motor Trend Car Road Tests (mtcars)",
x = "Miles Per Gallon (MPG)",
y = "Frequency",
caption = "Binwidth = 2"
) +
theme(
plot.title = element_text(face = "bold", size = 16, color = "#222222"),
plot.subtitle = element_text(size = 11, color = "#666666", margin = margin(b = 15)),
axis.title.x = element_text(margin = margin(t = 10), size = 11, face = "bold"),
axis.title.y = element_text(margin = margin(r = 10), size = 11, face = "bold"),
panel.grid.major.y = element_line(color = "#eaeaea"),
plot.margin = margin(20, 20, 20, 20)
) +
geom_density(
aes(y = after_stat(count) * 2),
color = "#e15759",
size = 1,
adjust = 1.2
)
colors()
# What color names are available
colors()
# Basic plot
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram()
# Adjust bins
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
)
# What color names are available
colors()
# Add labels
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
) +
labs(
title = "Distribution of Fuel Efficiency",
subtitle = "Data source: Motor Trend Car Road Tests (mtcars)",
x = "Miles Per Gallon (MPG)",
y = "Frequency",
caption = "Binwidth = 2"
)
# Adjust styles
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
) +
labs(
title = "Distribution of Fuel Efficiency",
subtitle = "Data source: Motor Trend Car Road Tests (mtcars)",
x = "Miles Per Gallon (MPG)",
y = "Frequency",
caption = "Binwidth = 2"
) +
theme(
plot.title = element_text(face = "bold", size = 16, color = "#222222"),
plot.subtitle = element_text(size = 11, color = "#666666", margin = margin(b = 15)),
axis.title.x = element_text(margin = margin(t = 10), size = 11, face = "bold"),
axis.title.y = element_text(margin = margin(r = 10), size = 11, face = "bold"),
panel.grid.major.y = element_line(color = "#eaeaea"),
plot.margin = margin(20, 20, 20, 20)
)
# Add density line
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
) +
labs(
title = "Distribution of Fuel Efficiency",
subtitle = "Data source: Motor Trend Car Road Tests (mtcars)",
x = "Miles Per Gallon (MPG)",
y = "Frequency",
caption = "Binwidth = 2"
) +
theme(
plot.title = element_text(face = "bold", size = 16, color = "#222222"),
plot.subtitle = element_text(size = 11, color = "#666666", margin = margin(b = 15)),
axis.title.x = element_text(margin = margin(t = 10), size = 11, face = "bold"),
axis.title.y = element_text(margin = margin(r = 10), size = 11, face = "bold"),
panel.grid.major.y = element_line(color = "#eaeaea"),
plot.margin = margin(20, 20, 20, 20)
) +
geom_density(
aes(y = after_stat(count) * 2),
color = "#e15759",
size = 1,
adjust = 1.2
)
mtcars
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 2,
fill = "royalblue",
color = "white"
)
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 20,
fill = "royalblue",
color = "white"
)
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 50,
fill = "royalblue",
color = "white"
)
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 75,
fill = "royalblue",
color = "white"
)
# Add title
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram() +
ggtitle("MTCARS MPG")
# Add title
ggplot(mtcars, mapping = aes(x = mpg)) +
geom_histogram() +
ggtitle("MTCARS Miles per gallon")
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 75,
fill = "royalblue",
color = "white"
) +
ggtitle("MTCARS Displacement Histogram")
colors()
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 75,
fill = "violetred",
color = "white"
) +
ggtitle("MTCARS Displacement Histogram")
# Adjust bins
ggplot(mtcars, mapping = aes(x = disp)) +
geom_histogram(
binwidth = 75,
fill = "royalblue2",
color = "white"
) +
ggtitle("MTCARS Displacement Histogram")
proc_freq(cls, tables = v(Sex, Group, Sex * Group),
plots = TRUE)
library(sassy)
cls <- read.table(header = TRUE, text = '
Name Sex Age Height Weight Group
Alfred   M  14   69.0  112.5 A
Alice    F  13   56.5   84.0 A
Barbara  F  13   65.3   98.0 A
Carol    F  14   62.8  102.5 A
Henry    M  14   63.5  102.5 A
James    M  12   57.3   83.0 A
Jane     F  12   59.8   84.5 A
Janet    F  15   62.5  112.5 A
Jeffrey  M  13   62.5   84.0 A
John     M  12   59.0   99.5 B
Joyce    F  11   51.3   50.5 B
Judy     F  14   64.3   90.0 B
Louise   F  12   56.3   77.0 B
Mary     F  15   66.5  112.0 B
Philip   M  16   72.0  150.0 B
Robert   M  12   64.8  128.0 B
Ronald   M  15   67.0  133.0 B
Thomas   M  11   57.5   85.0 B
William  M  15   66.5  112.0 B')
proc_freq(cls, tables = v(Sex, Group, Sex * Group),
plots = TRUE)
proc_means(cls, var = Height, by = Sex)
proc_ttest(cls, var = Height, class = Sex, plots = TRUE)
tbl <- create_table(mtcars) |>
titles("My Table") |>
footnotes("My footnote")
rpt <- create_report("./Course/04 Sassy/output/Ex3.rtf",
font = "Arial", output_type = "RTF") |>
add_content(tbl)
write_report(rpt)
# Define data library
libname(sdtm, "./Course/04 Sassy/data", "csv")
dat <- sdtm$DM
# Get one-way frequency counts
proc_freq(dat, tables = v(SEX, RACE, ARM))
# Get two-way frequency counts
proc_freq(dat, tables = v(SEX * ARM, RACE * ARM))
# Show default plots
proc_freq(dat, tables = v(SEX * ARM, RACE * ARM),
plots = TRUE)
# Show custom plots
proc_freq(dat, tables = v(SEX * ARM, RACE * ARM),
plots = freqplot(twoway = "cluster"))
# Get summary statistics
proc_means(dat, var = AGE,
by = ARM, stats = v(n, mean, median, std, min, max))
# Sample data
# Create sample data
cls <- read.table(header = TRUE, text = '
Name Sex Age Height Weight Group
Alfred   M  14   69.0  112.5 A
Alice    F  13   56.5   84.0 A
Barbara  F  13   65.3   98.0 A
Carol    F  14   62.8  102.5 A
Henry    M  14   63.5  102.5 A
James    M  12   57.3   83.0 A
Jane     F  12   59.8   84.5 A
Janet    F  15   62.5  112.5 A
Jeffrey  M  13   62.5   84.0 A
John     M  12   59.0   99.5 B
Joyce    F  11   51.3   50.5 B
Judy     F  14   64.3   90.0 B
Louise   F  12   56.3   77.0 B
Mary     F  15   66.5  112.0 B
Philip   M  16   72.0  150.0 B
Robert   M  12   64.8  128.0 B
Ronald   M  15   67.0  133.0 B
Thomas   M  11   57.5   85.0 B
William  M  15   66.5  112.0 B')
# Perform ttest
proc_ttest(cls, var = Height,
class = Sex, plots = TRUE)
# Perform regression
proc_reg(cls, model = "Weight = Height",
plots = regplot(id = Name, label = TRUE))
source("C:/Studies/WUSS/Course/04 Sassy/4.1 Demo.R")
source("C:/Studies/WUSS/Course/04 Sassy/4.1 Demo.R")
ggplot(mtcars, aes(x=cyl, y=mpg)) + geom_point()
mydat2 <- as_tibble(iris)
# Declare 2 vectors
v1 <- c(3, 5, 9, 2, 1)
v1
v2 <- c(4, 6, 8, 2, 0)
v2
# Basic mathematical operators
v1 + 2
v1 - 2
v1 * 2
v1 / 2
# Math with vectors
v1 + v2
v1 - v2
v1 * v2
v1 / v2
# Comparison Operators
v1 > 2
v1 > v2
v1 == v2
# Descriptive statistics
mean(v1)
sd(v1)
min(v1)
max(v1)
summary(v1)
# Reference an element of a vector
v1[2]
# Multiple elements
v1[c(2, 3, 4)]
v1 > 2
# Subset
v1[v1 > 2]
# Subset and assignment
v3 <- v1[v1 > 2]
v3
mean(v3)
# Update a vector
v1[2] <- 4
v1
# Character vector
v4 <- c("A", "B", "C", "A", "B")
v4
# Factors are used for Categorical Data
f1 <- factor(c("A", "B", "C", "A", "B"))
f1
# Get Frequency Counts
table(f1)
# Get Frequency Counts
table(f1)
# Lists can hold anything
l1 <- list("A", 1, "B", 2, Sys.Date())
l1
# Reference a list item
l1[[1]]  # First item in list
# Named list
l2 <- list(Vector1 = v1, Vector2 = v2)
l2
# Access named list item
l2[["Vector1"]]
l2$Vector1
v1
v2
f1
# Data frame from vectors
df1 <- data.frame(col1 = v1, col2 = v2, col3 = f1)
df1
# Class data frame
class(df1)
# Actually a list
typeof(df1)
# Data frame structure
str(df1)
# View column names
names(df1)
# Get rows and columns
nrow(df1)
ncol(df1)
# How to reference a variable/column
df1[[1]]
df1[["col1"]]
df1$col2
# Subset operators on data frame
df1[c(2, 3), c("col1", "col2")]
c(2, 3)
c("col1", "col2")
# Subset operators on data frame
df1[c(2, 3), c("col1", "col2")]
df1$col1 > 2
df1[df1$col1 > 2, ]
df1[2, "col1"]  # Returns a vector value
# Statistics on a variable/column
mean(df1$col1)
table(df1$col3)
# Statistics on data.frame
summary(df1)
# Create a new column on a data frame
df1$col4 <- v1 + 1
df1
l2
# Create data frame from list
df2 <- as.data.frame(l2)
df2
class(df2)
df2
class(df2)
l3 <- unclass(df2)
l3
class(df2)
l3 <- unclass(df2)
l3
f1
# Vector/Factor attributes
attributes(f1)
df1
# Data frame attributes
attributes(df1)
# Special attribute functions
names(df1)
row.names(df1)
class(df1)
# Custom attributes
attr(df1$col1, "label") <- "First Column"
attr(df1$col2, "label") <- "Second Column"
attr(df1$col3, "label") <- "Third Column"
attr(df1$col4, "label") <- "Fourth Column"
df1
attributes(df1$col1)
View(df1)
# Missing value
x <- NA
# NA can be contained in a vector
v5 <- c(3, 2, 4, NA, 6)
v5
# Test for missing
is.na(v5)
# Test for not missing
!is.na(v5)
# This doesn't work
x == NA
# NULL value
y <- NULL
# Test for NULL
is.null(y)
is.null(df1)
# This doesn't work
y == NULL
!is.null(y)
is.null(df1)
# This doesn't work
y == NULL
# Create a sequence
s1 <- seq(1, 10)
s1
# Shortcut syntax
s2 <- 1:10
s2
# Descending sequence
seq(10, 1)
# Negative sequence
seq(-1, -10)
# Vector of logical values
v4 <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
v4
typeof(v4)
v4
# Subset a vector
v1[v4]
v1 > 2
# Subset expression
v1[v1 > 2]
# Subset a data frame
df3 <- df1[v4, ]
df3
df3 <- df1[v1 > 2, ]
df3
# Get current date
Sys.Date()
# What is it?
v5 <- Sys.Date()
typeof(v5)
class(v5)
# Get current time
v5 <- Sys.time()
typeof(v5)
class(v5)
# POSIXlt date
v6 <- as.POSIXlt(v5)
v6
attributes(v6)
# Pick apart
v6$mon
v6$mday
v6$year
v6$hour
v6$min
