Skip to content

add_trace inherits properties in this example #280

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
chriddyp opened this issue Sep 14, 2015 · 3 comments
Closed

add_trace inherits properties in this example #280

chriddyp opened this issue Sep 14, 2015 · 3 comments

Comments

@chriddyp
Copy link
Member

zcol = c(1, 3, 2)
textcol = c('A', 'B', 'C')
countrycol = c('Canada', 'Egypt', 'USA')
locationscol = c('CAN', 'EGY', 'USA')
df = data.frame(z=zcol, text=textcol, country=countrycol, locations=locationscol)

p <- plot_ly(df, z = z, text = text, locations = locations, type = 'choropleth') %>%
  add_trace(type="scattergeo", mode="text") 
fig <- p %>% plotly_build

the second trace should only have "type" and "mode" keys, but instead:

> names(fig$data[[2]])
[1] "type"       "inherit"    "z"          "text"       "locations"  "colorbar"   "colorscale"
@chriddyp chriddyp added the bug label Sep 14, 2015
@cpsievert
Copy link
Collaborator

If you don't want add_trace() to inherit properties from plot_ly(), you have to specify inherit = FALSE

zcol = c(1, 3, 2)
textcol = c('A', 'B', 'C')
countrycol = c('Canada', 'Egypt', 'USA')
locationscol = c('CAN', 'EGY', 'USA')
df = data.frame(z=zcol, text=textcol, country=countrycol, locations=locationscol)

p <- plot_ly(df, z = z, text = text, locations = locations, type = 'choropleth', inherit = FALSE) %>%
    add_trace(type="scattergeo", mode="text") 
fig <- p %>% plotly_build
fig$data[[2]]
#> $type
#> [1] "scattergeo"
#> 
#> $mode
#> [1] "text"

Do you think the default should be inherit = FALSE?

@chriddyp
Copy link
Member Author

ahhh got it. Not sure about the default, but I think FALSE is the least surprising option and explicit option:

df <- data.frame(x1=c(1, 2, 3), y1=c(3, 1, 6), t1=c('a', 'b', 'c'), x2=c(2, 1, 5), y2=c(5, 4, 5), t2=('x', 'y', 'z')

plot_ly(df, x=x1, y=y1, text=t1) %>% add_trace(x=x2, y=y2)

In this case, the user might've forgotten to add text=t2 in the second call, and might be misled by the inherited hover text.

@cpsievert cpsievert added question and removed bug labels Sep 16, 2015
@cpsievert
Copy link
Collaborator

I'm starting to agree that FALSE might be a better default. Also, inherit=TRUE should mean to inherit the arguments, not actual values. This has important consequences when we have a different data frame for each traces

library(plotly)
library(dplyr)
p <- iris %>%
      count(Species) %>%
      plot_ly(x = Species, y = n, opacity = 0.5, type = "bar") %>%
      layout(barmode = "overlay", showlegend = FALSE)

s <- count(iris[sample(nrow(iris), 10), ], Species)
add_trace(p, data = s)

versus

add_trace(p, x = Species, y = n, data = s)

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

No branches or pull requests

2 participants