Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties from second add_trace() are ignored #790

Closed
rtaggart opened this issue Nov 9, 2016 · 2 comments
Closed

Properties from second add_trace() are ignored #790

rtaggart opened this issue Nov 9, 2016 · 2 comments

Comments

@rtaggart
Copy link

rtaggart commented Nov 9, 2016

In the example below, the colors and sizes attributes within add_markers() are ignored.

dat1 <- data.frame(x=1:10, y=rnorm(10))
dat2 <- data.frame(x=1:10, y=rnorm(10), group = c("g1", "g2"))


plot_ly() %>%
  add_lines(data = dat1, x=~x, y=~y, color = I("blue")) %>%
  add_markers(data = dat2, x=~x, y=~y, color = ~group, size = ~(abs(x)+1),
              colors = c("g1" = "green", "g2" = "red"),
              sizes = 30*c(1,4), inherit = FALSE)

If add_lines(data = dat1, x=~x, y=~y, color = I("blue")) %>% is removed, then the markers are rendered perfectly. The same issue occurs if plot_ly() is replaced with plot_geo().

@cpsievert
Copy link
Collaborator

This is sort of a quirk/undocumented, but scales should be specified when initiating the object

plot_ly(colors = c("g1" = "green", "g2" = "red"), sizes = 30*c(1,4)) %>%
    add_lines(data = dat1, x=~x, y=~y, color = I("blue")) %>%
    add_markers(data = dat2, x=~x, y=~y, color = ~group, size = ~(abs(x)+1))

@nitram9
Copy link

nitram9 commented Sep 30, 2021

@cpsievert I cannot believe I just now found this post. The colors = c("g1" = "green", "g2" = "red") example has fixed a problem I have had since over two years!!! Perhaps this information could be added somewhere to an example.

One can also use symbols by setting the following symbols = c("key1" = "symbol1", "key2" = "symbol2") in the first Plotly call.

plot_ly(colors = c("g1" = "green", "g2" = "red"), symbols = c("g1" = "circle", "g2" = "square"), sizes = 30*c(1,4)) %>%
    add_lines(data = dat1, x=~x, y=~y, color = I("blue")) %>%
    add_markers(data = dat2, x=~x, y=~y, color = ~group, symbol = ~group, size = ~(abs(x)+1))

To also change the size of the marker (for when doing add_trace with lines+markers for example), the marker attribute is needed:

plot_ly(colors = c("g1" = "green", "g2" = "red"), symbols = c("g1" = "circle", "g2" = "square"), sizes = 30*c(1,4)) %>%
    add_lines(data = dat1, x=~x, y=~y, color = I("blue")) %>%
    add_markers(data = dat2, x=~x, y=~y, color = ~group, symbol = ~group, size = ~(abs(x)+1), marker = list(size = 20))

The thing that confuses me is why I cannot use the marker attribute to also set the symbol and color such as in the following manner: marker = list(size = 20, symbol = ~group, color = ~group). Perhaps you can explain, @cpsievert ? Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants