Last active
September 5, 2017 14:17
-
-
Save matt-dray/d99d333d254a9e851f51d3a48c2c1ee8 to your computer and use it in GitHub Desktop.
Loop through plots of specified column pairs of a dataframe
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
# purpose: loop through plots of specified column pairs of a dataframe | |
# don't want to compare all possible permutations | |
# also avoids nested loop of x and y | |
library(tidyverse) # for tibble and ggplot | |
# generate example dataframe | |
test_data <- tibble::tibble(x1 = rnorm(100) | |
, x2 = rnorm(100) | |
, x3 = rnorm(100) | |
, y1 = rnorm(100) | |
, y2 = rnorm(100) | |
, y3 = rnorm(100) | |
) | |
# dataframe of column pairings from test_data to be compared | |
combinations <- tibble::tibble(x = c("x1", "x1", "x2", "x3") | |
, y = c("y1", "y3", "y2", "y1") | |
) | |
# loop through the combinations | |
for (i in 1:nrow(combinations)) { | |
g <- test_data %>% | |
ggplot2::ggplot(aes(x = test_data[, combinations$x[i]] | |
, y = test_data[, combinations$y[i]]) | |
) + | |
geom_point() + | |
labs(title = paste("Plot of" | |
, combinations$x[i] | |
, "and" | |
, combinations$y[i] | |
) | |
, x = combinations$x[i] | |
, y = combinations$y[i]) + | |
scale_x_continuous() + | |
scale_y_continuous() | |
print(g) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment