6
votes

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

1
You need quotes around the namesSotos
Tried with quotes i.e p <- plot_ly(iris, x='Petal.Length', y='Petal.Width',color='Species', mode = "markers"). No error message now but the visual output is different...with below warning message... No trace type specified: Based on info supplied, a 'histogram2d' trace seems appropriate. Read more about this trace type -> plot.ly/r/reference/#histogram2d Warning messages: 1: In RColorBrewer::brewer.pal(N, "Set2") : minimal value for n is 3, returning requested palette with 3 different levels 2: 'histogram2d' objects don't have these attributes: 'mode' Valid attributes.....SPS
Got an update from plot.ly community that the plotly latest version includes many updates and more information present in [link] (moderndata.plot.ly/upgrading-to-plotly-4-0-and-above)SPS
Indeed, the update from 3.6.0 to 4.5.2 includes several syntax changes in the package, including the one you mentioned above. It seems that now we have to reference the variables in a different way.Sorlac

1 Answers

10
votes

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) ).