Plotly: Control legends of individual plots within subplots

Created on 14 Dec 2016  路  3Comments  路  Source: ropensci/plotly

With the subplots approach I want to achieve the following

  • Not group all legends into one legend -> Each legend should be within its own pot area
  • Don't add legends when the individual ggplotly object doesnt have one.

Data

df <- structure(list(Tool = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
                                        3L), .Label = c("ToolA", "ToolB", "ToolC"), class = "factor"), 
                     StartTime = structure(c(1456383600, 1464291720, 1456383600, 
                                             1460710380, 1464291780, 1456383600, 1456383600, 1460710380
                     ), class = c("POSIXct", "POSIXt"), tzone = "MET"), Category = structure(c(1L, 
                                                                                               2L, 3L, 1L, 2L, 3L, 4L, 4L), .Label = c("C1", "C2", "C3", 
                                                                                                                                       "null"), class = "factor"), Type = structure(c(4L, 2L, 2L, 
                                                                                                                                                                                      3L, 3L, 2L, 1L, 1L), .Label = c("null", "T1", "T2", "T3"), class = "factor")), .Names = c("Tool", 
                                                                                                                                                                                                                                                                                "StartTime", "Category", "Type"), row.names = c(NA, -8L), class = "data.frame")

> df
   Tool           StartTime Category Type
1 ToolA 2016-02-25 08:00:00       C1   T3
2 ToolA 2016-05-26 21:42:00       C2   T1
3 ToolA 2016-02-25 08:00:00       C3   T1
4 ToolB 2016-04-15 10:53:00       C1   T2
5 ToolB 2016-05-26 21:43:00       C2   T2
6 ToolB 2016-02-25 08:00:00       C3   T1
7 ToolC 2016-02-25 08:00:00     null null
8 ToolC 2016-04-15 10:53:00     null null

Plotly Code

pOne <- ggplotly(ggplot(data=df[df$Tool=="ToolA",],aes(x=StartTime ,y= Tool, colour=Type))+
                   geom_point(alpha=0.8,size=4)) %>% layout(legend = list(orientation = 'h'))

pTwo <- ggplotly(ggplot(data=df[df$Tool=="ToolB",],aes(x=StartTime ,y= Tool,colour=Category))+
                    geom_point(alpha=0.8,size=4)) %>% layout(legend = list(orientation = 'h'))

pThree <- ggplotly(ggplot(data=df[df$Tool=="ToolC",],aes(x=StartTime ,y= Tool))+
                    geom_point(alpha=0.8,size=4)) %>%  layout(showlegend = FALSE)

subplot(pOne,pTwo,pThree, nrows=3, shareX= T,which_layout = 1)

Actual Behaviour

Highlighted the legend in red

capture

Expected Behaviour

untitled

Can you please add this feature, as this is essential

  1. To bring intuitiveness to visualisations, otherswise the business users find it complex to understand the plot with flying/ungrouped/wrongly-positioned legends
  2. To control legends' in such a way that they show in the right position and don't show up when not required

Most helpful comment

@cpsievert I don't think the referred "test" is relevant for this issue.

All 3 comments

@kickbox see this test

@cpsievert I don't think the referred "test" is relevant for this issue.

Legend groups (as that test uses) don't really solve this problem. Legend groups can add a little extra spacing between legend items for separate plots, but with the cost of being able to show/hide individual traces - a major feature of plotly's legends.

This issue seems to be subsumed by: https://github.com/plotly/plotly.js/issues/1668

Was this page helpful?
0 / 5 - 0 ratings