4. Overview
ggvis is a data visualization package for R which lets you:
• Declaratively describe data graphics with a syntax similar
in spirit to ggplot2.
• Create rich interactive graphics that you can play with
locally in Rstudio or in your browser.
• Leverage shiny’s infrastructure to publish interactive
graphics usable from any browser (either within your
company or to the world).
5. • The goal is to combine the best of R
• (e.g. every modeling function you can imagine) and the best
of the web (everyone has a web browser). Data manipulation
and transformation are done in R, and the graphics are
rendered in a web browser, using Vega.
• For RStudio users, ggvis graphics display in a viewer panel,
which is possible because RStudio is a web browser.
• ggvis also incorporates shiny’s reactive programming model
and dplyr’s grammar of data transformation.
6. • The graphics produced by ggvis are fundamentally web
graphics and work very differently from traditional R graphics.
• This allows us to implement exciting new features like
interactivity, but it comes at a cost. For example, every
interactive ggvis plot must be connected to a running R
session (static plots do not need a running R session to be
viewed).
• This is great for exploration, because you can do anything in
your interactive plot you can do in R.
7. This vignette is divided into four main sections:
• Dive into plotting with ggvis().
• Add interactivity with the mouse and keyboard.
• Create more types of graphic by controlling the layer
type.
• Build up rich graphics with multiple layers.
8. ggvis()
• Every ggvis graphic starts with a call to ggvis().
• The first argument is the data set that you want to
plot, and the other arguments describe how to map
variables to visual properties.
• Install the ggvis package by using below command.
9. install.packages(ggvis)
library(ggvis)
p <- ggvis(mtcars, x = ~wt, y = ~mpg)
• This doesn’t actually plot anything because you haven’t
told ggvis how to display your data. You do that by
layering visual elements, for example with layer_points().
10. • Now,if I’ll add a layer in the above script,
the output like:
Script:
p <- ggvis(mtcars, x = ~wt, y = ~mpg)
layer_points(p)
Or
layer_points(ggvis(mtcars, x = ~wt, y = ~mpg))
11.
12. To make life easier ggvis uses the
%>% (pronounced pipe) function from the magrittr package.
That allows you to rewrite the previous function call as:
-> mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points()
13. • The first two arguments to ggvis() are usually the
position, so by convention you can drop x and y .
• Now, we can write the script like this also.
->mtcars %>% ggvis(~mpg, ~disp) %>%
layer_points()
-> cs2m %>% ggvis( ~Chlstrl, ~AnxtyLH) %>%
layer_points()
14. • Using the layer_boxplots()
-> grades %>% ggvis(~ethnicity, ~gpa,fill = "blue") %>%
layer_boxplots()
15. -> cs2m %>% ggvis(x = ~BP , y = ~Age, fill = "blue")
%>% layer_boxplots()
16. Let’s move towards more interesting plots, adding more
variables in one plot.
You can add more variables to the plot by mapping them to
other visual properties like fill, stroke, size and shape in
mtcars dataset.
Similarly, we can add more variables in other datasets like
cs2m and grades.
25. Interaction
• As well as mapping visual properties to variables or
setting them to specific values, you can also connect
them to interactive controls.
• The following example allows you to control the size and
opacity of points with two sliders.
• To finish with a plot, press the stop button in Rstudio, or
close the browser window and then press Escape or Ctrl
+ C in R.
29. We can also connect interactive components to other plot
parameters like the width and centers of histogram bins:
HISTOGRAMS IN ggvis()
->mtcars %>% ggvis(~wt) %>%
layer_histograms(width = input_slider(0, 2, step = 0.10, label =
"width"),
center = input_slider(0, 2, step = 0.05, label = "center"))
33. LAYERS
So far, you seen three layer Functions:
layer_points() ,layer_boxplots() and layer_histograms().
There are many other layers, and they can be roughly
categorised into two types:
Simple, which include primitives like points, lines and rectangles.
Compound, which combine data transformations with one or
more simple layers.
All layer functions use the plural, not the singular. Think the verb,
not the noun: I’m going to layer some points onto my plot.
34. Simple layers
There are five simple layers:
• layer_points()
• layer_paths()
• layer_ribbons()
• layer_rects()
• layer_text()
41. layer_text()
The text layer has many new options to control the
apperance of the text:
• text (the label),
• dx and dy (margin in pixels between text and anchor point),
• angle (rotate the text),
• font (font name),
• fontSize (size in pixels),
• fontWeight (e.g. bold or normal),
• fontStyle (e.g. italic or normal.)
50. Multiple layers
• Rich graphics can be created by combining multiple
layers on the same plot.
• This easier to do: just layer on multiple elements:
53. ggvis interactivity
• Ggvis interactivity is built on top of Shiny’s reactive programming
model.
• It’s possible to use ggvis without understanding how Shiny works,
but you’ll be limited to relatively simple interactions. The first part
of this vignette describes basic interactive controls, which provide a
quick and easy way to add basic interactivity to a plot.
• They are not very flexible, but they cover the most common
interactive needs and you don’t need to know anything about Shiny
to use them.
54. We are going to learn how to do:
Basic interactive controls
Arguments
Multiple outputs
Property mappings
Limitations
55. Basic interactive controls
A plot with basic interactive controls looks very similar to a static plot. You
just replace constant values with functions that produce interactive controls
like input_slider()