Reputation: 49
When I look at documentation like:
plot_ly(mpg, x = ~cty, y = ~hwy, z = ~cyl) %>%
add_markers(color = ~cyl)
I wonder what the ~
is doing. The mpg data set has variables city, etc so what is wrong with using city
instead of ~city
?
When should I add ~
in front of variable names?
Upvotes: 0
Views: 22
Reputation: 887571
According to ?plot_ly
A formula must always be used when referencing column name(s) in data (e.g. plot_ly(mtcars, x = ~wt)). Formulas are optional when supplying values directly, but they do help inform default axis/scale titles (e.g., plot_ly(x = mtcars$wt) vs plot_ly(x = ~mtcars$wt))
In the context of plot_ly
, it is taking inputs as formula when unquoted columns are provided or can directly give the value of the column by extracting the column (mtcars$wt
or mtcars[["wt"]]
or mtcars[, "wt"]
Upvotes: 2