Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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