Skip to content

Instantly share code, notes, and snippets.

@expersso
Created October 29, 2018 16:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save expersso/4a8e1ebd19493c2e6dc96d1a5ddee670 to your computer and use it in GitHub Desktop.
Save expersso/4a8e1ebd19493c2e6dc96d1a5ddee670 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, `+`)
@expersso
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment