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

Color mapping disappears using dropdowns #803

Closed
alanocallaghan opened this issue Nov 21, 2016 · 6 comments
Closed

Color mapping disappears using dropdowns #803

alanocallaghan opened this issue Nov 21, 2016 · 6 comments

Comments

@alanocallaghan
Copy link
Contributor

I am trying to create a plotly graph with selectable x, y and color variables, based in part on this. The x and y variable selection appears to work, however when both new x and y variables are selected, the point color is lost.
Example:

library(plotly)
library(pcaMethods)
pca <- pcaMethods::pca(mtcars, nPcs=3)

df <- as.data.frame(pca@scores)

colors1 <- sample(c("red", "green", "blue"), nrow(df), replace=TRUE)
colors2 <- sample(c("red", "green", "blue"), nrow(df), replace=TRUE)

p <- plotly::plot_ly(df, x = ~PC1, y = ~PC2, type = "scatter",
    color = sample(c("red", "green", "blue"), nrow(df), replace=TRUE),
    mode = "markers") 

p <-  plotly::layout(
    p,
    title = "Extended PCA plot",
    updatemenus = list(
      list(
        y = 0.7,
        buttons = list(
            list(
                method = "update",
                args = list(
                    "x", list(df3$PC1)),
                label = "PC1"),
            list(
                method = "update",
                args = list(
                    "x", list(df3$PC2)),
                label = "PC2"),
            list(
                method = "update",
                args = list(
                    "x", list(df3$PC3)),
                label = "PC3"),
            list(
                method = "update",
                args = list(
                    "x", list(df3$PC4)),
                label = "PC4"),
            list(
                method = "update",
                args = list(
                    "x", list(df3$PC5)),
                label = "PC5")
            )
        ),
        list(
            y = 0.5,
            buttons = list(
            list(
                method = "update",
                args = list(
                    "y", list(df3$PC1)),
                label = "PC1"
                ),
            list(
                method = "update",
                args = list(
                    "y", list(df3$PC2)),
                label = "PC2"
                ),
            list(
                method = "update",
                args = list(
                    "y", list(df3$PC3)),
                label = "PC3"
                ),
            list(
                method = "update",
                args = list(
                    "y", list(df3$PC4)),
                label = "PC4"
                ),
            list(
                method = "update",
                args = list(
                    "y", list(df3$PC5)),
                label = "PC5"
                )
            )
        ),
        list(
            y = 0.2,
            buttons = list(
                list(
                    method = "update",
                    args = list(
                        "marker.color", list(df3[["a"]]),
                        "color", list(df3[["a"]]),
                        "text", list(df3[["a"]])
                        ),
                    label = "a"
                    ),
                list(
                    method = "update",
                    args = list(
                        "marker.color", list(df3[["b"]]),
                        "text", list(df3[["b"]])
                        ),
                    label = "b"
                    ),
                list(
                    method = "update",
                    args = list(
                        "marker.color", list(df3[["c"]]),
                        "marker.text", list(df3[["c"]])
                        ),
                    label = "c"
                    )
            )
        )
    )
)

htmlwidgets::saveWidget(p, "test.html", selfcontained=FALSE)
@alanocallaghan
Copy link
Contributor Author

Additionally, selecting a new color set using "marker.color" does not seem to cause the legend to be updated.

@alanocallaghan
Copy link
Contributor Author

Mouseover text also cannot be changed using [...], args = list("text" = text_factor)

More minimal reprex:
https://jsfiddle.net/kck84d1t/

library(plotly)
library(pcaMethods)
pca <- pcaMethods::pca(mtcars, nPcs=3)

df <- as.data.frame(pca@scores)

p <- plotly::plot_ly(df, x = ~PC1, y = ~PC2, type = "scatter",
    color = as.factor(mtcars[, "cyl"]),
    hoverinfo = "text",
    text = as.factor(mtcars[, "cyl"]),
    mode = "markers")

p <-  plotly::layout(
    p,
    title = "Dropdown PCA plot",
    updatemenus = list(
        list(
            y = 0.75,
            buttons = list(
                list(
                    method = "restyle",
                    args = list(
                        "marker.color", list(mtcars[["cyl"]]),
                        "text", list(factor(mtcars[["cyl"]]))
                    ),
                    label = "cyl"
                ),
                list(
                    method = "restyle",
                    args = list(
                        "marker.color", list(mtcars[["gear"]]),
                        "text", list(factor(mtcars[["gear"]]))
                    ),
                    label = "gear"
                )
            )
        )
    )
)

htmlwidgets::saveWidget(p, "test.html", selfcontained=TRUE)

@cpsievert
Copy link
Collaborator

pcaMethods is not on CRAN...can u provide a more minimal example?

@alanocallaghan
Copy link
Contributor Author

Apologies, I didn't realise. Of course. I should note, I haven't tested this with plotly.js so I don't know if it's an R interface issue. If so I'll migrate to their github

@alanocallaghan
Copy link
Contributor Author

The following example uses base PCA. From playing around with using restyle to reassign x and y it seems like it might be a plotly problem but I've not had time to investigate fully. Cheers

library(plotly)

pca <- princomp(mtcars, nPcs=3)

df <- as.data.frame(pca$scores)

p <- plotly::plot_ly(df, 
    x = ~Comp.1, 
    y = ~Comp.2, 
    type = "scatter",
    color = as.factor(mtcars[, "cyl"]),
    hoverinfo = "text",
    text = as.factor(mtcars[, "cyl"]),
    mode = "markers")

p <-  plotly::layout(
    p,
    title = "Dropdown PCA plot",
    updatemenus = list(
        list(
            y = 0.75,
            buttons = list(
                list(
                    method = "restyle",
                    args = list(
                        "marker.color", list(mtcars[["cyl"]]),
                        "text", list(factor(mtcars[["cyl"]]))
                    ),
                    label = "cyl"
                ),
                list(
                    method = "restyle",
                    args = list(
                        "marker.color", list(mtcars[["gear"]]),
                        "text", list(factor(mtcars[["gear"]]))
                    ),
                    label = "gear"
                )
            )
        )
    )
)

@cpsievert
Copy link
Collaborator

color/symbol/linetype/size/etc mapping (from data values to color codes) happen on the R side of things, so once the plot object gets passed to the browser, it loses information about those mapping(s), so I'm afraid something like this won't be possible...unless you do the mapping yourself

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