library(knitr)
opts_chunk$set(comment = "#>", error = FALSE, tidy = FALSE)
opts_chunk$set(fig.width = 2, fig.height = 1.25, dpi = 100)

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

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)

Basic histogram:

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

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)
faithful %>% ggvis(~eruptions) %>% layer_histograms(width=0.5, center=0)

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)

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

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

Box plots

mtc <- mtcars %>% mutate(cyl = factor(cyl))
mtc %>% ggvis(~cyl, ~mpg) %>% layer_boxplots()


rpruim/ggvis2 documentation built on May 28, 2019, 2:34 a.m.