gpt4 book ai didi

Shiny 的 rMaps : Legend and labels are not getting displayed in US choropleth in shiny app

转载 作者:行者123 更新时间:2023-12-01 23:42:31 24 4
gpt4 key购买 nike

图例和标签(州缩写)未在 Shiny 应用程序的美国等值区中显示。但是,当我在 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)
})
})

用户界面:

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”包。

更正的用户界面:

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 的 rMaps : Legend and labels are not getting displayed in US choropleth in shiny app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34386982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com