I want to scale my y-var by multiplying it by a number, say 10, in ggplot. The problem is this is in a Shiny app and the variable must be passed as a character string, i.e. input$variable
.
How can I multiply one of the variables in aes_string()
the same way I would in aes()
? Here is an example of when it fails:
library(ggplot2)
ggplot(data = subset(mtcars, cyl == 4), aes_string(x = "wt", y = "mpg")) +
geom_line(size = 1.5, color = "#00868B") +
geom_line(data = subset(mtcars, cyl == 8), aes_string(x = "wt", y = "mpg" * 10))
Error in "mpg" * 10 : non-numeric argument to binary operator
You can use
tidyeval
approach introduced inggplot2 v3.0.0
Or put everything in a function
Created on 2018-06-06 by the reprex package (v0.2.0).
I prefer to use
get
instead ofaes_string
to call variables insideggplot2
and it works with value modification, for example:PS: you don't need to call
wt
in secondaes
as it's the same "variable" as in firstaes
.