Chapter 6 GGvis cheatsheet
Chenhui Mao
6.1 ggvis
Official website: http://ggvis.rstudio.com/
ggvis is a visualization package that can: - plot the graph in the same way as ggplot2 - create interaction strategies where you can interact with the graph locally or in the website
6.1.1 Getting start
First thing first, we shall import some necessary packages
This is a simple demonstration on how to use ggvis
. As can be seen, ggvis can create graph in the same way ggplot does!
Similar way to write basic ggvis
Apply pipeline to write ggvis
We can add a theme on it
mtcars %>%
ggvis(x = ~wt, y = ~mpg) %>%
layer_points() %>%
add_axis("x", title = "Weight", ticks = 40,
properties = axis_props(
ticks = list(stroke = "red"),
majorTicks = list(strokeWidth = 2),
grid = list(stroke = "red"),
labels = list(
fill = "steelblue",
angle = 50,
fontSize = 14,
align = "left",
baseline = "middle",
dx = 3
),
title = list(fontSize = 16),
axis = list(stroke = "#333", strokeWidth = 1.5)
)
)
6.2 aesthetics
Try to adjust the parameters
6.3 Interaction
6.3.1 basic interaction
mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points() %>%
layer_smooths(
span = input_slider(0.2, 1, value = 0.5, step = 0.05, label = "span")
)
Next, we add some interaction strategies into it. We can add the following interaction into the graph: - Change the size of the dot - Change the opacity of the dot
mtcars %>%
ggvis(~wt, ~mpg,
size := input_slider(10, 100),
opacity := input_slider(0, 1)
) %>%
layer_points()
Add interaction strategies to a histograms - Change the width of the histograms - Change the center of the histograms
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"))
Add tooltip
6.3.2 Input option
6.3.2.1 input_select()
mtcars %>% ggvis(x = ~wt) %>%
layer_densities(
adjust = input_slider(.1, 2, value = 1, step = .1, label = "Bandwidth adjustment"),
kernel = input_select(
c("Gaussian" = "gaussian", "Epanechnikov" = "epanechnikov",
"Rectangular" = "rectangular", "Triangular" = "triangular",
"Biweight" = "biweight", "Cosine" = "cosine", "Optcosine" = "optcosine"),
label = "Kernel")
)
6.3.2.2 Checkbox input
mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points(
opacity := input_checkbox(label = "Semi-transparent",
map = function(val) ifelse(val, .3, 1))) %>%
layer_model_predictions(
model = input_checkbox(label = "LOESS (curve) model fit",
map = function(val) ifelse(val, "loess", "lm")))
6.3.2.3 Text input
mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points(fill := input_text(label = "Point color", value = "red")) %>%
layer_model_predictions(model = input_text(label = "Model type", value = "loess"))
6.3.3 Map
6.3.3.1 Example with value map function
mtcars %>% ggvis(x = ~wt, y = ~mpg) %>%
layer_points() %>%
layer_model_predictions(model = "loess",
model_args = list(n = input_select(
choices = c("Two", "Six", "Eighty"),
map = function(value) switch(value, Two = 2, Six = 6, Eighty = 80),
label = "Number of points"
))
)
new_vals <- input_select(c("Set A" = "A", "Set B" = "B"),
label = "Dynamically-generated column",
map = function(value) {
vals <- switch(value,
"A" = rep(c("One", "Two")),
"B" = c("First", "Second", "Third", "Fourth"))
rep(vals, length = nrow(mtcars))
})
mtcars %>% ggvis(x = ~wt, y = ~mpg, fill = new_vals) %>%
layer_points()
mtcars %>% ggvis(
x = ~wt,
y = ~mpg,
fill = input_select(c("mpg", "wt"), map = as.name)
) %>%
layer_points()
6.4 Layers
6.4.1 Simple layers
include primitives like points, lines and rectangles.
Paths and polygons
If you add fill
, you will get a polygon
t <- seq(0, 2 * pi, length = 100)
df <- data.frame(x = sin(t), y = cos(t))
df %>% ggvis(~x, ~y) %>% layer_paths(fill := "red")
Rectangle
set.seed(1014)
df <- data.frame(x1 = runif(5), x2 = runif(5), y1 = runif(5), y2 = runif(5))
df %>% ggvis(~x1, ~y1, x2 = ~x2, y2 = ~y2, fillOpacity := 0.1) %>% layer_rects()
Text
df <- data.frame(x = 3:1, y = c(1, 3, 2), label = c("a", "b", "c"))
df %>% ggvis(~x, ~y, text := ~label) %>% layer_text(fontSize := 50)