spurious warning when mapping marker size in plotly R

南楼画角 提交于 2020-12-09 08:39:02

问题


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:

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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/52692760/spurious-warning-when-mapping-marker-size-in-plotly-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!