2017-04-17 40 views
1

請看下面的非常簡單的例子:plotlyOutput與geom_bar沒有顯示負值

library(shiny) 
library(plotly) 

ui <- basicPage(
    plotlyOutput("plot") 
) 

server <- function(input, output) { 
    df <- data.frame(SomeX = c("Cat1", "Cat2", "Cat3", "Cat1", "Cat2", "Cat3"), 
        SomeFill = c("a", "a", "a", "b", "b", "b"), 
        SomeY = c(2, 3, 4, -4, 7, 3)) 

    output$plot <- renderPlotly({ 
    ggplot(df, aes(x = SomeX, y = SomeY, fill=SomeFill)) + 
     geom_bar(stat = "identity") 
    }) 
} 

shinyApp(ui, server) 

出於某種原因,它並不顯示值-4正常輸出。它顯示的值就好像是+4。這是從代碼的結果不同(使用情節而不是plotly):

library(shiny) 
library(plotly) 

ui <- basicPage(
    plotOutput("plot") 
) 

server <- function(input, output) { 
    df <- data.frame(SomeX = c("Cat1", "Cat2", "Cat3", "Cat1", "Cat2", "Cat3"), 
        SomeFill = c("a", "a", "a", "b", "b", "b"), 
        SomeY = c(2, 3, 4, -4, 7, 3)) 

    output$plot <- renderPlot({ 
    ggplot(df, aes(x = SomeX, y = SomeY, fill=SomeFill)) + 
     geom_bar(stat = "identity") 
    }) 
} 

shinyApp(ui, server) 

這是一個錯誤還是我做錯了什麼?

+0

這是一個錯誤,請參閱https://github.com/ropensci/plotly/issues/560 –

回答

0

Plotly在堆疊酒吧中有負值的問題。如果您通過plot_ly直接定義繪圖並使用barmode = 'relative',它應該可以工作。

enter image description here

library(shiny) 
library(plotly) 

ui <- fluidPage(
    plotlyOutput("plot"), 
    verbatimTextOutput("event") 
) 

server <- function(input, output) { 

    df <- data.frame(SomeX = c("Cat1", "Cat2", "Cat3", "Cat1", "Cat2", "Cat3"), 
        SomeFill = c("a", "a", "a", "b", "b", "b"), 
        SomeY = c(2, 3, 4, -4, 7, 3)) 

    output$plot <- renderPlotly({ 
    plot_ly(df, x=~SomeX, y=~SomeY, color=~SomeFill, type='bar') %>% layout(barmode = 'relative') 
    }) 


} 

shinyApp(ui, server) 
+0

感謝。解決了它。我希望這會有一種策略(僅僅因爲我在其他地方使用它),但如果情況並非如此,那麼plot_ly肯定會完成工作。 – ChrR