R: How to create a scatterplot using variable names stored in a different dataframe?

137 views Asked by At

I am trying to automate some graphing in R. In summary, I have a large dataset (called myDAT) with many variables and datapoints. I am trying to explore correlations between variables.

I have a correlation matrix which I have converted into a tibble.

Predictors <- c("Pred1", "Pred2", "Pred3")
Outcome1<- c(.4,.6,.3)
Outcome2<- c(.9,.2,.5)

corr.df <- data.frame(Predictors, Outcome1, Outcome2)

I then filtered the correlation matrix and created a new dataframe for correlations with an r above a certain threshold. One column lists predictor variables while the other lists the outcome variable I am exploring. My dataframe looks something like this:

xaxis_var <- c("Pred1", "Pred2", "Pred3")
yaxis_var <- c("Outcome2", "Outcome1", "Outcome2")
var_df <- c(xaxis_var, yaxis_var)

I want to create a scatter plot demonstrating the correlations between these variables with high correlations. Each row of my var_df table, I want to use the xaxis_var as the variable on the x axis on my graph, and the yaxis_var as the variable on the y axis on my graph. The datapoints for the scatterplot should come from the dataframe myDAT.

  for (j in 1:nrow(var_df)) {

    ggplot(myDAT, aes(x= print(var_df$xaxis_var[j]), y= print(var_df$yaxis_var[j])))+
  geom_point()

}

I also tried this:

  for (i in 1:nrow(b)) {
  x_axis <- var_df$Predictors[i]
  y_axis <- var_df$col[i]

    ggplot(myDAT, aes(x_axis, y_axis))+
  geom_point()
  }

I don't get an error, but nothing happens when I run my code. Why isn't it showing?

0

There are 0 answers