首页 > 解决方案 > 加载 ggplot2 后,Highcharter 绘图失败

问题描述

在我闪亮的应用程序中,我想创建一个线图,其中的group值基于选定的用户输入:input$tt_groupvar. Highcharter 可以使用 来做到这一点hcaes_string,但是我在现实生活中的应用程序加载了 ggplot2,并且hchart失败了。

在下面的 app1 中,ggplot2 未显式加载,并且该图正确地将数据分组基于input$tt_groupvar. 但在下面的 app2 中,唯一的区别是 ggplot2 已加载,应用程序失败。通过 app2 加载 ggplot2 后,app1 不再正常工作。

我知道我可以使用 ggplot2 或 plotly 使这项工作正常工作,但我真的很想使用 highcharter。有没有办法解决这个问题?

任何见解都非常感谢!

应用程序1

library(shiny)
library(dplyr)
library(highcharter)

df <- data.frame(floorDate = as.Date(c("01/01/2017", "01/01/2017", "01/01/2017", "01/01/2018", "01/01/2018", "01/01/2018", "01/01/2019", "01/01/2019", "01/01/2019", "06/01/2017", "11/01/2018", "12/01/2019"), format = "%m/%d/%Y"),
                   location = c("MWH", "MWH", "MWH", "MWH", "SH", "MWH","MWH","MWH", "SH", "SH", "MIF", "MIF"),
                   ethnicity = c("W", "W", "B", "B", "MULT", "MULT","MULT","W","W","B","MULT","B"))

ui <- fluidPage(
  titlePanel("my title"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select a grouping variable to plot the number of ROCs 
               by the values in that group across January 2017 to January 2020."),

      selectizeInput("tt_groupvar",
                     label = "Choose a grouping variable:",
                     choices = c("Location"="location", "Ethnicity"="ethnicity"),
                     multiple = FALSE,
                     selected = c("location"))
      ),

    mainPanel(
      highchartOutput("timeTrend_1")
      )
  )
  )

server <- function(input, output) {

  output$timeTrend_1 <- renderHighchart({
    hchart(df %>% 
             group_by_at(c("floorDate", input$tt_groupvar)) %>%
             count(),
           type = "line",
           hcaes_string(x = "floorDate", y = "n", group = input$tt_groupvar)) %>% #THE ISSUE IS HERE!!!!
      hc_xAxis(title = list(text = "")) %>%
      hc_yAxis(title = list(text = "# of ROCs")) %>%
      hc_title(text = "Number of ROCs per Month by Group from 2017 to January 2019")
  })
}

shinyApp(ui, server)

应用程序2



library(shiny)
library(dplyr)
library(highcharter)
library(ggplot2)


df <- data.frame(floorDate = as.Date(c("01/01/2017", "01/01/2017", "01/01/2017", "01/01/2018", "01/01/2018", "01/01/2018", "01/01/2019", "01/01/2019", "01/01/2019", "06/01/2017", "11/01/2018", "12/01/2019"), format = "%m/%d/%Y"),
                   location = c("MWH", "MWH", "MWH", "MWH", "SH", "MWH","MWH","MWH", "SH", "SH", "MIF", "MIF"),
                   ethnicity = c("W", "W", "B", "B", "MULT", "MULT","MULT","W","W","B","MULT","B"))

ui <- fluidPage(
  titlePanel("my title"),

  sidebarLayout(
    sidebarPanel(
      helpText("Select a grouping variable to plot the number of ROCs 
               by the values in that group across January 2017 to January 2020."),

      selectizeInput("tt_groupvar",
                     label = "Choose a grouping variable:",
                     choices = c("Location"="location", "Ethnicity"="ethnicity"),
                     multiple = FALSE,
                     selected = c("location"))
      ),

    mainPanel(
      highchartOutput("timeTrend_1")
      )
  )
  )

server <- function(input, output) {

  output$timeTrend_1 <- renderHighchart({
    hchart(df %>% 
             group_by_at(c("floorDate", input$tt_groupvar)) %>%
             count(),
           type = "line",
           hcaes_string(x = "floorDate", y = "n", group = input$tt_groupvar)) %>% # THE ISSUE IS HERE!
      hc_xAxis(title = list(text = "")) %>%
      hc_yAxis(title = list(text = "# of ROCs")) %>%
      hc_title(text = "Number of ROCs per Month by Group from 2017 to January 2019")
  })
}

shinyApp(ui, server)


标签: rggplot2shinyr-highcharter

解决方案


推荐阅读