The following examples show you how to create a selection of common graphics with ggvis. First, load ggvis and dplyr:
library(ggvis)
library(dplyr)
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)
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)
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")
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()
pressure %>% ggvis(~temperature, ~pressure) %>% layer_lines()
Lines with points:
pressure %>% ggvis(~temperature, ~pressure) %>%
layer_points() %>%
layer_lines()
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()
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")
mtcars %>% ggvis(~factor(cyl), ~mpg) %>% layer_boxplots()