0

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?

10
  • 1
    Unless you explicitly want to print (anonymously) to the console the variables used for the x= and y= aesthetics each and every time you plot, remove print(.). It doesn't break things (since print idempotently and invisibly returns its argument, but it's just overhead (and noise) you generally don't need. It adds nothing. Commented Jul 18, 2022 at 17:39
  • 1
    Remove the prints, as @r2evans suggests, and use aes_string instead of aes. aes expects symbols as arguments. aes_string, would you believe, expects - well- strings. Ok, characters. But you get the point. Commented Jul 18, 2022 at 17:44
  • 1
    It may be a lazy evaluation problem. (I avoid for loops for this and many other reasons.). Are you creating the plots within a function? If so, you need to explicitly print them. Without a reproducible example, it’s difficult to tell. Commented Jul 18, 2022 at 17:58
  • 1
    aes_string and aes_ are soft-deprecated, suggesting instead that the preferred method is quasiquotation inaes itself. Commented Jul 18, 2022 at 18:32
  • 1
    The rendering of a ggplot plot happens when it is printed. This is implicit when called by itself on the console, as in ggplot(...) + geom_point(..), not capturing its return into an object, and not running it in the middle of a block from which another object is returned. But that's just the implicit method. If you want to be sure to render it, i.e., within a loop of some sort, you must explicitly print it, as in gg <- ggplot(...) + geom_point(...); print(gg);. Commented Jul 18, 2022 at 18:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.