A Short Introduction to ggvis in R | Towards Data Science

The Grammar of Graphics, the product of Leland Wilkinson’s multi-decade long work and research in the graphics domain, provides the philosophical basis for R graphing libraries such as ggplot2, ggvis and Julia graphing libraries such as Gadfly. Although, there are other libraries which can provide you really amazing graphics but what distinguishes these packages is that they are based on the research done by Wilkinson which essentially tries to look at graphics as a language and breaks it down into separate components just like any other language breaks down its components into subject, verb, object etc.
Wilkinson wrote a statistical package known for its comprehensive graphics in the early 1970s which he later sold to SPSS in mid-1990s. While working at SPSS, he wrote his groundbreaking research that was mentioned a while back. His research has had a major impact on the philosophy of graphics in computer sciences. The most famous plotting package in R ggplot2, maintained by Winston Chang and Hadley Wickham, derives its underlying philosophy from The Grammar of Graphics. Another such package is ggvis. We’ll talk about the latter.
ggplot2 in itself is an amazing package. ggvis takes nothing away from that, rather it is just an extension of the former with the ability to build HTML graphs. Because ggvis graphs are HTML, they can be used in a shiny web application, R markdown reports and with JavaScript too. We’ll figure out what some of it means in a while.
The documentation for ggvis can be found here along with a really good tutorial on datacamp. For additional information, you can visit RStudio’s blog on ggvis along with a crisp summary of The Grammar of Graphics. The prime motive of this post is to explore and understand the grammar and syntax of ggvis.
$ religiongdp <- read.table("/Users/kovid.rathee/Downloads/ReligionGDP.csv", header=TRUE, sep=",")$ religiongdp %>% ggvis (~GDP, ~Religiosity) %>% layer_points()
The first thing that we’d learn about is the pipe operator %>% taken from the magrittr package. This works just like a shell pipe operator works.
religiongdp %>% compute_smooth(GDP ~ Religiosity) %>% ggvis (~pred_, ~resp_) %>% layer_lines(stroke := "darkblue", strokeWidth := 3, strokeDash := 5)
religiongdp %>% compute_smooth(GDP ~ Religiosity) %>% ggvis (~pred_, ~resp_) %>% layer_points(fill := "darkblue", size = 0.5)
Plotting the original data with a line that kind of fits. We can consider it to be a regression line. The features in the dataset are not enough to do a proper classification so as to guess what a country’s or a state’s GDP should be. Apart from religiosity, there are a lot more important things like population, the form of government so on and so forth. But even from this graph, we can loosely establish that there’s a negative correlation between GDP and Religiosity — although, there are two main exceptions of that, Kuwait and the United States of America. We can call them the outliers.
Please notice the settings button on the upper right corner of the rendition. When you click on that button, it provides you with an option to download a svg file or a png file.
ggvis also distinguishes itself from ggplot2 for having interactive features like input sliders. Interactivity is one of the major achievements of ggvis. It works in the same way how it would work in a Tableau or Qlikview environment, however, when writing your own code using these libraries, you’ll have much more control and a much clearer understanding of how these graphs are rendered. Here’s an example:
religiongdp %>% ggvis (~GDP, ~Religiosity) %>% layer_points(fill := "darkblue", size := input_slider (5,15,value=1)) %>% layer_smooths(stroke:="orange", span = input_slider (5,15,value=1))
iris %>% ggvis(~Sepal.Length,~Sepal.Width,fill=~Species) %>% layer_smooths(span = 2, stroke := “darkred”, strokeDash := 6) %>% layer_smooths(span = 1, stroke := “darkblue”) %>% layer_smooths(span = 0.5, stroke := “orange”)
iris %>% ggvis(~Sepal.Length,~Sepal.Width,fill=~Species) %>% layer_points() %>% add_axis ("x",title="Sepal Length",properties=axis_props(axis=list(stroke="darkblue"),grid=list(stroke="brown",strokeDash=3),ticks=list(stroke="orange",strokeWidth=2),labels=list(angle=45, align="left", fontSize=12)))
iris %>% ggvis (~Sepal.Length,~Sepal.Width) %>% mutate(Sepal.Length = Sepal.Length * 1.5) %>% layer_points(fill=~Species,size=~Petal.Width) %>% add_axis(“x”,title=”Sepal Length”,orient=”top”) %>% add_axis(“y”,title=”Sepal Width”,orient=”right”) %>% add_legend(c(“size”,”fill”),title=”Petal Width”))
iris %>% ggvis(x=~Sepal.Length,y=~Petal.Length,fill=~Species) %>% layer_points(opacity=~Sepal.Width) %>% layer_model_predictions(model="lm",stroke:="green",opacity=3)
iris %>% ggvis(x=~Sepal.Length,y=~Petal.Length,fill=~Species) %>% layer_points(opacity=~Sepal.Width) %>% layer_model_predictions(model="loess",stroke:="green",opacity=3)
ggvis is a great package which provides an upgrade to ggplot2 with a lot of new age features like — interactive plots and the ability to create HTML plots enabling embedding of these plots in Shiny applications and more. Wilkinson’s work has greatly affected the discourse in the language of plotting — which is done on a daily basis while doing data analysis, business intelligence, machine learning and more.