The ggvis package is used to make interactive data visualizations. The fact that it combines shiny’s reactive programming model and dplyr’s grammar of data transformation make it a useful tool for data scientists.
This package may allows us to implement features like interactivity, but on the other hand every interactive ggvis plot must be connected to a running R session.
PACKAGE INSTALLATION & DATA FRAME
The first thing you have to do is install and load the ggvis package with:
Moreover we need a data set to work with. Tha dataset we chose in our case is “Cars93” which contains data from 93 Cars on Sale in the USA in 1993 and we can find it in the MASS package which of course must be installed and called too. To install and call those packages and attach the “Cars93” dataset use:
You can use
head(Cars93) in order to see the variables of your dataset.
Furthermore you need to install and call the shiny and the magrittr package with:
NOTE: Because of the fact that alla ggvis graphics are web graphics,if you’re not using RStudio (which provides a built-in browser), you’ll notice that this plot opens in your web browser.
The ggvis() function
The first thing we have to do is call
ggvis(). The first argument is the data set that we want to plot, and the second describes which variables we will use. Look at the example below.
plot1 <- ggvis(Cars93, x = ~Length, y = ~Wheelbase)
This doesn’t plot anything because you haven’t set how to display your data. The example below creates a scatterplot:
There is an alternative and maybe more practical way to produce the same result with the
%>% function of the magrittr package like the example below:
ggvis(x = ~Length, y = ~Wheelbase) %>%
~ before the variable name to indicate that we don’t want to literally use the value of the variable, but instead we want we want to use the variable inside in the dataset. We will use it from now on. This is how we can drop
You can add more variables to the plot by mapping them to other visual properties like
shape. Look at the examples below.
Cars93 %>% ggvis(~Length, ~Wheelbase, stroke = ~EngineSize) %>% layer_points() #stroke
Cars93 %>% ggvis(~Length, ~Wheelbase, fill = ~EngineSize) %>% layer_points() #fill
Cars93 %>% ggvis(~Length, ~Wheelbase, size = ~EngineSize) %>% layer_points() #size
Cars93 %>% ggvis(~Length, ~Wheelbase, shape = ~factor(Passengers)) %>% layer_points()#shape
If you want to make the points a fixed colour, size or shape, you need to use
:= instead of
=. Look at the examples below.
Cars93 %>% ggvis(~Length, ~Wheelbase, fill := "yellow", stroke := "black") %>% layer_points() #fill
Cars93 %>% ggvis(~Length, ~Wheelbase, size := 350, opacity := 0.5) %>% layer_points() #size
Cars93 %>% ggvis(~Length, ~Wheelbase, shape := "cross") %>% layer_points() #shape
You can map visual properties to variables or set them to specific values, but it is far more interesting to connect them to interactive controls. Look at the two sliders’ example below
size := input_slider(1, 100),
opacity := input_slider(0, 1)
You can also add interactivity to other plot parameters like the width and centers of histogram bins like the example below:
layer_histograms(width = input_slider(0, 2, step = 0.10, label = "width"),
center = input_slider(0, 2, step = 0.05, label = "center"))
input_slider(), ggvis also provides
You can also use keyboard controls with
up_down(). The following example shows how to control the size of the points by pressing the left and right keyboard controls.
arrow % ggvis(~Length, ~Wheelbase, size := arrow, opacity := 0.5) %>% layer_points()
With tooltips you can add more complex interactivity:
Cars93 %>% ggvis(~Length, ~Wheelbase) %>%