繁体   English   中英

R:facet_wrap无法在“闪亮”应用中使用ggplotly正确渲染

[英]R: facet_wrap does not render correctly with ggplotly in Shiny app

当我在ggplotly()中为具有大量构面组的Shiny App做facet_grid时,情节变得混乱。 但是,它在Shiny外部正常工作。

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


这是一个基于plotly的钻石示例的可复制示例。

比较有光泽和非有光泽的输出: 外部和内部的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") 


闪亮应用中的相同代码:

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()。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM