简体   繁体   中英

How to modify legends of multiple Plotly graphs in R

I have many individual ggplotly objects, some of them with legends and some without.

I want to create a single view of shared X axis for these ggplotly objects.(i am using subplot() of plotly for this )

With the subplots approach I want to achieve the following

  1. Not group all legends into one legend.

  2. Dont add legends when the individual ggplotly object doesnt have one.

Sample Code

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")

Data

> 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

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 在此处输入图片说明

Expected Behaviour 在此处输入图片说明

How can I get the expected behaviour? Please help.

[Answering my own question, as this is the direction I took]

Instead of using plotly's subplot function, I found shiny dashboard to be a better solution. It can directly take multiple plots not just from plotly but also from ggplot objects.

Only disadvantage is that we cannot make the plots share the same axis in shiny dashboard. Apart from this I found that shinyDashboard also helps in better structuring the UI of the shiny app.

I fiddled around flexDashboard but din't find it interactive enough as shiny dashboard for my needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM