2

I want to pass to ggplot an x and a y variable where the names come from another data frame.

I thought it would be possible to use the aes_string() argument and pass the column names from data1 but use the data from data2.

data(iris)
data1 <- iris
data2 <- data

dev.off()
ggplot() +
  geom_point(aes_string(
    x = rlang::eval_tidy(colnames(data1)[1]),
    y = rlang::eval_tidy(colnames(data1)[2])
  ), data = data2
  )

How can I pass the variable names using something similar to eval_tidy?

0

1 Answer 1

2

We can convert to symbol and evaluate (!!) in aes

library(ggplot2)
ggplot() +
  geom_point(aes(
    x = !!rlang::sym(colnames(data1)[1]),
    y = !!rlang::sym(colnames(data1)[2])), data = data2)

NOTE: aes_string is getting deprecated. So, either we pass unquoted argument as quosure and evaluate (!!) or use {{...}} or with strings, convert to symbol and evaluate

Sign up to request clarification or add additional context in comments.

Comments

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.