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?
x=andy=aesthetics each and every time you plot, removeprint(.). It doesn't break things (sinceprintidempotently and invisibly returns its argument, but it's just overhead (and noise) you generally don't need. It adds nothing.prints, as @r2evans suggests, and useaes_stringinstead ofaes.aesexpects symbols as arguments.aes_string, would you believe, expects - well- strings. Ok, characters. But you get the point.forloops 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.aes_stringandaes_are soft-deprecated, suggesting instead that the preferred method is quasiquotation inaesitself.ggplotplot happens when it is printed. This is implicit when called by itself on the console, as inggplot(...) + 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 ingg <- ggplot(...) + geom_point(...); print(gg);.