如何使用Shiny + gvisGeoChart设置恒定色标?

时间:2015-12-17 09:46:05

标签: r google-visualization shiny ggvis googlevis

我能够使用下面的脚本创建一个交互式geoChart,问题是每天都要区分地图颜色的比例。我的数据集是美国每个州的每日统计数据。

例如,对于第1天,比例取特定日期的最小值和最大值。但我正在尝试更改脚本,以便在任何给定的日期(并显示全年的最小值和最大值)的比例变得不变

任何人都可以请教如何做到这一点?谢谢!

global.R

library(shiny)
states <- read.csv("queries_geo.csv")
states$StartDate <- as.Date(states$StartDate, "%m/%d/%Y")

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("PlayStation4 Search Volume Trend by States"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("StartDate", "Quarter",
              min = min(states$StartDate), 
              max = max(states$StartDate),
              value = min(states$StartDate),
              step = 1,
              animate = TRUE)),

mainPanel(
  htmlOutput ("GeoStates")
))))

server.R

library(shiny)
library(dplyr)
library(googleVis)

shinyServer(function(input,output,session){

  querydate <- reactive({
    states_new <- states %>%
      filter(StartDate == input$StartDate) %>%
      select(Geo,Queries) %>%
      arrange(Geo)})

  output$GeoStates <- renderGvis ({
    GeoStates <- gvisGeoChart(querydate(), 
                              "Geo", #locationvar
                              "Queries", # colorvar
                              options = list(region = "US",
                                            displayMode = "regions",
                                            resolution = "provinces",
                                            sizeAxis.maxValue = max(states$Queries),
                                            sizeAxis.minValue = min(states$Queries),
                                            width = 600, 
                                            height = 400)
                              )})})

1 个答案:

答案 0 :(得分:0)

我能够弄清楚这一点。我发布了我的解决方案,希望将来可以节省别人的时间!感谢您阅读我的问题!

global.R =&gt; 此部分没有变化

library(shiny)
states <- read.csv ("queries_geo.csv")
states$StartDate <- as.Date(states$StartDate, "%m/%d/%Y")

ui.R =&gt; 这一部分发生了重大变化。此代码不是在ui.R中的server.R和htmlOutput中创建地图,而是在ui.R中创建地图。这样,地图比例保持不变,动画也能顺利运行。 server.R只是将反应数据提供给ui.R。

中的地图
library(shiny)
library(googleVis)
library(googleCharts)

min_query <- min(states$Queries)
max_query <- max(states$Queries)

shinyUI(fluidPage(
  # This line loads the Google Charts JS library
  googleChartsInit(),

  # https://developers.google.com/chart/interactive/docs/gallery/geochart
  googleGeoChart("GeoStates",
                 width = 1000,
                 height = 600,
                 options = list(
                   fontSize = 13,
                   region = "US",
                   displayMode = "regions",
                   resolution = "provinces",
                   colorAxis = list(
                     maxValue = round(max_query, -3),
                     minValue = round(min_query, -1)
                   ))),

  fluidRow(
    shiny::column(4, offset = 4,
    sliderInput("StartDate",
                "StartDate",
                min = min(states$StartDate), 
                max = max(states$StartDate),
                value = min(states$StartDate),
                step = 60,
                animate = TRUE)
  ))))

<强> server.R

library(shiny)
library(dplyr)
library(googleVis)

shinyServer(function(input,output,session){

  querydate <- reactive({
    states_new <- states %>%
      filter(StartDate == input$StartDate) %>%
      select(Geo,Queries) %>%
      arrange(Geo)
    })

  output$GeoStates <- reactive({
    list(
      data = googleDataTable(querydate())
    )})})
相关问题