Interactive, web-ready ggplot2-style graphics with ggvis

June 25, 2014
By

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

Hadley Wickham's been working on the next-generation update to ggplot2 for a while, and now it's available on CRAN. The ggvis package is completely new, and combines a chaining syntax reminiscent of dplyr with the grammar of graphics concepts of ggplot2. The resulting charts are web-ready in scalable SVG format, and can easily be made interactive thanks to RStudio's shiny package.

For example, here's the code to create a scatterplot with a smoothing line from the mtcars data set:

mtcars %>% 
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_smooths()

And here's the corresponding SVG image:

SVG graphics are great online, because they're compact (this one's just 25Kb) and look great whatever size they're displayed at (it's a vector format, so you never get pixellation). The only think SVGs don't work well for is charts with millions of elements (points, lines, etc.) because then they can be large and slow to render. (The only other downside is that our blogging platform, TypePad, doesn't support SVG with its image tools, so I had to insert an <image> element into the HTML directly.)

You can easily add interactivity to a chart, by specifying parameters as input controls rather than numbers. Here's the code for the same chart, with a slider to specify the smoothing parameter and point size:

mtcars %>%
  ggvis(~wt, ~mpg) %>%
  layer_smooths(span = input_slider(0.5, 1, value = 1)) %>%
  layer_points(size := input_slider(100, 1000, value = 100))

If you run that code in RStudio you'll get an interactive chart, or go here to see the same interactivity on a web page, rendered with RStudio's Shiny. For more details, check out the ggvis website linked below.

RStudio: ggvis 0.3 overview

To leave a comment for the author, please follow the link and comment on his blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.