3

Legend and Labels (state abbreviations) are not getting displayed in US choropleth in shiny app. However, when I run the code in global.R functions in RStudio console, both labels and legend are displayed fine. Please help!

Here is the code:

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
}

Server:

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")  )
))
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • your example was not reproducible for me. it does even recognize showOutput(). I assume it's related to showOutput("animatedChart","datamaps") – MLavoie Dec 21 '15 at 09:49
  • @MLavoie `showOutput` is a part of `rCharts` so load `library(rCharts)` – Pork Chop Dec 21 '15 at 11:39
  • I did (I know the package), but it still does not work :)..weird – MLavoie Dec 21 '15 at 11:40
  • I guess I missed the line 'library(rCharts)' in my code. – Ms Data Science Dec 21 '15 at 11:40
  • @MsDataScience can you import an image showing what you suppose to see please. Im not 100% what is missing. I can reproduce the charts btw and I see the state names – Pork Chop Dec 21 '15 at 11:43
  • @PorkChop, I do not have the privilege to edit the question yet. Here is a link to the screenshot: [Image](http://imgur.com/gallery/OpTSFMk/new) – Ms Data Science Dec 21 '15 at 12:10
  • Yeah I see that too, show me what you dont see and what is required. You can edit the image with paint or something showing what is missing – Pork Chop Dec 21 '15 at 12:15
  • @PorkChop, here is the image I see in RStudio: [RStudio_Image](http://imgur.com/hWiYrHT). The legend and labels go missing displayed in RStudio when I run the shiny app. The code I use in RStudio is the same as in global.R. – Ms Data Science Dec 21 '15 at 12:25

1 Answers1

3

It turns out the version of 'DataMaps' library in rCharts does not display the legend and labels but the one is rMaps does. When both the packages are loaded in Shiny, it uses the rCharts version of 'DataMaps' by default. I had to change the code in ui.R to use 'rMaps' package instead.

Corrected 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")  )
))