如何在 R 中修改多个 Plotly 图的图例

How to modify legends of multiple Plotly graphs in R

我有很多单独的 ggplotly 对象,其中一些有图例,一些没有。

我想为这些 ggplotly 对象创建共享 X 轴的单一视图。(我为此使用 plotly 的 subplot())

通过子图方法,我想实现以下目标

  1. 不将所有图例归为一个图例。

  2. 当单个 ggplotly 对象没有图例时,不要添加图例。

示例代码

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

情节

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)

实际行为

用红色突出显示图例

预期行为

如何获得预期的行为?请帮忙。

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

我发现 shiny dashboard 不是使用 plotly 的 subplot 函数,而是更好的解决方案。它不仅可以从 plotly 中直接获取多个图,还可以从 ggplot 对象中直接获取多个图。

唯一的缺点是我们不能让绘图在闪亮的仪表板中共享相同的轴。除此之外,我发现 shinyDashboard 还有助于更好地构建 shiny 应用程序的 UI。

我摆弄了一下 flexDashboard 但发现它的交互性不足以满足我的需求。