Friday, June 28, 2013

rCharts Remake of NYT

For those wondering if I have forsaken finance, the answer is no.  I just don’t think there is much to do in here besides watch and wait.  So more d3 and R as I try to distract myself from doing something dumb in the markets.

This time I used rCharts and slidify to  recreate another NYT visualization similar to what I did in 512 Paths to the White House.  Now with any cumulative growth time series that we build in R we can apply the template and create this amazing visualization.  Below is a screenshot.  Click here to see the live version.

nyt home price rcharts

I’ll be living the good life over the next week, so no posts until after the 4th of July.


  1. I'd really like to recreate this chart but I don't understand how to use the two lines of code:

    p2$setTemplate(script = "libraries/widgets/nyt_home/layouts/nyt_home.html")

    I get the error:

    Error in envRefInferField(x, what, getClass(class(x)), selfEnv) :
    ‘setLib’ is not a valid field or method name for reference class “rCharts

    I downloaded the rCharts_nyt_home_price repository but I'm not sure where it should sit in my directories. Is there anything simple that I'm missing?

  2. Have you updated your version of rCharts to the newest version? This method was just recently added. I am hoping this is the problem. Let me know.

  3. Thanks, updating rCharts was enough to make it work. I'm trying to make edits so the chart will take my own data but it isn't working too well...

  4. I will be happy to help. I did not spend any time incorporating flexibility in the data input, so I can see where it might cause some trouble. Let me know basic structure of your data, and I will try to add some insight.

  5. Thanks for the offer, I will post some data tonight. To be honest I thought I reshaped my data so it was in the exact format (including the date range) as your data but with only two classes instead of the 10 in your example . The only thing I didn't check was the class of dates.

  6. Also, do you have any example pages like your polycharts gallery ( for other rCharts? I am particularly interested in implementing NVD3 time series charts but I'm having trouble with x-axis display and the nuances of chart display (in essence, I'm waiting on the documentation...). Thanks for the polycharts galler, it's awesome! Tim.

  7. here is a gallery of all the dimple examples

    there are lots of other examples scattered about. I will try to put together a bunch of NVD3 examples.

  8. Thanks, hadn't seen that one before, I like it! Do you use the Quandl R plugin? I think you'd like it here -

    Here's some code that pulls data (just two series) that I would love to put in an interactive chart like this. Sorry for the messy code, the two data.frames are jobs and jobs2. If you don't want to use the Quandl plugin I'll put together some other data.


    #insert own auth code
    Quandl.auth(" ")

    # Taking the dates from col 1 and the number of employed ('000s) from col 4.
    NSW <- Quandl("AUSBS/6202004")[,c(1,4)]
    NSW[,3] <- "NSW"
    colnames(NSW) <- c("date", "employed", "state")

    VIC <- Quandl("AUSBS/6202005")[,c(1,4)]
    VIC[,3] <- "VIC"
    colnames(VIC) <- c("date", "employed", "state")

    jobs <- cbind(NSW[,c(1,2)],VIC[,2])
    colnames(jobs) <- c("date", "NSW", "VIC")

    #melt the dataset
    jobs2 <- rbind(NSW,VIC)

    ggplot(jobs, aes(date)) +
    geom_line(aes(y = NSW, colour = "NSW")) +
    geom_line(aes(y = VIC, colour = "VIC"))

  9. To save you having to reply to lots of "what is function x", two things might help in the scripts.
    1.Perhaps use the :: syntax. So rCharts::envRefInferField # then people can see what package is expected to have the function.
    2. Might be helpful in general to add something like a checkMinVersions() function up top, which might take c(ggplot2=1.3, rCharts=.34).

    In OpenMx I do this a bit, using mxVersion() to make sure users have the latest version.

    And thanks for the nice articles! Enjoying the NYT D3 podcast now too..

  10. Hello Kenton
    I have downloaded the above script as it is and I tried it on my PC (Windows 7, latest R Version). However, it does not work properly. The problem is that I get the message in R "Listening on port 8100" and then at some Point "Connection reset by peer or he get's hung up. I attach a screenshot on how it looks on the IE Explorer. Can you help me to sort this out?
    Kind regards

  11. I have copied your code to make this -