Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - plotly error object ... not found

Tags:

r

plotly

I try to run a sample line with iris data set gives object '...' not found error. Is there any I need to check specific in my environment?

library(plotly)
p <- plot_ly(iris, x = Petal.Length, y = Petal.Width,color = Species, mode = "markers")

Error in plot_ly(iris, x = Petal.Length, y = Petal.Width, color = Species, : object 'Petal.Length' not found

like image 941
SPS Avatar asked Oct 07 '16 10:10

SPS


1 Answers

This happen to be known issue reported to plotly. To fix your example you should add tilde "~" to the data frame columns names:

library(plotly)
p <- plot_ly(iris, x = ~Petal.Length, y = ~Petal.Width,color = ~Species, mode = "markers")
p

This should give you:
plot_ly chart of built-in iris data set

Quote from the latest plotly doc for plotly 4.0 and above:

plot_ly() now requires a formula (which is basically an expression, but with a ~ prefixed) when referencing variables. You do not have to use a formula to reference objects that exist in the namespace, but I recommend it, since it helps populate sensible axis/guide title defaults (e.g., compare the output of plot_ly(z = volcano) with plot_ly(z = ~volcano) ).

like image 128
Dmitry Shevkoplyas Avatar answered Oct 17 '22 09:10

Dmitry Shevkoplyas