Here is a sample code for ggplot with facets.
The current code creates a 2 by 4
panel of plots. My most important categorical variable is A. I want to distinctly separate plots based on values of A i.e. 2 sets/nests of 2 by 2
panels based on values of A.
- Is it possible to create a common top panel for same values of A.
Thanks
library(tidyverse)
# continuous variables
x <- runif(160)
y <- runif(160)
# 3 categorical variables, each with 2 levels
A <- c(rep(0, 80), rep(1, 80))
b <- c(rep(0, 40), rep(1, 40), rep(0, 40), rep(1, 40))
c <- c(rep(0, 20), rep(1, 20), rep(0, 20), rep(1, 20),
rep(0, 20), rep(1, 20), rep(0, 20), rep(1, 20))
# tibble
tbl <- tibble(x, y, A, b, c) %>%
# rename the categorical variables for better labels in facet panels
mutate(A = factor(A, levels = c(1, 0), labels = paste0("A = ", c(1, 0)))) %>%
mutate(b = factor(b, levels = c(1, 0), labels = paste0("b = ", c(1, 0)))) %>%
mutate(c = factor(c, levels = c(1, 0), labels = paste0("c = ", c(1, 0))))
# ggplot
ggplot(data = tbl,
aes(x = x,
y = y)) +
geom_point() +
facet_grid(c ~ A + b) +
theme_bw() +
theme(aspect.ratio = 1)
[![enter image description here][1]][1]