9

The simple scatterplot has a third variable mapped to the marker/point size. The plot looks perfect to me, but it throws a warning about multiple values. Each x & y value has exactly one size value.

Other than suppressing the warning, can a respecify this graph so it does not throw the warning?

Warning message:
`line.width` does not currently support multiple values.

Code:

plotly::plot_ly(
  data  = iris, 
  x     = ~Sepal.Length, 
  y     = ~Petal.Length, 
  size  = ~Sepal.Width,
  type  = 'scatter', 
  mode  = 'markers'
)

Graph: scatterplot

Note: This may be related to Plotly R - error "`line.width` does not currently support multiple values." or Scatter mapbox in shiny R will not render, but those questions have a lot more moving pieces, so I don't know if this is their core problem.

edit: I've since posted this question at https://github.com/ropensci/plotly/issues/1367

wibeasley
  • 5,000
  • 3
  • 34
  • 62

2 Answers2

6

I had the same issue as well. Looking in the plotly github page, as suggested here: https://www.reddit.com/r/RStudio/comments/9xq4gv/plotly_api_error_client_error_400_bad_request/, and searching for the error, led me to the code that produces it:

# if fill does not exist, `size` controls line.width
      if (!has_fill(trace) && has_attr(type, "line")) {
        s <- if (isSingular) size_ else if (array_ok(attrs$line$width)) sizes else NA
        if (is.na(s)) {
          warning("`line.width` does not currently support multiple values.", call. = 
    FALSE)
        } else {
          trace[["line"]] <- modify_list(list(width = default(s)), trace[["line"]])
        }
      }

The parameter fill defaults to "none". Setting this to an empty string, and setting
sizemode = ~diameter in marker worked for me:

plotly::plot_ly(
data   = iris, 
x      = ~Sepal.Length, 
y      = ~Petal.Length,
type   = 'scatter', 
mode   = 'markers',
size = ~Sepal.Width,
fill = ~'',
marker = list(sizemode = 'diameter'))
franky
  • 76
  • 1
  • 4
  • 1
    Welcome to Stack Overflow and thanks for digging into the plotly code. I still get the "`line.width` does not currently support multiple values." error messages when pasting & executing your code in a fresh R session. I tried with the latest version of plotly from both CRAN and GitHub. However, the warning disappears when I uncomment the `fill` line. I assume it's supposed to be uncommented? – wibeasley Nov 12 '20 at 16:30
  • Sorry, yes its supposed to be uncommented. Thank you for pointing that out! – franky Nov 12 '20 at 19:10
5

I've mostly used Plotly in Python so I'm not sure about the details, but size is a property of a number of things in Plotly. I'm guessing that by setting size = ~Sepal.Width at that level the library cannot know you want to set the markers size.

plotly::plot_ly(
    data   = iris, 
    x      = ~Sepal.Length, 
    y      = ~Petal.Length,
    type   = 'scatter', 
    mode   = 'markers',
    marker = list(
        size = ~Sepal.Width*3
    )
)

This worked for me, for some reason the points got a lot smaller but scaling them works fine.

vlizana
  • 2,962
  • 1
  • 16
  • 26
  • Great, that helps for the current task. For my general plotly knowledge, I'd like know how to *map* the size ([in contrast to *set*](https://www.r-bloggers.com/ggplot2-mapping-vs-setting/)) so the default sizes are reasonable, regardless if the sepal widths are measured in cm or km. – wibeasley Oct 07 '18 at 22:03
  • I tend to do this manually dividing by the max value and multiplying by a constant, I'm not aware of Plotly supporting this feature yet. – vlizana Oct 07 '18 at 22:31
  • I think it's already supporting this feature (ie, autosizing the mapped size) as shown in the post's image. For instance, those small dots around (5, 3) are some of the narrowest sepal width values. I like the plot, just not the warning. But your approach does provide a good work-around for my current project. – wibeasley Oct 08 '18 at 00:45
  • For future reference, one can also tune the size scale using `sizeref` and `sizemin` options. See: https://plot.ly/r/bubble-charts/#scaling-using-sizeref – José Luiz Ferreira Jul 04 '19 at 16:02