-
-
Save briatte/bba7b6785bc9cddf4fc07c0b8831978b to your computer and use it in GitHub Desktop.
Create scatterplot matrix using the tidyverse
This file contains hidden or 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, `+`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment