簡體   English   中英

如何在R中修改多個圖的圖例

[英]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)

實際行為

用紅色突出顯示傳說 在此處輸入圖片說明

預期行為 在此處輸入圖片說明

我如何獲得預期的行為? 請幫忙。

[回答我自己的問題,因為這是我的方向]

我沒有使用plotly的子圖功能,而是發現閃亮的儀表板是更好的解決方案。 它可以直接從多個繪圖中直接獲取多個繪圖,還可以從ggplot對象中直接獲取多個繪圖。

唯一的缺點是我們不能使繪圖在閃亮的儀表板上共享相同的軸。 除此之外,我發現ShinyDashboard還有助於更好地構建光澤應用程序的UI。

我擺弄了flexDashboard,但發現它作為交互式儀表板的互動性不足,無法滿足我的需求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM