rMaps在Shiny中:在Shiny应用程序中,US区域地图的图例和标签未显示。

3

在shiny应用程序中,US区域地图(choropleth)没有显示图例或标签(州缩写)。但是,当我在RStudio控制台中运行global.R函数时,标签和图例都能正常显示。请帮忙解决!

以下是代码:

global.R

#install.packages("Quandl")
library(Quandl)
library(lubridate)
library(rMaps)
library(plyr)
library(shiny)
library(reshape2)
library(rCharts)

getData <- function()
{
  birth.rate <- Quandl("CDC/42512_40827_00")
  birth.rate
}

transformData <- function()
{
  birth.rate <- Quandl("CDC/42512_40827_00")
  birth.rate <- melt(birth.rate, variable.name = 'State', value.name = 'Birth.Rate', id = 'Year')
  b <- transform(birth.rate, State = state.abb[match(State, state.name)], 
                 Year = year(birth.rate$Year),
                 fillKey = cut(Birth.Rate, quantile(Birth.Rate, seq(0, 1, 1/4)), 
                               include.lowest=TRUE, labels = LETTERS[1:4]))
  b[is.na(b)] <- "DC"
  b
}

createMap <- function(data)
{
  fillColors <- setNames(
    RColorBrewer::brewer.pal(4, 'Greens'),
    c(LETTERS[1:4])
  )

  d <- Datamaps$new()
  fml = lattice::latticeParseFormula(Birth.Rate~State, data = data)

  d$set(
    scope = 'usa', 
    data = dlply(data, fml$right.name),
    fills = as.list(fillColors),
    legend = TRUE,
    labels = TRUE)

  d
}

服务器:

source('global.R')
b <- transformData()
shinyServer(function(input, output) {

  output$animatedChart = renderChart({
    animatedChart=createMap(b[b$Year==input$Year,]
    )
    animatedChart$addParams(dom = 'animatedChart') 
    return(animatedChart)
  })
})

UI:

library(shiny)
shinyUI(bootstrapPage(
  div(class="row",
      div(class="span4",
          sliderInput("Year","", min=1990, max=2009, value=1990,step=1))),

  mainPanel(
    showOutput("animatedChart","datamaps")  )
))

你的示例对我来说无法重现。它甚至无法识别showOutput()函数。我猜测这可能与showOutput("animatedChart","datamaps")有关。 - MLavoie
@MLavoie showOutputrCharts 的一部分,因此需要加载 library(rCharts) - Pork Chop
我试过了(我知道这个包),但它仍然不起作用 :)..很奇怪。 - MLavoie
我想我在我的代码中错过了'library(rCharts)'这一行。 - Ms Data Science
@MsDataScience,您能导入一张图像展示您认为应该看到的内容吗?我不确定缺少什么。顺便说一下,我可以重现这些图表,并且我能看到州的名称。 - Pork Chop
显示剩余3条评论
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
3

事实证明,rCharts库中的'DataMaps'版本不显示图例和标签,但是rMaps库中的版本可以。当在Shiny中加载这两个包时,默认使用rCharts版本的'DataMaps'。我必须更改ui.R中的代码,以使用'rMaps'包代替。

更正后的UI:

library(shiny)
shinyUI(bootstrapPage(
  div(class="row",
      div(class="span4",
          sliderInput("Year","", min=1990, max=2009, value=1990,step=1))),

  mainPanel(
    showOutput("animatedChart","datamaps", package="rMaps")  )
))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,