I would like to create a function that takes a column name and creates a plot based on that. For example, I want to be able to plot the Petal.Length
column of the iris dataset against other variables. The way to do it without indirection is
ggplot(data = iris) + geom_point(x = Petal.Width, y = Petal.Length)
This is the plot I would like to replicate through indirection, but none of the following attempts work. These two return similar undesired plots:
colname = "Petal.Width"
ggplot(data = iris) + geom_point(x = colname, y = Petal.Length)
ggplot(data = iris) + geom_point(x = {{colname}}, y = Petal.Length)
The following attempt does not work either, it returns an error:
ggplot(data = iris) + geom_point(aes(x = !!!rlang::syms(colname), y = Petal.Length))
#> Warning in geom_point(aes(x = !!!rlang::syms(colname_x), y = Petal.Length)):
#> Ignoring unknown aesthetics:
#> Error in `geom_point()`:
#> ! Problem while setting up geom.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `compute_geom_1()`:
#> ! `geom_point()` requires the following missing aesthetics: x
Any hint on how we could do this? The idea is to have a function that is able to plot that kind of chart just by giving a string corresponding to one x variable that appears in the dataset.