考虑以下非常简单的例子:
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。这与代码的结果不同(使用绘图而不是绘图):
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 :(得分:0)
Plotly在堆积条中有一些负值的问题。如果您直接通过plot_ly
定义绘图并使用barmode = 'relative'
,则应该可以使用。
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)