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?

gbg
  • 69
  • 5
  • 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. – r2evans Jul 18 '22 at 17:39
  • okay, thank you! I just removed it. No plots are showing up still--do you know why that would happen? I'm able to generate a scatterplot when I type the actual variable name into the ggplot function. – gbg Jul 18 '22 at 17:41
  • 1
    Remove the `print`s, 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. – Limey Jul 18 '22 at 17:44
  • Also, note that `aes_string` is soft deprecated. [Possible duplicate](https://stackoverflow.com/questions/69561396/programming-with-ggplot-using-aes-or-aes-string-with-special-characters-in-colu) – Limey Jul 18 '22 at 17:47
  • Thanks--it runs with aes_string (with no errors), but again no graphs are produced. Is there a reason that my graph wouldn't be appearing? – gbg Jul 18 '22 at 17:52
  • 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. – Limey Jul 18 '22 at 17:58
  • If you want either @r2evans or me to see your comment, you need to tag us explicitly. Otherwise you rely on our continued curiosity to see it. – Limey Jul 18 '22 at 17:59
  • @Limey thank you I got it!!! :) I used aes_string and explicitly coded print(plot1) into my for loop. – gbg Jul 18 '22 at 18:17
  • 1
    `aes_string` and `aes_` are [soft-deprecated](https://ggplot2.tidyverse.org/reference/aes_.html#life-cycle), suggesting instead that the preferred method is [quasiquotation in`aes` itself](https://ggplot2.tidyverse.org/reference/aes.html#quasiquotation). – r2evans Jul 18 '22 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);`. – r2evans Jul 18 '22 at 18:34

0 Answers0