Skip to content

Commit da1d9c5

Browse files
20260331 - marginal figures
1 parent 70d04a7 commit da1d9c5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Imports:
6262
furrr,
6363
MplusAutomation,
6464
ggpubr,
65+
patchwork,
6566
naniar,
6667
MBESS,
6768
XICOR,

figures.qmd

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ library("ggExtra")
142142
library("viridis")
143143
library("ggthemes")
144144
library("ggpubr")
145+
library("patchwork")
145146
```
146147

147148
## Simulate Data
@@ -386,6 +387,52 @@ densityMarginal <- ggMarginal(scatterplot, type = "density", xparams = list(fill
386387
print(densityMarginal, newpage = TRUE)
387388
```
388389

390+
(Or we can create the marginal figures manually using the `patchwork` package):
391+
392+
```{r}
393+
densX <- ggplot2::ggplot(
394+
data = df,
395+
aes(
396+
x = predictor)) +
397+
coord_cartesian() + # expand = FALSE (if you want to remove the space between data and axis; if you use, would need to use expand = FALSE for all three plots)
398+
geom_histogram(
399+
aes(y = after_stat(density)),
400+
color = "#000000",
401+
fill = "#0099F8"
402+
) +
403+
geom_density(
404+
color = "#000000",
405+
fill = "#F85700",
406+
alpha = 0.6 # add transparency
407+
) +
408+
theme_void()
409+
410+
densY <- ggplot2::ggplot(
411+
data = df,
412+
aes(
413+
x = outcome)) +
414+
geom_histogram(
415+
aes(y = after_stat(density)),
416+
color = "#000000",
417+
fill = "#0099F8"
418+
) +
419+
geom_density(
420+
color = "#000000",
421+
fill = "#F85700",
422+
alpha = 0.6 # add transparency
423+
) +
424+
theme_void() +
425+
coord_flip() # expand = FALSE (if you want to remove the space between data and axis; if you use, would need to use expand = FALSE for all three plots)
426+
427+
densX + patchwork::plot_spacer() + scatterplot + densY +
428+
patchwork::plot_layout(
429+
ncol = 2,
430+
nrow = 2,
431+
widths = c(4, 1),
432+
heights = c(1, 4)
433+
)
434+
```
435+
389436
### Histogram
390437

391438
```{r}

0 commit comments

Comments
 (0)