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

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)

Regression lines

Adding a smooth line

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

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

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

Scatter plots with grouping

Coloring points by a variable:

mtcars %>% 
  ggvis(~wt, ~mpg) %>% 
  layer_points(fill = ~factor(cyl))

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

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

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

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

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

Line graphs

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

Lines with points:

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

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

Modify the fill color and binwidth:

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