Refs:
package ggvis is used to turn a dataset into a visualisation, setting up default mappings between variables in the dataset and visual properties. It uses a syntax similar in spirit to ggplot2 and dplyr.
The goal is to combine the best of R (e.g. every modelling function you can imagine) and the best of the web (everyone has a web browser). Data manipulation and transformation are done in R, and the graphics are rendered in a web browser, using Vega [ref.(http://cran.r-project.org/web/packages/ggvis/vignettes/overview.html)
library(ggvis)
library(dplyr)
Several examples:
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()
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points(size := 25, shape := "diamond", stroke := "red", fill := NA)
mtcars %>%
ggvis(~mpg, ~disp, size = ~vs) %>%
layer_points()
mtcars %>%
ggvis(~mpg, ~disp, size = ~vs, opacity := 0.4) %>%
layer_points()
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_axis("x", title = "Weight (lb/1000)", title_offset = 50, orient = "top") %>%
add_axis("y", title = "Miles/(US) gallon", subdivide = 2, values = seq(10,34,by=2))
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_axis("x", title = "") %>%
add_axis("x", title = "Weight (lb/1000)", offset = 60, grid = FALSE) %>%
hide_axis("y")
# For axes, you can set the properties of the ticks (or majorTicks and minorTicks separately), the labels and axis.
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_axis("x", properties = axis_props(
axis = list(stroke = "red", strokeWidth = 3),
grid = list(stroke = "blue"),
ticks = list(stroke = "blue", strokeWidth = 2),
labels = list(angle = 45, align = "left", fontSize = 10)
))
mtcars %>% ggvis(y = ~mpg) %>%
layer_points(prop("x", ~disp, scale = "xdisp")) %>%
layer_points(prop("x", ~wt, scale = "xwt"), fill := "blue") %>%
add_axis("x", "xdisp", orient = "bottom") %>%
add_axis("x", "xwt", orient = "top",
properties = axis_props(labels = list(fill = "blue")))
Unlike ggplot2, by default, ggvis will not combine scales based on the same underlying variables into a single legend. Instead you must do this yourself by supplying the name of multiple scales to one legend:
mtcars %>%
ggvis(~wt, ~mpg, size = ~cyl) %>%
layer_points() %>%
add_legend(c("size"))
mtcars %>%
ggvis(~wt, ~mpg, size = ~cyl, fill=~cyl) %>%
layer_points() %>%
add_legend(c("size","fill"))
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_smooths(span=0.5) # add a smooth loess curve using span% of the neighbors
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_model_predictions(model = "lm", se = TRUE, stroke := "red") # add a regression line with 95% CI
## Guessing formula = mpg ~ wt
# layer_smooths() is just compute_smooth() + layer_paths()
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
compute_smooth(mpg ~ wt) %>%
layer_paths(~pred_, ~resp_, strokeWidth := 1, stroke := "red")
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points(size := 5) %>%
layer_smooths(span = 1, stroke := "orange") %>%
layer_smooths(span = 0.3, stroke := "red")
Behind the scenes, interactive plots are built with shiny, and you can currently only have one running at a time in a given R session. To finish with a plot, press the stop button in Rstudio, or close the browser window and then press Escape or Ctrl + C in R.
mtcars %>%
ggvis(~wt, ~mpg,
size := input_slider(10, 100, label = "Point size"),
opacity := input_slider(0, 1, label = "Opacity"),
stroke := input_select(c("GOLD" = "gold", "BLACK" = "black"), label="Select color"),
strokeWidth := input_numeric(label = "Stroke size", value = 5)
) %>%
layer_points()
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
layer_smooths(se = TRUE, span = input_slider(0.2, 1), stroke := "blue", strokeWidth := 5)
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>% ggvis(~wt, ~mpg) %>% layer_points() %>%
layer_model_predictions(model = "lm",
domain = input_slider(0, 10, value = c(1, 4))) # domain determines the interval of the regression
## Guessing formula = mpg ~ wt
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt*100)
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points(fill = ~factor(cyl))
mtcars %>%
ggvis(~wt, ~mpg, fill = ~factor(cyl)) %>%
layer_points() %>%
group_by(cyl) %>%
layer_smooths(span = input_slider(0.66, 1, value=1))
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
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
pressure %>%
ggvis(~temperature, ~pressure) %>%
layer_bars(width = 12)
pressure %>%
ggvis(~temperature, ~pressure) %>%
layer_lines()
pressure %>%
ggvis(~temperature, ~pressure) %>%
layer_points(shape := "square", stroke := "darkgreen", fill := "gold") %>%
layer_lines(stroke := "blue", strokeWidth := 1)
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
faithful %>%
ggvis(~eruptions) %>%
layer_histograms()
## Guessing width = 0.1 # range / 35
faithful %>%
ggvis(~eruptions) %>%
layer_histograms(width=0.5, center=0)
mtcars %>%
ggvis(~wt) %>%
layer_histograms(width = input_slider(0, 2, step = 0.10, label = "width"),
center = input_slider(0, 2, step = 0.05, label = "center"))
## Warning: Can't output dynamic/interactive ggvis plots in a knitr document.
## Generating a static (non-dynamic, non-interactive) version of the plot.
cocaine %>% ggvis(~month, fill := "#fff8dc") %>%
layer_histograms(width = 1, center = 0) %>%
add_axis("x", title = "month") %>%
add_axis("y", title = "count")
# with grouping
mtcars %>%
ggvis(~mpg, fill = ~factor(cyl)) %>%
group_by(cyl) %>%
layer_histograms(width = 2, stack=TRUE)
## Warning: Unequal factor levels: coercing to character
mtcars %>%
ggvis(~factor(cyl), ~mpg) %>%
layer_boxplots()
## Warning: Unequal factor levels: coercing to character
mtcars %>%
arrange(wt) %>%
ggvis(~wt, ~mpg) %>%
layer_paths() # arrange+layer_paths is the same as layer_lines
t <- seq(0, 2 * pi, length = 100)
data.frame(x = sin(t), y = cos(t)) %>%
ggvis(~x, ~y) %>%
layer_paths(fill := "red") # with fill makes a polygon
mtcars %>%
arrange(wt) %>%
ggvis(~wt, ~mpg) %>%
layer_ribbons()
data.frame(x = 1:10, y = (1:10) ^ 2) %>%
ggvis(~x, ~y, y2 := 0) %>%
layer_ribbons()
set.seed(1014)
data.frame(x1 = runif(5), x2 = runif(5), y1 = runif(5), y2 = runif(5)) %>%
ggvis(~x1, ~y1, x2 = ~x2, y2 = ~y2, fillOpacity := 0.1) %>%
layer_rects()
data.frame(x = 3:1, y = c(1, 3, 2), label = c("a", "b", "c"), type=c("x","x","y")) %>%
ggvis(~x, ~y, text := ~label) %>%
layer_text(fontSize := 15, fontWeight := "bold", angle := 45) %>%
layer_text(text := ~type, dx := 10, dy := -10, stroke := "blue")