Skip to content

Instantly share code, notes, and snippets.

@sarahromanes
Created December 6, 2019 05:09
Show Gist options
  • Save sarahromanes/143a10e0fed8f879565d7e87bf7a9c4d to your computer and use it in GitHub Desktop.
Save sarahromanes/143a10e0fed8f879565d7e87bf7a9c4d to your computer and use it in GitHub Desktop.
using ggMarginal
library(tidyverse)
library(ggExtra)
library(mvtnorm)
# Generate MVN data
s <- matrix(c(4,2,2,3), ncol = 2)
s1 <- matrix(c(3, -0.6,-0.6, 1), ncol=2)
s2 <- matrix(c(2,-0.08,-0.08,1), ncol = 2)
m1 <- c(0, 0)
m2 <- c(-3, 4)
m3 <- c(2,3)
n <- 1000
set.seed(42)
x1 <- rmvnorm(n = n, mean = m1, sigma = s2)
x2 <- rmvnorm(n = n, mean = m2, sigma = s1)
x3 <- rmvnorm(n = n, mean = m3, sigma = s)
d <- data.frame(rbind(x1,x2,x3))
d$class <- as.factor(rep(c("1", "2", "3"), each = 1000))
# Base ggplot of MVNS
p <- ggplot(d, aes(x = X1, y = X2, group = class, color =class)) +
geom_point(alpha = .5) +
geom_density_2d() +
scale_colour_brewer(palette = "Dark2") +
theme_minimal() +
theme(text = element_text(size=20),
legend.position = "bottom")
# Add in marginals
ggMarginal(p, groupFill = TRUE, groupColour = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment