5

当我在 ggplotly() 中为 Shiny App 做一个 facet_grid 时,有大量的 faceting 组,情节就混乱了。但是它在 Shiny 之外可以正常工作。

我怎样才能解决这个问题?
我怀疑它与 Y 比例有关,但我找不到解决方案。


这是一个基于plotly 中的 diamonds示例的可重现示例。

Shiny 与非 Shiny 输出的比较:Shiny 内外 facet_grid 的比较

代码

外面闪亮:

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


Shiny App 中的相同代码:

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() 因为我在我的真实应用程序中需要它。

4

1 回答 1

2

首先要注意的是,问题与您的使用无关,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 个方面。如果您从中删除任何单个组,rdmGroupplotly输出工作正常,例如

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)

提供以下输出: 输出

一种解决方法可能是简单地拥有多个图,将数据集分成 25 个组。

编辑:我做了更多研究,当面板边距太大而无法显示所有绘图时,绘图停止显示。您可以通过减少显示所有 26 行,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"))
于 2017-09-26T10:45:59.783 回答