反应性 ggplot2 图表 - 自定义每个图表

reactive ggplot2 graph - customize each graph

我正在创建一个带有 select 图表下拉菜单的闪亮应用程序。我从我的工作目录中提取数据,创建一些变量,然后在 ggplot2 中 select 这些变量来显示我想要的图表。但是,我想为这些 graphs.I 努力寻找一种能够在每个图形上以不同方式显示信息的方法添加一些单独的自定义 - 例如,每个图形具有不同的 x 和 y 名称。这是我目前拥有的:

selection <- reactive({if(input$var=="Average Price by Country") {
  return(mpc)
} else if (input$var =="Average Price by Vintage") {
  return(mpv)
} else if (input$var == "Standard Deviation by Country") {
  return(sdc)
} 
})

output$plot <- renderPlot({

  function() {
    if(selection ==mpc) {
    selection() %>%
    ggplot(aes(V1, V2, fill = V3)) +
    geom_bar(stat = "identity", position="dodge")    
    }else {
    selection() %>%
    ggplot(aes(V1, V2, fill = V3)) +
    geom_bar(stat = "identity", position="dodge")    
    }
  }
})

按国家/地区的平均价格、按年份的平均价格和按国家/地区的标准差都是 UI 下拉列表中的选项。理想情况下,我希望能够以不同方式自定义每个 ggvis 图。目前的设置方式,它只是使用相同的 ggvis 函数显示不同的数据。我试图将 selection 包装在 outputplot 中,但是当我 运行 应用程序时,这甚至没有显示图形。

有没有办法编写一个函数来做到这一点?谢谢你。

我看不到您的其余代码,因此无法对其进行测试,但这就是我要做的:

output$plot <- renderPlot({
  if(input$var == "Average Price by Country") {
    selection <- mpc
    plot_type = "mpc"
  } else if (input$var =="Average Price by Vintage") {
    selection <- mpv
    plot_type = "mpv"
  } else if (input$var == "Standard Deviation by Country") {
    selection <- sdc
    plot_type = "sdc"
  } 
  if(plot_type == "mpv") { # CHANGE ME
      selection %>%
        ggplot(aes(V1, V2, fill = V3)) +
        geom_bar(stat = "identity", position="dodge")    
    }else {
      selection %>%
        ggplot(aes(V1, V2, fill = V3)) +
        geom_bar(stat = "identity", position="dodge")    
    }
}

主要假设是 mpcmpvsdc 在工作区中。 (即 load("yourworkspace.RData")shinyServer() 之前)