Skip to content

Instantly share code, notes, and snippets.

@briatte
Forked from expersso/pairs_plot.R
Created May 8, 2019 13:40
Show Gist options
  • Save briatte/bba7b6785bc9cddf4fc07c0b8831978b to your computer and use it in GitHub Desktop.
Save briatte/bba7b6785bc9cddf4fc07c0b8831978b to your computer and use it in GitHub Desktop.
Create scatterplot matrix using the tidyverse
library(tidyverse)
library(patchwork)
plot_pair <- function(data, x, y) {
ggplot(data, aes_string(x = x, y = y, color = "Species", shape = "Species")) +
geom_point() +
scale_color_brewer(palette = "Dark2") +
theme(legend.position = "none", plot.title = element_text(size = 7)) +
labs(x = NULL, y = NULL, title = paste0(y, " ~ ", x))
}
df <- names(iris) %>%
head(-1) %>%
list(x = ., y = .) %>%
cross_df() %>%
mutate(data = list(iris)) %>%
mutate(p = pmap(list(data, x, y), plot_pair))
# Add legend to last plot
df$p[[nrow(df)]] <- df$p[[nrow(df)]] + theme(legend.position = "right")
reduce(df$p, `+`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment