只有 1 行的数据框不能显示 3D 条形图,而多行的数据框可以显示 3D 条形图

The dataframe with only 1 row can't display 3D bar whereas dataframe with more than one rows can display 3D bars

提问人:akshay bholee 提问时间:1/5/2021 最后编辑:Ronak Shahakshay bholee 更新时间:1/5/2021 访问量:37

问:

我正在尝试使用 highcharter 制作带有 z 轴标签和 x 轴标签的 3d 条形图。 但是,当我的数据帧只有一行时,图表不会显示条形图。如果数据帧由多行组成,则可以正确显示。

我希望能够在图表中显示数据帧中的 1 行。

下面是一个可重现的示例:

library(shiny)
library(highcharter)

shinyApp(
  
  ui <- fluidPage(
    fluidRow(
      highchartOutput('chart')
    )
  ),
  
  server <- function(input, output, session) {
    
    df <- data.frame(
      Z1 = c(1, 2, 1, 4),
      Z2 = c(2, 3, 2, 7),
      Z3 = c(6, 3, 7, 4),
      Z4 = c(3, 4, 1, 5)
    )
    
    df <- df[-(1:3),] #reduced dataframe to 1 row
    
    
    dta <- lapply(seq(ncol(df)), function(x) {
      list(
        name = colnames(df)[x], 
        data = df[, x]
      ) 
    })
    
    output$chart <- renderHighchart({
      
      highchart() %>%
        hc_chart(
          type = "column", 
          options3d = list(
            enabled = TRUE,
            beta = 20,
            alpha = 30,
            depth = 400,
            viewDistance = 10
          )
        ) %>%
        hc_xAxis(categories = row.names(df[1:nrow(df)-1,])) %>%
        hc_zAxis(
          min = 0,
          max = 3,
          categories = colnames(df)
        ) %>%
        hc_plotOptions(
          series = list(
            depth = 100,
            grouping = FALSE,
            groupZpadding = 10
          )
        ) %>%
        hc_add_series_list(dta)
    })
  })
r

评论


答: 暂无答案