qvis and ggvis

There are two primary functions in ggvis that are used to create plots: qvis and ggvis. qvis provides a streamlined interface which is suitable for simple plots; when you need more control over plots, ggvis may be more appropriate.

In some of the simpler examples below, we’ll show equivalent code qvis and ggvis.

Basic plots

First, load the ggvis package:

library(ggvis)

Scatterplots

We’ll use the built-in mtcars data set, and look at two columns of interest, mpg, and wt:

# The first few rows of mtcars
head(mtcars)
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
#> Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

A basic scatter plot:

# qvis(mtcars, ~wt, ~mpg)
ggvis(mtcars, props(x = ~wt, y = ~mpg)) + mark_point()
1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpg

Smaller points, a different shape, a different outline (stroke) color, and empty fill:

# qvis(mtcars, ~wt, ~mpg, size := 25, shape := "diamond", stroke := "red", fill := NA)
ggvis(mtcars, props(x = ~wt, y = ~mpg)) +
  mark_point(props(size := 25, shape := "diamond", stroke := "red", fill := NA))
1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpg

Bar graphs

We’ll use the built-in pressure data set for these examples:

# The first few rows
head(pressure)
#>   temperature pressure
#> 1           0   0.0002
#> 2          20   0.0012
#> 3          40   0.0060
#> 4          60   0.0300
#> 5          80   0.0900
#> 6         100   0.2700

When the variable on the x axis is continuous (e.g., numeric or date-time):

# Note that y2 is the bottom of the bar, and the width is specified in pixels
ggvis(pressure, props(x = ~temperature, y = ~pressure, y2 = 0)) +
  mark_rect(props(width := 10))
050100150200250300350400temperature0100200300400500600700800900pressure

In the previous example, the width of each bar was specified in pixels. If you change the width of the plot, you’ll see that the bars remain 10 pixels wide.

Instead of setting the width in pixels, it’s possible to set the width in the scaled data space, by specifying the x and x2 values for each bar. If you do this, the width of the bars isn’t determined by the In this example, each bar is 20 wide in the x scale, so the bars touch:

ggvis(pressure,
      props(x = ~temperature - 10, x2 = ~temperature + 10, y = ~pressure, y2 = 0)) +
  mark_rect() +
  guide_axis("x", title = "temperature")
-50050100150200250300350400temperature0100200300400500600700800900pressure

When the variable on the x axis is categorical (e.g., factor or character):

# First, modify the pressure data set so that the x variable is a factor
pressure2 <- pressure
pressure2$temperature <- factor(pressure2$temperature)

# qvis(pressure2, ~temperature, ~pressure, y2 = 0, width = band(), layers = "rect") +
#   dscale("x", "nominal", padding = 0, points = FALSE)
ggvis(pressure2, props(x = ~temperature, y = ~pressure)) +
  mark_rect(props(y2 = 0, width = band())) +
  dscale("x", "nominal", padding = 0, points = FALSE)
010012014016018020200220240260280300320340360406080temperature0100200300400500600700800900pressure

(Notice that in this example, the x values aren’t sorted quite right: they’re sorted lexically, by the first, second, and then third digit, instead of by the numeric value. This is due to a bug in ggvis.)

Line graphs

# qvis(pressure, ~temperature, ~pressure, layers = "line")
ggvis(pressure, props(x = ~temperature, y = ~pressure)) + layer_line()
050100150200250300350400temperature0100200300400500600700800900pressure

Lines with points:

# qvis(pressure, ~temperature, ~pressure, layers = c("line", "point"))
ggvis(pressure, props(x = ~temperature, y = ~pressure)) +
  layer_line() +
  mark_point()
050100150200250300350400temperature0100200300400500600700800900pressure

Histograms

We’ll use the built-in faithful data set for these examples:

# The first few rows
head(faithful)
#>   eruptions waiting
#> 1     3.600      79
#> 2     1.800      54
#> 3     3.333      74
#> 4     2.283      62
#> 5     4.533      85
#> 6     2.883      55

Basic histogram:

# qvis(faithful, ~eruptions)
ggvis(faithful, props(x = ~eruptions)) + layer_histogram()
1.01.52.02.53.03.54.04.55.05.5xmin__024681012141618202224count__

Modify the fill color and binwidth, and add titles for the axes, since the automatic titles aren’t very informative:

ggvis(faithful, props(x = ~eruptions, fill := "#fff8dc")) +
  layer_histogram(binwidth = 0.25) +
  guide_axis("x", title = "eruptions") +
  guide_axis("y", title = "count")
1.01.52.02.53.03.54.04.55.05.5eruptions05101520253035404550count

Regression lines

Adding a smoothing line (defaults to a loess model):

# qvis(mtcars, ~wt, ~mpg, layers = c("point", "smooth"))

ggvis(mtcars, props(x = ~wt, y = ~mpg)) +
  mark_point() +
  layer_smooth()
1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpg

With a linear model, and 95% confidence interval for the model:

# qvis(mtcars, ~wt, ~mpg) + layer_smooth(method = "lm", se = TRUE)
ggvis(mtcars, props(x = ~wt, y = ~mpg)) +
  layer_point() +
  layer_smooth(method = "lm", se = TRUE)
1.52.02.53.03.54.04.55.05.5wt51015202530mpg

Scatter plots with grouping

Coloring points by a variable:

ggvis(mtcars, props(x = ~wt, y = ~mpg)) +
  layer_point(props(fill = ~factor(cyl)))
1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpgfactor(cyl)468

Coloring points, and adding a smoother for each group. The grouping variable (which is applied before the transform_smooth is calculated) must be specified with by_group():

ggvis(mtcars, by_group(cyl), props(x = ~wt, y = ~mpg)) +
  layer_point(props(fill = ~factor(cyl))) +
  layer_smooth(method = "lm", props(stroke = ~factor(cyl)))
1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpgfactor(cyl)468factor(cyl)468

Interactive plots

The following plots won’t display correctly in this online document, but they will display correctly when run from R.

Slider

Scatter plot with a loess smoother, and span controlled by a slider:

ggvis(mtcars, props(x = ~wt, y = ~mpg)) +
  layer_smooth(se = TRUE,
    span = input_slider(min = 0.3, max = 1, value = 0.8, step = 0.1,
      label = "Smoothing span")) +
  layer_point() +
  guide_axis("x", title = "Weight") +
  guide_axis("y", title = "MPG")

Slider and select box

A kernel density estimate where the adjust parameter is controlled by a slider, and kernel is controlled by a select input.

ggvis(mtcars, props(x = ~wt)) +
  layer_density(
    adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
    kernel = input_select(
      c("Gaussian" = "gaussian",
        "Epanechnikov" = "epanechnikov",
        "Rectangular" = "rectangular",
        "Triangular" = "triangular",
        "Biweight" = "biweight",
        "Cosine" = "cosine",
        "Optcosine" = "optcosine"),
      label = "Kernel")
  )