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()
<!–html_preserve–>
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)
<!–html_preserve–>
Adding a smooth line
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_smooths()
<!–html_preserve–>
With a linear model, and 95% confidence interval for the model:
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_model_predictions(model = "lm", se = TRUE)
<!–html_preserve–>
Coloring points by a variable:
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points(fill = ~factor(cyl))
<!–html_preserve–>
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")
<!–html_preserve–>
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()
<!–html_preserve–>
It's possible to specify the width of the bars:
pressure %>%
ggvis(~temperature, ~pressure) %>%
layer_bars(width = 10)
<!–html_preserve–>
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()
<!–html_preserve–>
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 limitation in ggvis.
It's possible to work around this by using a data set with a numerical x, and then tell it to display using a nominal scale. Notice in the example below, the x-axis labels are centered under each bar, each bar has a label, and they are sorted numerically.
# Use the original pressure data set with continuous x values
pressure %>% ggvis(~temperature, ~pressure) %>%
layer_bars()
<!–html_preserve–>
pressure %>% ggvis(~temperature, ~pressure) %>% layer_lines()
<!–html_preserve–>
Lines with points:
pressure %>% ggvis(~temperature, ~pressure) %>%
layer_points() %>%
layer_lines()
<!–html_preserve–>
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()
<!–html_preserve–>
Modify the fill color and binwidth, and add titles for the axes, since the automatic titles aren't very informative:
faithful %>% ggvis(~eruptions, fill := "#fff8dc") %>%
layer_histograms(binwidth = 0.25) %>%
add_axis("x", title = "eruptions") %>%
add_axis("y", title = "count")
<!–html_preserve–>