2017-09-26 123 views
0

當我在ggplotly()中爲具有大量構面組的閃亮應用程序執行facet_grid時,情節會混亂。但它在Shiny之外正常工作。R:facet_wrap無法正確顯示ggplotly中的閃亮應用程序

我該如何解決這個問題?
我懷疑它是連接到Y規模,但我找不到解決方案。


下面是基於diamonds example from plotly的可重複的示例。閃亮的

比較與非閃亮輸出:Comparison of facet_grid outside and within Shiny

代碼

外閃亮:

library(ggplot2) 

data(diamonds, package = "ggplot2") 

# new faceting group 
    diamonds$rdmGroup <- as.factor(sample(LETTERS, dim(diamonds)[1], replace=TRUE)) 

# subset of diamonds 
    diamonds <- diamonds[sample(nrow(diamonds), 1000),] 

ggplot(diamonds , aes_string(x = diamonds$x, y = diamonds$y, color = diamonds$x)) + 
     geom_point() + facet_grid(rdmGroup~.) + 
     guides(color=FALSE) + 
     labs(x = "X", y="Y") 


相同的代碼在一個閃亮的應用程序:

library(shiny) 
library(plotly) 
library(ggplot2) 

data(diamonds, package = "ggplot2") 

# new faceting group 
    diamonds$rdmGroup <- as.factor(sample(LETTERS, dim(diamonds)[1], replace=TRUE)) 

# subset of diamonds 
    diamonds <- diamonds[sample(nrow(diamonds), 1000),] 

ui <- fluidPage(
    headerPanel("Diamonds Explorer"), 
    sidebarPanel(
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
       min = 100, max = 2000, value = 1000) 
), 
    mainPanel(
    plotlyOutput('trendPlot') 
) 
) 


server <- function(input, output) { 

    output$trendPlot <- renderPlotly({ 
     p <- ggplot(diamonds, aes_string(x = diamonds$x, y =diamonds$y, color = diamonds$x)) + 
      geom_point()+ facet_grid(rdmGroup~., scales = "free_y") + 
      labs(x = "X", y="Y") 

     ggplotly(p) %>% 
      layout(height = input$plotHeight, autosize=TRUE) 
    }) 
} 
shinyApp(ui, server) 

PS:)我用aes_string(而不是AES()故意,因爲我需要它在我真正的應用程序。

+0

我運行你的例子,當獲得大量的警告。您是否嘗試安裝ggp​​lot2的最新github版本? 「我們建議您使用GGPLOT2的開發版本'ggplotly()' 與安裝它:'devtools :: install_github( '哈德利/ GGPLOT2')'」 – DataJack

+0

我更新GGPLOT2和警告都沒有了。感謝您的建議。但它並沒有解決這個問題。 – Jim

回答

0

首先要注意的是,該問題與Shiny無關,而是您使用ggplotly。這個問題可以只複製:

library(ggplot2) 
library(plotly) 

data(diamonds, package = "ggplot2") 

# new faceting group 
    diamonds$rdmGroup <- as.factor(sample(LETTERS, dim(diamonds)[1], replace=TRUE)) 

# subset of diamonds 
    diamonds <- diamonds[sample(nrow(diamonds), 1000),] 

p <- ggplot(diamonds , aes_string(x = diamonds$x, y = diamonds$y, color = diamonds$x)) + 
     geom_point() + facet_grid(rdmGroup~.) 

ggplotly(p) 

儘管你需要的東西,查看輸出,這也不失爲shiny

在回答你的問題時,問題似乎是你不能超過25個方面。如果您從rdmGroup中刪除任何一個組,則plotly輸出可以正常工作,例如,

diamonds <- subset(diamonds, rdmGroup != "Q") 

要更新您的閃亮的例子:

library(shiny) 
library(plotly) 
library(ggplot2) 

data(diamonds, package = "ggplot2") 

# new faceting group 
diamonds$rdmGroup <- as.factor(sample(LETTERS, dim(diamonds)[1], replace=TRUE)) 

# subset of diamonds 
diamonds <- diamonds[sample(nrow(diamonds), 1000),] 
diamonds <- subset(diamonds, rdmGroup != "Q") 

ui <- fluidPage(
    headerPanel("Diamonds Explorer"), 
    sidebarPanel(
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
       min = 100, max = 2000, value = 1000) 
), 
    mainPanel(
    plotlyOutput('trendPlot') 
) 
) 


server <- function(input, output) { 

    output$trendPlot <- renderPlotly({ 
    p <- ggplot(diamonds, aes_string(x = diamonds$x, y =diamonds$y, color = diamonds$x)) + 
     geom_point()+ facet_grid(rdmGroup~., scales = "free_y") + 
     labs(x = "X", y="Y") 

    ggplotly(p) %>% 
     layout(height = input$plotHeight, autosize=TRUE) 
    }) 
} 
shinyApp(ui, server) 

提供了以下的輸出: output

一種解決方法可以是簡單地擁有多個地塊,將數據集到的25組

編輯:我做了一些更多的研究和情節停止顯示如預期當面板邊緣太la rge以允許顯示所有圖。您可以通過減少panel.spacing.y顯示所有26但這隻能走這麼遠取決於你需要多少行:

p <- ggplot(diamonds, aes_string(x = diamonds$x, y =diamonds$y, color = diamonds$x)) + 
    geom_point()+ facet_grid(rdmGroup~., scales = "free_y") + 
    labs(x = "X", y="Y") + theme(panel.spacing.y = unit(0.2, "lines")) 
+0

好的謝謝。我沒有意識到方面的限制。我按組的級別分割數據集,並在循環中生成與組數一樣多的圖。 – Jim

相關問題