使用renderPlotly和plot_ly绘制数据的子集时,xAxis的值不正确

问题描述 投票:0回答:1

我对不同国家的不同指标有价值。堆积的boxplot应该可视化我的数据。我的目标是用户选择selectInput应该绘制哪些国家/地区。因此我正在使用子集。仅为所选国家/地区绘制条形图,但x轴上的标签太多。看来,只要所选国家/地区的值为零,就会显示所有标签,直到某个国家/地区的值大于零为止。

为了检查数据是否被正确过滤,我也打印了一张表。

这是我的代码:

    Country <- c("Germany", "France", "UK", "Italy","Spain","US", "Canada","China","India","Japan")
data <- data.frame(country=Country,value=c(4,1,0,0,2,5,2,7,3,1,3,1,0,0,1,5,7,2,2,3),Ind=c(rep("1", times=10),rep("2", times=10)))



UI <- dashboardPage(

dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItemOutput("menu"),

      menuItem(selectInput(inputId = "ChooseCountry", label = "Choose Countries", Country,selected=list("India", "China"), multiple = T))
    )
  ),

  dashboardBody("barchart",
          fluidRow(
            box(plotlyOutput("step1"), width = 12),
          fluidRow(
            box(tableOutput("Table"), width=12)
          )

          ))

)




Server <- function(input, output, session) {



output$step1 <- renderPlotly({
  Data <- subset(data, country %in% input$ChooseCountry)
  plot_ly(data = Data, 
          x = ~country, y = ~value, color = ~Ind, type = "bar") %>%
    layout(barmode="stack")
})


output$Table <- renderTable( {
  Data <- subset(data, country %in% input$ChooseCountry)
})

}

谢谢你的建议! :)

r shiny subset plotly action-button
1个回答
0
投票

您可以删除未使用的级别。试试这个:

Server <- function(input, output, session) {
  output$step1 <- renderPlotly({
    Data <- subset(data, country %in% input$ChooseCountry)
    Data[] <- lapply(Data, function(x) if(is.factor(x)) factor(x) else x)
    plot_ly(data = Data, 
            x = ~country, y = ~value, color = ~Ind, type = "bar") %>%
      layout(barmode="stack")
  })
  output$Table <- renderTable( {
    Data <- subset(data, country %in% input$ChooseCountry)
  })
}

shinyApp(ui = UI,server = Server)
© www.soinside.com 2019 - 2024. All rights reserved.