Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
install.packages("ggvis")
library(ggvis)
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:
install.packages("MASS")
library(MASS)
data("Cars93")
attach(Cars93)
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:
install.packages("shiny")
library(shiny)
install.packages("magrittr")
library(magrittr)
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:
layer_points(plot1)
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:
Cars93 %>%
ggvis(x = ~Length, y = ~Wheelbase) %>%
layer_points()
We use ~
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 x
and y
.
You can add more variables to the plot by mapping them to other visual properties like fill
, stroke
, size
and 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
- Work extensively with the ggvis package and its functionality
- Learn what visualizations exist for your specific use case
- And much more
Interaction
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
Cars93 %>%
ggvis(~Length, ~Wheelbase,
size := input_slider(1, 100),
opacity := input_slider(0, 1)
) %>%
layer_points()
You can also add interactivity to other plot parameters like the width and centers of histogram bins like the example below:
Cars93%>%
ggvis(~Length) %>%
layer_histograms(width = input_slider(0, 2, step = 0.10, label = "width"),
center = input_slider(0, 2, step = 0.05, label = "center"))
Except of input_slider()
, ggvis also provides input_checkbox()
, input_checkboxgroup()
, input_numeric()
, input_radiobuttons()
, input_select()
and input_text()
.
You can also use keyboard controls with left_right()
and 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) %>%
layer_points() %>%
add_tooltip(function(df) df$Length)
Related exercise sets:
R-bloggers.com 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.