The following examples show you how to create a selection of common graphics with ggvis. First, load ggvis and dplyr:

library(ggvis)
library(dplyr)

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
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points()

1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpg

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

mtcars %>% 
  ggvis(~wt, ~mpg) %>% 
  layer_points(size := 25, shape := "diamond", stroke := "red", fill := NA)

1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpg

Regression lines

Adding a smooth line

mtcars %>% 
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_smooths()

1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpg

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

mtcars %>% 
  ggvis(~wt, ~mpg) %>%
  layer_points() %>%
  layer_model_predictions(model = "lm", se = TRUE)

1.52.02.53.03.54.04.55.05.5wt5101520253035mpg

Scatter plots with grouping

Coloring points by a variable:

mtcars %>% 
  ggvis(~wt, ~mpg) %>% 
  layer_points(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 group_by():

mtcars %>% 
  ggvis(~wt, ~mpg, fill = ~factor(cyl)) %>% 
  layer_points() %>% 
  group_by(cyl) %>% 
  layer_model_predictions(model = "lm")

1.52.02.53.03.54.04.55.05.5wt10121416182022242628303234mpgfactor(cyl)468

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):

pressure %>% 
  ggvis(~temperature, ~pressure) %>%
  layer_bars()

050100150200250300350temperature0100200300400500600700800pressure

It’s possible to specify the width of the bars:

pressure %>% 
  ggvis(~temperature, ~pressure) %>%
  layer_bars(width = 10)

050100150200250300350temperature0100200300400500600700800pressure

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 %>% mutate(temperature = factor(temperature))

pressure2 %>% ggvis(~temperature, ~pressure) %>%
  layer_bars()

020406080100120140160180200220240260280300320340360temperature0100200300400500600700800pressure

Line graphs

pressure %>% ggvis(~temperature, ~pressure) %>% layer_lines()

050100150200250300350temperature0100200300400500600700800pressure

Lines with points:

pressure %>% ggvis(~temperature, ~pressure) %>%
  layer_points() %>% 
  layer_lines()

050100150200250300350temperature0100200300400500600700800pressure

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:

faithful %>% ggvis(~eruptions) %>% layer_histograms()

1.52.02.53.03.54.04.55.0eruptions024681012141618count

The bin selection can be controled by specifying width and at most one of center or boundary of one of the bins. boundary and center may be outside the range of the data.

faithful %>% ggvis(~eruptions) %>% layer_histograms(width=0.5, boundary=0)

1.52.02.53.03.54.04.55.05.5eruptions010203040506070count

faithful %>% ggvis(~eruptions) %>% layer_histograms(width=0.5, center=0)

1.52.02.53.03.54.04.55.0eruptions010203040506070count

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

faithful %>% ggvis(~eruptions, fill := "#fff8dc") %>%
  layer_histograms(width = 0.25)

1.52.02.53.03.54.04.55.0eruptions0510152025303540count

By default, when the number of integer values is small, bins will be centered at integers and have a width of 1:

cocaine %>% ggvis(~month, fill := "#fff8dc") %>%
  layer_histograms() %>%
  add_axis("x", title = "month") %>%
  add_axis("y", title = "count")

123456789101112month050100150200250300350count

This can be forced with

cocaine %>% ggvis(~month, fill := "#fff8dc") %>%
  layer_histograms(width = 1, center = 0) %>%
  add_axis("x", title = "month") %>%
  add_axis("y", title = "count")

012345678910111213month050100150200250300350count
## Box plots

mtcars %>% ggvis(~factor(cyl), ~mpg) %>% layer_boxplots()

468factor(cyl)10121416182022242628303234mpg