Skip to content

Legend not showing for main plot when using add_markers #1262

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

Closed
leungi opened this issue May 23, 2018 · 2 comments
Closed

Legend not showing for main plot when using add_markers #1262

leungi opened this issue May 23, 2018 · 2 comments

Comments

@leungi
Copy link

leungi commented May 23, 2018

The following is sample data and plot_ly() call.

Goal: show two legends - one for main plot (via plot_ly() ) and one for trace (via add_markers())

Issue: only the trace from add_markers() has legend

I've tried the following but to no avail:

  1. using layout(showlegend = TRUE)
  2. add_markers(showlegend = FALSE)
  3. using legendgroup argument

Look forward to advice.

Thanks!

  library(dplyr)
  library(plotly)
  library(RColorBrewer)
  library(tibble)

  tibble::tribble(
                       ~cxn_id,          ~x, ~y, ~z, ~key,   ~enthalpy,   ~pressure,          ~So, ~Ss, ~state,          ~Sw, ~temperature, ~time_id, ~API_NO14, ~API_NO14R,
                            1L,           0, 0L, 5L,   1L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 27.77777778, 0L, 5L,   2L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 55.55555556, 0L, 5L,   3L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 83.33333333, 0L, 5L,   4L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 111.1111111, 0L, 5L,   5L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 138.8888889, 0L, 5L,   6L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 166.6666667, 0L, 5L,   7L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 194.4444444, 0L, 5L,   8L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L, 222.2222222, 0L, 5L,   9L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L,         250, 0L, 5L,  10L,  92.5470681,        1000,          0.6,  0L,     1L,            0,          122,       1L,        0L,        10L,
                            1L,           0, 0L, 5L,   1L, 91.95025793,   884.31628,  0.604071037,  0L,     1L,  0.395928963,  121.7012993,       2L,        0L,        10L,
                            1L, 27.77777778, 0L, 5L,   2L,  92.0084321, 900.9498023, 0.6040059162,  0L,     1L, 0.3959940838,  121.7168734,       2L,        0L,        10L,
                            1L, 55.55555556, 0L, 5L,   3L,  92.0562342, 914.3801565,  0.603953405,  0L,     1L,  0.396046595,  121.7302864,       2L,        0L,        10L,
                            1L, 83.33333333, 0L, 5L,   4L, 92.09014787, 923.8711344, 0.6039163417,  0L,     1L, 0.3960836583,  121.7399005,       2L,        0L,        10L,
                            1L, 111.1111111, 0L, 5L,   5L, 92.10828653, 928.9274416, 0.6038966194,  0L,     1L, 0.3961033806,  121.7450946,       2L,        0L,        10L,
                            1L, 138.8888889, 0L, 5L,   6L, 92.10950726, 929.2714203, 0.6038952728,  0L,     1L, 0.3961047272,  121.7454347,       2L,        0L,        10L,
                            1L, 166.6666667, 0L, 5L,   7L, 92.09340571, 924.8306592, 0.6039125358,  0L,     1L, 0.3960874642,   121.740701,       2L,        0L,        10L,
                            1L, 194.4444444, 0L, 5L,   8L,  92.0606885,  915.735205, 0.6039479817,  0L,     1L, 0.3960520183,  121.7312695,       2L,        0L,        10L,
                            1L, 222.2222222, 0L, 5L,   9L, 92.01265544, 902.3237359, 0.6040003203,  0L,     1L, 0.3959996797,  121.7175755,       2L,        0L,        10L,
                            1L,         250, 0L, 5L,  10L, 91.94997909, 885.1604704, 0.6040674541,  0L,     1L, 0.3959325459,  121.6988436,       2L,        0L,        10L
                       ) -> main_frame
  
p <- plot_ly(main_frame, 
               x = ~x, 
               y = ~y, 
               z = ~-z, 
               color = ~ Sw, 
               frame = ~ time_id, 
               type = 'scatter3d', 
               mode = 'lines', 
               colors = "RdBu",
               legendgroup = ~ Sw,
               showlegend = TRUE) %>% 
    add_markers(data = main_frame,
                x = ~x,
                y = ~y,
                z = ~-z,
                frame = ~time_id,
                color = ~temperature,
                colors = "BuGn",
                text = ~paste('API_NO14: ', API_NO14,
                              '<br>',
                              'Temperature: ', temperature),
                hoverinfo = 'text',
                inherit = FALSE,
                legendgroup = ~ temperature,
                showlegend = TRUE)

p

Created on 2018-05-23 by the reprex package (v0.2.0).

@cpsievert
Copy link
Collaborator

Goal: show two legends

Do you mean to legend entries? I see two entries. Creating two legends is not (yet) possible with plotly.js -- plotly/plotly.js#1668

@leungi
Copy link
Author

leungi commented May 27, 2018

@cpsievert, thanks for confirmation.

Prior to filing issue, I'd looked up several SO posts regarding this matter and found the same conclusion. Given your in-depth knowledge and work on plotly, thought you'd have a workaround :)

Glad to see the 'yet'.

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

2 participants