How to feed a variable into a loop that have ggplot in it

Hi,

this is becoming pretty much a joke. Spent all day trying to do such a simple thing.

How do you plot a variable through ggplot in a loop?

I have a table called "table_test" which is a result of aggregation for each variable and some statistics derived.

"Plotting function" is a function that plots statistics using "table_test" table.

if I use an actual variable name without any quotation marks - works like a charm
Plotting_function(table_test, Var_name_from_table)

But If I use proxy value from a loop - fails.

Var_from_loop <-  Var_name_from_table
Plotting_function(table_test, Var_from_loop)

I have tried everything I could have though about, my colleagues tried helping me as well. Nothing.... How is this even possible...

Thanks

the simple answer is that using bare (unquoted) names, is for interactive programming; and when you want to do metaprogramming (constructing programs from programs, or data driven) you will need to adapt, and the easiest way is to alter your function to take strings; as you can easily do that interactively anway. whereas to go the other way and get strings converted to 'symbols' will require using things like tidyeval / rlang.
so if we are talking about ggplot2 and your wanting to change a variable, I assume in your aes(); use aes_string() instead.

1 Like

Thanks, this gave me atleast something to grab to work with. Finally managed to do what I needed. Holy smokes, people should talk more about this. I went through all the guidelines I could find and nothing worked

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.