I am trying to create scatterplots where a single plot displays the relationship between each predictor and a single outcome. However, the outcome is not displaying normally. I assume this is because the ggplot function is not recognizing the outcome as a column name. Any advice on how to properly refer to the outcome?
# data
data <- data.frame(o1=rnorm(100, 3, sd=1.2),
o2=rnorm(100, 3.5, sd=1.4),
p1=rnorm(100, 2, sd=1.9),
p2=rnorm(100, 1, sd=1.2),
p3=rnorm(100, 7, sd=1.6)
)
func <- function(data, outcomes, predictors) {
for(i in seq_along(outcomes)){
print(data %>% select(outcomes[[i]], predictors[[i]]) %>%
gather(var, value, -outcomes[[i]]) %>%
ggplot(aes(x=value, y=outcomes[[i]])) + geom_point() + facet_wrap(~var))
}
}
func(data, outcomes=c("o1", "o2"), predictors=list(c("p1", "p2"), c("p2","p3")))
