0

I have a graph I'm making with multiple calls to x and y using the same dataset.

I want to be able to something like

x <- "variable_1"
y <- "variable_2"
y.min <- 100

so that I can have

data %>%
filter(y > y.min)
ggplot(data, aes(x = x, y = y)) + geom_point()

Therefore, if I change x to "variable_3", running the ggplot will display variable_3 on the x-axis instead of variable_1. Also, if I change the y variable, it will filter correctly

user3304359
  • 335
  • 1
  • 9
  • 2
    This can be done by the aes_string parameter instead of aes: https://stackoverflow.com/questions/19826352/pass-character-strings-to-ggplot2-within-a-function. For dplyr-functions you can use rlang::sym: https://stackoverflow.com/questions/44121728/programming-with-dplyr-using-string-as-input – TinglTanglBob Feb 04 '19 at 15:02
  • Thanks. That works for the ggplot area, but doesn't work for the filter part. What would I use for that? – user3304359 Feb 04 '19 at 15:15
  • 1
    it should be enough to wrinte filter(!!sym(y) > y.min) - hopefully :) – TinglTanglBob Feb 04 '19 at 15:25

1 Answers1

-1

get everything into a string and then execute the string using eval(parse(text = "stringFormula"))

Jorge Lopez
  • 467
  • 4
  • 10