Created
October 29, 2018 16:26
-
-
Save expersso/4a8e1ebd19493c2e6dc96d1a5ddee670 to your computer and use it in GitHub Desktop.
Create scatterplot matrix using the tidyverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, `+`) |
Author
expersso
commented
Oct 29, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment