2017-09-26 124 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

相关问题