简体   繁体   English

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

[英]How to modify legends of multiple Plotly graphs in R

I have many individual ggplotly objects, some of them with legends and some without. 我有很多单独的ggplotly对象,其中有些带有图例,有些没有。

I want to create a single view of shared X axis for these ggplotly objects.(i am using subplot() of plotly for this ) 我想为这些ggplotly对象创建共享X轴的单个视图。(我为此使用了plotly的subplot())

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. 当单个ggplotly对象没有图例时,不要添加图例。

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. 我没有使用plotly的子图功能,而是发现闪亮的仪表板是更好的解决方案。 It can directly take multiple plots not just from plotly but also from ggplot objects. 它可以直接从多个绘图中直接获取多个绘图,还可以从ggplot对象中直接获取多个绘图。

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. 除此之外,我发现ShinyDashboard还有助于更好地构建光泽应用程序的UI。

I fiddled around flexDashboard but din't find it interactive enough as shiny dashboard for my needs. 我摆弄了flexDashboard,但发现它作为交互式仪表板的互动性不足,无法满足我的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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