Interactive, web-ready ggplot2-style graphics with ggvis

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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() %>%

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 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 their blog: Revolutions. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)