【问题标题】:R: facet_wrap does not render correctly with ggplotly in Shiny appR:facet_wrap 在 Shiny 应用程序中无法使用 ggplotly 正确渲染
【发布时间】:2018-03-07 12:18:50
【问题描述】:

当我在 ggplotly() 中为 Shiny 应用程序执行 facet_grid 时,具有大量分面组,情节混乱。但是它在 Shiny 之外可以正常工作。

我该如何解决这个问题?
我怀疑它与 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") 


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(),因为我在实际应用中需要它。

【问题讨论】:

  • 在运行您的示例时收到很多警告。您是否尝试过安装 ggplot2 的最新 github 版本? "我们建议您使用带有ggplotly() 的开发版ggplot2 安装它:devtools::install_github('hadley/ggplot2')"
  • 我更新了 ggplot2 并且警告消失了。谢谢你的建议。但它并不能解决 pb。

标签: r shiny facet-grid ggplotly


【解决方案1】:

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

提供以下输出:

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

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

【讨论】:

  • 好的,谢谢。我不知道方面数量的限制。我按组级别拆分数据集,并在循环中生成与组数一样多的图。
猜你喜欢
  • 2016-05-28
  • 2019-03-12
  • 1970-01-01
  • 2018-07-08
  • 2019-07-25
  • 1970-01-01
  • 2019-07-21
  • 2021-04-14
  • 2020-04-06
相关资源
最近更新 更多