![]() I expect that a unique legend name controls more than one trace in the plot. However this is not good, because if the legend is hidden the traces cannot be accessed from it. Suggested workarounds: Hide some legend names. Since two traces share a name, they should be mapped to a single name in the legend, and when clicking on that name both traces should get hidden. with 'color', if it is within an aesthetic group it's mapped, but when it is outside that group it is fixed and does not appear in the legend). The result is a plot per trace (correct), mapping of legend to both color and symbol (not desired), and repeated trace names in the legend (I would like the trace names to show as unique names).Įxpected behaviour: No automatic mapping, the user can decide whether to map or not (e.g. I have a plot with a legend with two elements: 'name': 'trace1' and 'name':'trace2' but four actual traces, which I divide in two groups by marker symbol. If traces share names (I think) they should be plotted with a unique legend name and not with one name per trace, repeating the names. The problems: mapping of data to the legend seems automatic and cannot be disabled. Plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species)%>%Īdd_markers(y = ~Sepal.I am not sure whether this is a bug or a feature to request, perhaps it's in between. Variables with missing values: df Sepal.Length Sepal.Width Petal.Length Petal.Width Species Plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%Īdd_markers(y = ~Sepal.Width, showlegend = FALSE) ![]() Plot_ly(x = ~Sepal.Length, color = ~Species, legendgroup = ~Species) %>%Īdd_markers(y= ~Sepal.Width, showlegend = FALSE) that the variables you are using don't contain missing values (NAs).that your data frame is sorted by your grouping variable (fortunately iris data are already sorted by Species),. ![]() Besides the required legendgroup you have therefore to ensure But there is another pitfall which nevertheless prevents the code working. So the data frame must sorted by the variable which is intended to serve as grouping variable. It is a question of sorting not grouping (as Maltas comment above indicates). There seem to be some uncertain points in the answers given until now.įirst of all data frame grouping hasn't any influence as far as I can see it. Writing your code like this is easier when you want to add or remove traces and their respective options, add a grouping variable, or split/summarize your table. You can see that the data is first grouped by Species, passed to the plot_ly() function -which initializes the plot- and then you specify your trace type (markers) to actually make the plot. Regardless of the legend issue, using a single data frame with faceting seems like a more natural approach, given that the grouping variable is the same in each data frame. This practice allows you to better understand how traces works in plotly. I wasn't able to fix the double legend with two separate plots, but you can combine the two data frames to make a single faceted plot. Plot_ly(x=~Sepal.Length, color= ~Species)%>%Īdd_markers(y= ~Sepal.Width, showlegend = F) P1 % and the group_by() function instead of split, as follows: p1 % histogram ( df, x 'sex', y 'totalbill', color 'time', title 'Total Bill by Sex, Colored by Time' ) fig. Your code would now be as follows: require(plotly) By default the legend is displayed on Plotly charts with multiple traces, and this can be explicitly set with the layout.showlegend attribute. But using showlegend option from the plot_ly() function will affect the trace itself, saving its behavior within your subplot. In other words, the layout showlegend option is only taken from your last plot. Layout options found later in the sequence of plots will override options found earlier in the sequence. If we look at the ?subplot documentation: Try to add showlegend = FALSE within the plot_ly() function, not in the layout() one. I'll give you two answers, a straight one, and one for better practice and posterity (which also helps to better understand the problem) :
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |