代码之家  ›  专栏  ›  技术社区  ›  Jim

R: 在Shining app中,facet\u wrap无法使用ggplotly正确渲染

  •  5
  • Jim  · 技术社区  · 7 年前

    当我在ggplotly()中为一个有很多刻面组的闪亮应用程序制作facet\u网格时,绘图会变得一团糟。然而,它在外面正常工作。

    我怎样才能解决这个问题?
    我怀疑它与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(),因为我在我的真实应用程序中需要它。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Eumenedies    7 年前

    首先要注意的是,这个问题与 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

    panel.spacing.y 但这取决于需要多少行:

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