Rob Kabacoff
Saturday, April 23, 2022
Categorical vs. Categorical
Quantitative vs. Quantitative
Categorical vs. Quantitative
faceting
bubble chart
correlation matrix
3D scatter plot
mosaic chart
Two categorical variables
ggplot(data, aes(x =, color = )) +
geom_bar(position =)
Position
Two quantitative variables
ggplot(data, aes(x =, y = )) +
geom_point()
Common options
ggplot(data, aes(x =, y = )) +
geom_point() +
geom_smooth(method = , formula = , se =)
For geom_smooth
ggplot(data, aes(x =, y = )) +
geom_boxplot()
Common options
ggplot(data, aes(x =, y =)) +
geom_violin()
Common options
library(dplyr)
plotdata <- data %>%
group_by(x) %>%
summarize(n = n(),
mean = mean(y),
se = sd(y) / sqrt(y))
ggplot(plotdata,
aes(x = rank, y = mean, group = 1)) +
geom_point() +
geom_line() +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se),
width = )
ggplot(data, aes(x =, y =, fill = )) +
geom_density(alpha = )
Common options
library(ggridges)
ggplot(data,
aes(x = , y =, fill =)) +
geom_density_ridges() +
theme_ridges() +
theme(legend.position = "none")
ggplot(data, aes(x =, y = )) +
geom_jitter()
library(ggbeeswarm)
ggplot(data, aes(x = , y =)) +
geom_quasirandom()
Common options
Categorical variable with many levels vs. quantiative variable
ggplot(plotdata, aes(x=quantvar, y=reorder(catvar, quantvar))) +
geom_point()
3 quantitative variables
ggplot(data, aes(x = , y = , size = )) +
geom_point()
Common options
+ scale_size_continuous(range = c(1, 10))
r <- cor(df, use="complete.obs")
library(ggcorrplot)
ggcorrplot(r,
hc.order = TRUE, # order variables into clusters?
type = "lower", # upper, lower, or full matrix?
lab = TRUE) # include numeric labels?
2 or more categorical variables
library(vcd)
mosaic(~var1 + var2 + ..., data, shade=TRUE)
hard to read after 4 variables
static
library(scatterplot3d)
with(data, scatterplot3d(x=, y=, z=))
interactive
library(car)
with(data, scatter3d(x=, y=, z=))