in Education by
I've been trying to use plot_ly with transforms (ultimately to subset data from a dropdown menu) and a color mapping. Following is a reproducible example based on mtcars: transforms without color mapping If I exclude the color mapping, everything works as expected: library(plotly) library(tidyverse) plot_ly( data = mtcars %>% rownames_to_column("id"), x = ~mpg, y = ~disp, text = ~id, type = "scatter", mode = "markers+text", marker = list(size = 10, opacity = 0.8), transforms = list( list( type = "filter", target = ~cyl, operation = "=", value = sort(unique(mtcars$cyl))[1]))) Data is filtered for mtcars$cyl values matching sort(unique(mtcars$cyl))[1] = 4. So far so good. transforms with color mapping I now like to color-code markers based on the number of carburetors from mtcars$carb; if I add a color mapping, I end up with library(plotly) library(tidyverse) plot_ly( data = mtcars %>% rownames_to_column("id"), x = ~mpg, y = ~disp, text = ~id, color = ~as.factor(carb), type = "scatter", mode = "markers+text", marker = list(size = 10, opacity = 0.8), transforms = list( list( type = "filter", target = ~cyl, operation = "=", value = sort(unique(mtcars$cyl))[1]))) Somehow, the transforms filter is ignored, as the plot shows entries from mtcars where cyl != 4. I actually don't understand which element plot_ly decides to show here. Would somebody be able to provide any insight? Many thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It doesn't seem to like the factor in the color in combination with the filter. It looks like stuff is getting shuffled and screwed up, e.g. color = ~factor(carb, levels = unique(carb)) will give you yet another selection and plot. Below is working with a factor (including the color in the marker). Specifying just color = ~carb would work as well. I understand none of these really give you the same plot / legend though. library(plotly) library(magrittr) plot_ly( data = mtcars %>% tibble::rownames_to_column("id"), x = ~mpg, y = ~disp, text = ~id, type = "scatter", mode = "markers+text", marker = list(color = ~as.factor(carb), size = 10, opacity = 0.8), transforms = list( list( type = "filter", target = ~cyl, operation = "=", value = sort(unique(mtcars$cyl))[1]))) Edit You can apparently work around the re-shuffling problem by sorting your data first according to the factor you desire to use. The code below works fine in this respect, although I still don't like having all the levels in the legend. Hope it's of some help nonetheless: library(plotly) library(magrittr) xx <- mtcars[order(mtcars$carb), ] plot_ly( data = xx %>% tibble::rownames_to_column("id"), x = ~mpg, y = ~disp, text = ~id, color = ~factor(carb), type = "scatter", mode = "markers+text", marker = list(size = 10, opacity = 0.8), transforms = list( list( type = "filter", target = ~xx$cyl, operation = "=", value = sort(unique(xx$cyl))[1]))) Explanation Looking at plotly:::plotly_build.plotly, we can see that sorting is applied in certain cases, e.g. when working with groups, or discrete colors or symbols. This sorting however does not include the transforms/filter, therefore the two things get out of sync. If you want to look this up, you can also find arrange_safe() within https://github.com/ropensci/plotly/blob/master/R/plotly_build.R. It should obviously be considered a bug, but a good fix is maybe not exactly trivial. Another way of confirming that else it's fine, is if you just mask arrange_safe. E.g. like below: library(godmode) arrange_safe_new <- function(x, y) x godmode:::assignAnywhere("arrange_safe", arrange_safe_new) Afterwards, your case will be fine (but of course you might need the original arrange_safe functionality for other cases, so this is really just to prove a point).

Related questions

0 votes
    I am running the code and it works ggplot(data_df, aes(x= RR, y= PPW, col = year)) + ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    Which of the following transforms can be performed with data value? (a) log2 (b) cos (c) log10 (d) ... -learning-in-Data-Science,Data-Science-Lifecycle,Applications-of-Data-Science...
asked Oct 30, 2021 in Education by JackTerrance
+1 vote
    __________ takes in raw data and transforms into charts, graphs and images that can flawlessly explain numbers in a marvelous way to gain insights from it...
asked Oct 11, 2020 in Education by anonymous
0 votes
    Regex has a character for or but what is the character for and? For example, say I have a folder with the following files: ... to get an and on a regex and got confused. data x...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    A ________________ in R programming language can also contain numeric and alphabets along with special characters like ... Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    R comes with a ________ to help you optimize your code and improve its performance. (a) debugger (b) ... Analysis of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    ________ generate random Normal variates with a given mean and standard deviation. (a) dnorm (b) rnorm (c) ... of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    ___________ provides optional labels with the columns and rows. (a) Disnames (b) Dimnames (c) Denmes (d) ... Out of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following package contains functions for reading and displaying satellite data for oceanographic ... Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    In order to calculate confidence intervals and hypothesis tests, it is assumed that the errors are independent and ... R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am trying to connect to remote site via https and download some information. I am doing this: library("httr") library("XML") library(RCurl) url...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have the following data frame, I want to find the name of the column with the minimum date for ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
...