Section 1: Introduction to ggvis

Set-Up

data(mtcars)

install.packages('ggvis', repos = 'https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/ggvis_0.4.3.tgz')
## Warning: unable to access index for repository https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/ggvis_0.4.3.tgz/src/contrib:
##   cannot download all files
## Warning: package 'ggvis' is not available (for R version 3.3.1)
## Warning: unable to access index for repository https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/ggvis_0.4.3.tgz/bin/macosx/mavericks/contrib/3.3:
##   cannot download all files
library(ggvis)

Let’s Begin

mtcars %>% ggvis(~disp, ~mpg) %>% layer_points()
mtcars %>% ggvis(~wt, ~mpg, fill := "red") %>% layer_points()
mtcars %>% ggvis(~wt, ~mpg) %>% layer_smooths()
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% layer_smooths()

Section 2: The Grammer of Graphics

Graphics Grammer

ggvis follows the grammar of graphics. You can combine a set of data, properties and marks with the following format.

%>% ggvis(,, fill = ~, …) %>% layer_()

Don’t forget the tilde (~)!

#Histogram
pressure %>% ggvis(~temperature, ~pressure) %>% layer_bars()
#Line
pressure %>% ggvis(~temperature, ~pressure) %>% layer_lines()
#Fill based on temperature
pressure %>% ggvis(~temperature, ~pressure, fill = ~temperature) %>% layer_points()
#Size based on size
pressure %>% ggvis(~temperature, ~pressure, size = ~pressure) %>% layer_points()

4 Essential Components of a Graph

Every ggvis graph contains 4 essential components: data, a coordinate system, marks and corresponding properties. By changing the values of each of these components you can create a vast array of unique plots.


Section 3: Three New Types of Snytax

Three Operators: %>%, :=, and =

The pipe operator passes the result from its left-hand side into the first argument of the function on its right-hand side. f(x) %>% g(y) is a shortcut for g(f(x), y).

= maps a property to a data value or a set of data values. This is how you visualize variation in your data set. ggvis will scale the values appropriately and add a legend that explains how values are mapped to particular instances of the property.

:= sets a property to a specific color (or size, width, etc.). This is how you customize the appearance of your plots. Numbers will be typically interpreted as pixels, such as size or displacement from the top-left hand corner of the plot. Color specifications are passed to vega, a javascript library, so you can use any color name recognized by HTML/CSS.

layer_points(ggvis(faithful, ~waiting, ~eruptions)) 
# is the same as 
faithful %>% ggvis(~waiting, ~eruptions) %>% layer_points()
# Size is based on the variable pressure
pressure %>% ggvis(~temperature, ~pressure, size = ~pressure) %>% layer_points()
# Size is based on the value 100
pressure %>% ggvis(~temperature, ~pressure, size := 100) %>% layer_points()
# Fill is based on the value red
pressure %>% ggvis(~temperature, ~pressure, fill := "red") %>% layer_points()

Referring to Different Objects

You can refer to three different types of objects in your ggvis code: objects, variables, and raw values.

  • If you type a string of letters, ggvis will treat the string as an object name. It will look for an object with that name in your current environment.

  • If you place a tilde, ~, at the start of the string, ggvis will treat the string as a variable name. It will look for the column with that name in the data set that the graph visualizes.

  • If you surround a string of letters with quotation marks, ggvis will treat the string as a raw value, e.g., a piece of text.


Properties for Points

You can manipulate many different properties when using layer_points(), including x, y, fill, fillOpacity, opacity, shape, size, stroke, strokeOpacity, andstrokeWidth.

The shape property recognizes several different values: circle (default), square, cross, diamond, triangle-up, and triangle-down.

faithful %>% ggvis(~waiting, ~eruptions) %>% layer_points(opacity := 0.5, fill := "blue", stroke := "black", size = ~eruptions)
faithful %>% ggvis(~waiting, ~eruptions) %>% layer_points(fillOpacity = ~eruptions, size := 100, fill := "red", stroke := "red", shape := "cross")

Section 4: The Line, a Special Type of Mark

Properties for Lines

In the previous section, you learned that you can manipulate many different properties when using the points mark. This mark type responds to, among others, x, y, fill, fillOpacity, opacity, shape, size, stroke, strokeOpacity, andstrokeWidth.

Similar to points, lines have specific properties; they respond to: x, y, fill, fillOpacity, opacity, stroke, strokeDash, strokeOpacity, and strokeWidth. Most of them are common to the properties for points, some are missing - there’s no size property - and others are new, like strokeDash).

pressure %>% ggvis(~temperature, ~pressure) %>% layer_lines(stroke := "red", strokeWidth := 2, strokeDash := 6)

Path Marks and polygons

layer_lines() will always connect the points in your plot from the leftmost point to the rightmost point. This can be undesirable if you are trying to plot a specific shape.

A dataframe texas is available in your workspace, containing the coordinates of the state of Texas; it is arranged such that consequent observations should be connected. The code on the right would plot a map of Texas if ggvis connected the points in the correct order.

You can do this with layer_paths(): this mark connects the points in the order that they appear in the data set. So the paths mark will connect the point that corresponds to the first row of the data to the point that corresponds to the second row of data, and so on — no matter where those points appear in the graph.

Ex. texas %>% ggvis(~long, ~lat) %>% layer_paths(fill := “darkorange”)


Display Model Fits

compute_model_prediction() is a useful function to use with line graphs. It takes a data frame as input and returns a new data frame as output. The new data frame will contain the x and y values of a line fitted to the data in the original data frame.

The code below computes a line that shows the relationship between the eruptions and waiting variables of the faithful data set.

faithful %>% compute_model_prediction(eruptions ~ waiting, model = “lm”)

compute_model_prediction() takes a couple of arguments:

  • faithful, the dataset,

  • an R formula, eruptions ~ waiting, that specifies the relationship to model.

  • a model argument, the name of the R modelling function that is used to calculate the line. “lm” calculates a linear fit, “loess” uses the LOESS method.

compute_smooth() is a wrapper around compute_model_prediction() that calculates a LOESS smooth line by default.

# Compute the x and y coordinates for a loess smooth line that predicts mpg with the wt
mtcars %>% compute_smooth(mpg ~ wt) 
##       pred_    resp_
## 1  1.513000 32.08897
## 2  1.562506 31.68786
## 3  1.612013 31.28163
## 4  1.661519 30.87037
## 5  1.711025 30.45419
## 6  1.760532 30.03318
## 7  1.810038 29.60745
## 8  1.859544 29.17711
## 9  1.909051 28.74224
## 10 1.958557 28.30017
## 11 2.008063 27.83462
## 12 2.057570 27.34766
## 13 2.107076 26.84498
## 14 2.156582 26.33229
## 15 2.206089 25.81529
## 16 2.255595 25.29968
## 17 2.305101 24.79115
## 18 2.354608 24.29542
## 19 2.404114 23.81818
## 20 2.453620 23.36514
## 21 2.503127 22.95525
## 22 2.552633 22.61385
## 23 2.602139 22.32759
## 24 2.651646 22.08176
## 25 2.701152 21.86167
## 26 2.750658 21.65260
## 27 2.800165 21.43987
## 28 2.849671 21.20875
## 29 2.899177 20.95334
## 30 2.948684 20.71584
## 31 2.998190 20.49571
## 32 3.047696 20.28293
## 33 3.097203 20.06753
## 34 3.146709 19.83950
## 35 3.196215 19.58885
## 36 3.245722 19.29716
## 37 3.295228 18.94441
## 38 3.344734 18.56700
## 39 3.394241 18.20570
## 40 3.443747 17.90090
## 41 3.493253 17.62060
## 42 3.542759 17.34002
## 43 3.592266 17.07908
## 44 3.641772 16.81759
## 45 3.691278 16.55757
## 46 3.740785 16.30833
## 47 3.790291 16.07916
## 48 3.839797 15.87937
## 49 3.889304 15.70181
## 50 3.938810 15.52594
## 51 3.988316 15.35173
## 52 4.037823 15.17933
## 53 4.087329 15.00894
## 54 4.136835 14.84072
## 55 4.186342 14.67484
## 56 4.235848 14.51148
## 57 4.285354 14.35082
## 58 4.334861 14.19302
## 59 4.384367 14.03826
## 60 4.433873 13.88672
## 61 4.483380 13.73856
## 62 4.532886 13.59396
## 63 4.582392 13.45310
## 64 4.631899 13.31614
## 65 4.681405 13.18326
## 66 4.730911 13.05464
## 67 4.780418 12.93045
## 68 4.829924 12.81086
## 69 4.879430 12.69604
## 70 4.928937 12.58617
## 71 4.978443 12.48143
## 72 5.027949 12.38198
## 73 5.077456 12.28799
## 74 5.126962 12.19966
## 75 5.176468 12.11713
## 76 5.225975 12.04060
## 77 5.275481 11.97023
## 78 5.324987 11.90620
## 79 5.374494 11.84868
## 80 5.424000 11.79784

compute_smooths() to Simplify Model Fits

compute_smooth() always returns a data set with two columns, one named pred_ and one named resp_. You can easily pass this data to a ggvis() call to plot a smoothed line of your data, as this example shows:

faithful %>%
  compute_smooth(eruptions ~ waiting) %>%
  ggvis(~pred_, ~resp_) %>%
  layer_lines()

Because first calling compute_smooth() and then layer_lines() can be a bit of a hassle, ggvis features the layer_smooths() function: this layer automatically calls compute_smooth() in the background and plots the results as a smoothed line.

mtcars %>% compute_smooth(mpg ~ wt) %>% ggvis(~pred_, ~resp_) %>% layer_lines()
#layer_smooths() avoids the need to explicitly set up a ggvis graphic with pred_ and resp_
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% layer_smooths()

Section 5: Compute Functions

Histograms

faithful %>% ggvis(~waiting) %>% layer_histograms(width = 5)

Instead of plotting data that appears directly in your dataset, layer_histograms() plots the counts of each bin on the y axis. Behind the scenes, compute_bin() calculates these counts. This function requires two arguments: a data set (which you will provide with %>%), and a variable name to bin on. You can aditionally pass a width argument, to specify the binwidth. It returns a data frame that provides everything you need to build the histogram’s rectangles with layer_rects().

Just as combining compute_smooth() and layer_lines() does the same as layer_smooths(), combining compute_bin() and layer_rects() does the same as layer_histograms().Can you spot the analogy?

faithful %>%
  compute_bin(~waiting, width = 5) %>%
  ggvis(x = ~xmin_, x2 = ~xmax_, y = 0, y2 = ~count_) %>%
  layer_rects()

This is the same plot that you’ve coded up in the previous exercise. Remember that there is no need to explicitly code the combination of compute_bin() and layer_rects(). Unless you want to do special things, use layer_histograms().


Density Plots

Density plots provide another way to display the distribution of a single variable. A line displays the density of a variable at each point in its range. You can think of a density plot as a continuous version of a histogram with a different y scale (although this is not exactly accurate).

You can build a density plot by combining compute_density() with layer_lines(). compute_density() takes two arguments, a data set and a variable name. It returns a data frame with two columns: pred_, the x values of the variable’s density line, and resp_, the y values of the variable’s density line.

Just like layer_histograms() combines compute_bin() and layer_rects(), you can use layer_densities() to create density plots easily: it calls compute_density() and layer_lines() in the background.

faithful %>% ggvis(~waiting) %>% layer_densities(fill := "green")

Shortcuts

You do not need to use a compute function to transform the variables in your data set. You can directly plot transformations of the variables.

  • Instead of compute_smooth() and layer_lines(), you can use layer_smooths()
  • Instead of compute_bin() and layer_rects(), you can use layer_histograms()
  • Instead of compute_density() and layer_lines(), you can use layer_densities()

Similar to the above, you can use layer_bars() to build a bar plot. In the background, it will use compute_count() and layer_rects() to do this. Have a look at an example of this in the editor, that plots a version of cyl that has been transformed into a factor (R’s version of a categorical variable).

# mtcars %>%
#   compute_count(~factor(cyl)) %>%
#   ggvis(x = ~x_, y = 0, y2 = ~count_, width = band()) %>%
#   layer_rects()

# is the same as 

mtcars  %>%
  ggvis(~factor(cyl)) %>%
  layer_bars()

Section 6: ggvis and dplyr

install.packages("dplyr", repos = 'https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/dplyr_0.5.0.tgz')
## Warning: unable to access index for repository https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/dplyr_0.5.0.tgz/src/contrib:
##   cannot download all files
## Warning: package 'dplyr' is not available (for R version 3.3.1)
## Warning: unable to access index for repository https://cran.rstudio.com/bin/macosx/mavericks/contrib/3.3/dplyr_0.5.0.tgz/bin/macosx/mavericks/contrib/3.3:
##   cannot download all files
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

ggvis and group_by

group_by() uses a grouping variable to organize a data set into several groups of observations. It places each observation into a group with other observations that have the same value of the grouping variable. In other words group_by() will create a separate group for each unique value of the grouping variable. When ggvis encounters grouped data, it will draw a separate mark for each group of observations.

mtcars %>% 
  group_by(cyl) %>% 
  ggvis(~mpg, ~wt, stroke = ~factor(cyl)) %>% 
  layer_smooths()
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
mtcars %>% 
  group_by(cyl) %>% 
  ggvis(~mpg, fill = ~factor(cyl)) %>% 
  layer_densities()
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character

group_by() vs. interaction()

group_by() can group data based on the interaction of two or more variables. Simply pass multiple variable names:

my_data %>% group_by(, )

group_by() will create a separate group for each distinct combination of values within the grouping variables. However, to also correctly map ggvis properties to unique combinations of multiple variables, you need an additional step: the interaction() function. The following example:

stroke = ~interaction(, )

will map stroke to the unique combinations of and .

mtcars %>% 
  group_by(cyl, am) %>% 
  ggvis(~mpg, fill = ~interaction(cyl, am)) %>% 
  layer_densities()
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character

Make sure you do not mix up group_by() and interaction(). The former is used to group observations, while the latter allows you to specify properties.


Chaining is a virture

You might have noticed that by now, the size of our of chain of operations is becoming considerable. Using the pipe becomes even more useful! Let’s retake the solution to the previous exercise:

mtcars %>% group_by(cyl, am) %>% ggvis(~mpg, fill = ~interaction(cyl, am)) %>% layer_densities()

This call is exactly equivalent to the following piece of code that is very hard to read:

layer_densities(ggvis(group_by(mtcars, cyl, am), ~mpg, fill = ~interaction(cyl, am)))

The pipe efficiently solves the so-called Dagwood sandwich problem that drives your functions and arguments further and further apart when building richer plots.

However, you should always remember that the pipe is just a way to restructure your command rather than an actual operation.

mtcars %>%
  group_by(am) %>%
  ggvis(~mpg, ~hp) %>%
  layer_smooths(stroke = ~factor(am)) %>%
  layer_points(fill = ~factor(am))
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character

## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
# The layer_smooths() and layer_points() calls can be safely interchanged.

It seems that cars with automatic transmission (am = 0) tend to have a lower ‘mileage per gallon’ variable then cars with a manual gearbox (am = 1)


Section 7: Interactive Plots

The Basics of Interactive Plots

The first lines of code on the right make a basic interactive plot. It includes a select box that to change the shape of the points in the plot. The DataCamp interface only supports static plots, where the interactions are removed. The dynamic versions of this code - as they would appear in the RStudio IDE - can be found on DataCamp’s Shiny Server to see and experiment with the interactive plot by changing the point shape. The command sets shape to a select box created with input_select(): the visualization updates on the fly if you update your selection.

You can make your plots interactive by setting a property to the output of an input widget. ggvis comes with seven input widgets:

  • input_checkbox(),
  • input_checkboxgroup(),
  • input_numeric(),
  • input_radiobuttons(),
  • input_select(),
  • input_slider(), and
  • input_text().

By default, each returns their current value as a number or character string.

(Needs a way to interact with the graph such as a shiny app)

faithful %>%
  ggvis(~waiting, ~eruptions, fillOpacity := 0.5,
        shape := input_select(label = "Choose shape:",
                              choices = c("circle", "square", "cross", "diamond", "triangle-up", "triangle-down")),
        fill := input_select(label = "Choose color:", 
                             choices = c("black", "red", "blue", "green"))) %>%
  layer_points()
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>%
  ggvis(~mpg, ~wt,
        fill := input_radiobuttons(label = "Choose color:", 
                                   choices = c("black", "red", "blue", "green"))) %>%
  layer_points()
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

(Remember to hit ‘Escape’ to stop the interactive plot before moving on!)


Input Widgets in More Detail

Some input widgets provide choices for the user to select from. Others allow the user to provide their own input. For example, input_text() provides a text field for users to type input into. Instead of assigning input_text() a choices argument, you assign it a value argument: a character string to display when the plot first loads.

For more details on the interactive controls, you can always consult the ggvis interactivity vignette.

mtcars %>%
  ggvis(~mpg, ~wt,
        fill := input_text(label = "Choose color:", value = "black")) %>%
  layer_points()
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

(Remember to hit ‘Escape’ to stop the interactive plot before moving on!)

By default, input widgets return their values as character strings and numbers. To have a widget return its value as a variable name, you need to add the extra argument map = as.name.

For example, the text widget in the command on the right will pass the character string “black” to the fill argument, which is useful for setting. If we add map = as.name to the arguments of input_text(), the widget would return ~black which is useful for mapping (or would be if black were a real variable in mtcars):

mtcars %>%
  ggvis(~mpg, ~wt) %>%
  layer_points(fill = input_select(label = "Choose fill variable:", choices = names(mtcars), map = as.name))
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

(Remember to hit ‘Escape’ to stop the interactive plot before moving on!)


Control Parameters and Values

The previous exercises all manipulated properties of the ggvis plots, such as the shape and fill of points in scatterplots. As you will recall from earlier exercises, ggvis often needs additional parameters to build the correct graphs. You can also use widgets to control these parameters. Typically, you want to use the input_numeric() and input_slider() widgets to set numerical parameters.

mtcars %>% 
  ggvis(~mpg) %>%
  layer_histograms(width = input_numeric(label = "Choose a binwidth:", value = 1))
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>%
  ggvis(~mpg) %>%
  layer_histograms(width = input_slider(label = "Choose a binwidth:", min = 1, max = 20))
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.

(Remember to hit ‘Escape’ to stop the interactive plot before moving on!)


Section 8: Multi-layered Plots

Multi-layered Plots and Their Properties

You can create multi-layered plots by adding additional layers to a graph with the %>% syntax.

If you set or map a property inside ggvis() it will be applied globally , every layer in the graph will use the property. If you set or map a property inside a layer_() function it will be applied locally: only the layer created by the function will use the property. Where applicable, local properties will override global properties.

pressure %>%
  ggvis(~temperature, ~pressure, stroke := "skyblue") %>%
  layer_lines() %>% 
  layer_points()
pressure %>%
  ggvis(~temperature, ~pressure) %>%
  layer_lines(stroke := 'skyblue') %>% 
  layer_points()

Global properties can cause trouble when you use multiple layers. For example, the first ggvis command on the right causes an error since it also applies the shape property to layer_lines(), which does not use the shape property.

pressure %>%
  ggvis(~temperature, ~pressure) %>%
  layer_lines(stroke := "skyblue") %>%
  layer_points(shape := "triangle-up")
pressure %>%
  ggvis(~temperature, ~pressure, stroke := "skyblue",
              strokeOpacity := 0.5, strokeWidth := 5) %>%
  layer_lines() %>%
  layer_points(fill = ~temperature,
              shape := "triangle-up",
              size := 300)

There is No Limit on the Number of Layers

layer_model_predictions() plots the prediction line of a model fitted to the data. It is similar to layer_smooths() but you can extend it to more models than just the “loess” or “gam” model.

layer_model_predictions() takes a parameter named model; it should be set to a character string that contains the name of an R model function. layer_model_predictions() will use this function to generate the model predictions. So for example, you could draw the model line of a linear model with:

layer_model_predictions(model = “lm”) Notice that model is a parameter, not a property. This means that you do not need to worry about setting vs. mapping. You can always set parameters with the equal sign, =.

pressure %>%
  ggvis(~temperature, ~pressure) %>%
  layer_lines(opacity := 0.5) %>% 
  layer_points() %>%
  layer_model_predictions(model = 'lm', stroke := 'navy') %>%
  layer_smooths(stroke := 'skyblue')
## Guessing formula = pressure ~ temperature

Axes

Axes help you to customize the plots you create with ggvis. add_axis() allows you to change the titles, tick schemes and positions of your axes. The example code below clarifies:

add_axis(“x”, title = “axis title”, values = c(1, 2, 3), subdivide = 5, orient = “top”)

The first argument specifies which axis to customize.

TITLE - the title of the axis you specified in the first argument. VALUES - determine where labelled tick marks will appear on each axis. SUBDIVIDE - insert unlabelled tick marks between the labelled tick marks on an axis. ORIENT - control where the axis appears. For the x axis, you can use “top” or “bottom”, for the y axis, you can use “left” or “right”.

faithful %>% 
  ggvis(~waiting, ~eruptions) %>% 
  layer_points() %>%
  add_axis("x", 
           title = "Time since previous eruption (m)", 
           values = c(50, 60, 70, 80, 90), 
           subdivide = 9, 
           orient = "top") %>%
  add_axis("y", 
           title = "Duration of eruption (m)", 
           values = c(2, 3, 4, 5), 
           subdivide = 9, 
           orient = "right")

Legends

add_legend() works similarly to add_axis(), except that it alters the legend of a plot. The following example adds a legend for the fill property, with a meaningful title:

pressure %>% ggvis(~temperature, ~pressure, fill = ~pressure) %>% layer_points() %>% add_legend(“fill”, title = “~ pressure”)

faithful %>% 
  ggvis(~waiting, ~eruptions, opacity := 0.6, 
        fill = ~factor(round(eruptions))) %>% 
  layer_points() %>% 
  add_legend("fill", 
  title = "~ duration (m)", 
  orient = "left")

If you don’t use add_legend(), ggvis will automatically create a separate legend for each property that you map a variable on, which can lead to confusing results. For example, the code chunk in the editor creates three separate legends - for fill, one for shape, and one for size - and draws them each on top of each other.

To solve this, you can specify a vector of property names as the first argument of the add_legend() call. For example, to combine a stroke legend with an opacity legend, you use:

add_legend(c(“stroke”, “opacity”))

faithful %>% 
  ggvis(~waiting, ~eruptions, opacity := 0.6, 
        fill = ~factor(round(eruptions)), shape = ~factor(round(eruptions)), 
        size = ~round(eruptions))  %>%
  layer_points() %>%
  add_legend(c("fill", "shape", "size"), title = "~ duration (m)")

Section 10: Customize Property Mappings

Scale Types

You can change the color scheme of a ggvis plot by adding a new scale to map a data set variable to fill colors. The first chunk of code on the right creates a new scale that will map the numeric disp variable to the fill property. The scale will create color output that ranges from red to yellow.

ggvis provides several different functions for creating scales: scale_datetime(), scale_logical(), scale_nominal(), scale_numeric(), scale_singular(). Each maps a different type of data input to the visual properties that ggvis uses.

mtcars %>% 
  ggvis(~wt, ~mpg, fill = ~disp, stroke = ~disp, strokeWidth := 2) %>%
  layer_points() %>%
  scale_numeric("fill", range = c("red", "yellow")) %>%
  scale_numeric("stroke", range = c("darkred", "orange"))
mtcars %>% ggvis(~wt, ~mpg, fill = ~hp) %>%
  layer_points() %>%
  scale_numeric("fill", range = c("green", "beige"))
mtcars %>% ggvis(~wt, ~mpg, fill = ~factor(cyl)) %>%
  layer_points() %>%
  scale_nominal("fill", range = c("purple", "blue", "green"))

Adjust Any Visual Property

You can adjust any visual property in your graph with a scale (not just color).

Let’s look at another property that you may frequently want to adjust: opacity. When you map a variable to opacity some data points will end up so transparent that they are hard to see, as in the plot that’s coded on the right.

mtcars %>% ggvis(x = ~wt, y = ~mpg, fill = ~factor(cyl), opacity = ~hp) %>%
  layer_points() %>%
  scale_numeric("opacity", range = c(0.2, 1))

Just as you can change the range of visual values that your scales produce, you can also change the domain of data values that they consider. For example, you can expand the domain of the x and y scales to zoom out on your plot. The second plot on the right will expand the y axis to cover data values from 0 to the largest y value in the data set. NA here stands for not availble; if used when specifying a domain inside scale_numeric(), ggvis interprets this as the largest value of the dataset variable in question.

mtcars %>% ggvis(~wt, ~mpg, fill = ~disp) %>%
  layer_points() %>%
  scale_numeric("y", domain = c(0, NA)) %>%
  scale_numeric("x", domain = c(0, 6))

“=” versus “:=”

Scales help explain the difference between = and :=. Variables tend to contain values in the data space, things such as numbers measured in various units, datetimes, and so on. But properties need visual values, things such as numbers measured in pixels, colors, opacity levels, and so on.

Whenever you use = to map a variable to a property, ggvis will use a scale to transform the variable values into visual values. Whenever you set a value (or variable) to a property with :=, ggvis will pass the value on as is, without transforming it. This can work nicely if the value passed through makes sense in the visual space, but it can have unfortunate consequences if the value does not.

When you use :=, ggvis uses the colors named in mtcars$color to fill the points.