闪亮的 rMaps:图例和标签不会在闪亮的应用程序中显示在美国等高线中
rMaps in Shiny: Legend and labels are not getting displayed in US choropleth in shiny app
图例和标签(州缩写)未显示在 Shiny 应用程序的美国等值区中。但是,当我 运行 global.R 中的代码在 RStudio 控制台中运行时,标签和图例都可以正常显示。请帮忙!
代码如下:
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") )
))
事实证明,rCharts 中的 'DataMaps' 库版本不显示图例和标签,但 rMaps 会显示图例和标签。当这两个包都加载到 Shiny 中时,它默认使用 'DataMaps' 的 rCharts 版本。我不得不更改 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") )
))
图例和标签(州缩写)未显示在 Shiny 应用程序的美国等值区中。但是,当我 运行 global.R 中的代码在 RStudio 控制台中运行时,标签和图例都可以正常显示。请帮忙!
代码如下:
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") )
))
事实证明,rCharts 中的 'DataMaps' 库版本不显示图例和标签,但 rMaps 会显示图例和标签。当这两个包都加载到 Shiny 中时,它默认使用 'DataMaps' 的 rCharts 版本。我不得不更改 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") )
))