library(ggvis)
library(shiny)

# Let’s plot our first graph
# For this we will use “mtcars” dataset, and
# on x-axis we will have weight of engines “wt”,
# and on y-axis, “miles per gallon (mpg)”.
# i.e. how MPG vaies with Wt of engines in different models of cars
p <- ggvis(data = mtcars, x = ~wt, y = ~mpg)
# nothing is plotted as we stored the plot into a vector “p”
# if we excute “p”, ggvis will guess the layer point, as they are absent
# in the above code…
p
# Guessing layer_points()

# we will address this “guessing” issue by supplying layer points to plot:
layer_points(p)
# plot is same as earlier (default) plot

# alternate to above can be:
layer_points(ggvis(data = mtcars, x = ~wt, y = ~mpg))
# or
layer_points(mtcars %>% ggvis(~mpg, ~wt))

# the last code above can be modified to generate something like this:
layer_points(mtcars %>% ggvis(~mpg, ~wt, fill = ~cyl))
# or
layer_points(mtcars %>% ggvis(~mpg, ~wt, fill := “red”))

# if needed, you can plot smooth lines, as in:
layer_smooths(ggvis(data = mtcars, x = ~wt, y = ~mpg))

# The whole thing can be modified to look easier and to play around with
# multiple options… :

mtcars %>%
ggvis(x = ~wt, y = ~mpg, fill := “red”) %>%
layer_points() %>%
layer_smooths()
## Aaahaa!!
## So you can start playing around…

mtcars %>%
ggvis(x = ~wt, y = ~mpg, fill := “red”) %>%
layer_points() %>%
layer_smooths(span = input_slider(0.2, 1))

# In the mean time, we do some more things…
# Lets’

library(dplyr)
mtcars %>%
# how MPG varies with respect to engine displacements
ggvis(y = ~mpg, x = ~disp, fill:= “red”) %>%
mutate(disp = disp / 61.0237) %>%
# Above, converts engine displacment to litres
layer_points() %>%
layer_smooths()

mtcars %>%
ggvis(y = ~mpg, x = ~disp, fill := “red”) %>%
mutate(disp = disp / 61.0237) %>%
layer_points() %>%
layer_smooths(se = TRUE, stroke := “blue”, fill := “blue”, strokeWidth := 2) %>%
# smoothing with fitting of ordinary linear model
layer_model_predictions(model = “lm”) %>%
# smoothing with robust fitting of linear model
layer_model_predictions(model = “MASS::rlm”, stroke := “green”)

# Factoring the displacement w.r.t. cylinder
mtcars %>%
ggvis(y = ~mpg, x = ~disp, fill:= “yellow”, size = ~vs,
stroke := “red”, strokeWidth := 2,
shape = ~factor(cyl)) %>%
mutate(disp = disp / 61.0237) %>%
layer_points()

# Resizing and changing Opacity of the plotting points
mtcars %>%
ggvis(y = ~mpg, x = ~disp, fill:= “yellow”, size := 300, # Change the size
opacity := 0.4,  # And change the opacity
stroke := “red”, strokeWidth := 2,
shape = ~factor(cyl)) %>%
mutate(disp = disp / 61.0237) %>%
layer_points()

mtcars %>%
ggvis(~wt, ~mpg,
size := input_slider(10, 100),
opacity := input_slider(0, 1)
) %>%
layer_points()

# Allowing a user to be able to change the size of points and opacity
mtcars %>%
ggvis(y = ~mpg, x = ~disp, fill:= “yellow”,
# Now, you can change the size of points
size := input_slider(10, 300),
# And also, the Opacity
# (Invisible (= 0) to fully visible (= 100))
opacity := input_slider(0, 1),
stroke := “red”, strokeWidth := 2,
shape = ~factor(cyl)) %>%
mutate(disp = disp / 61.0237) %>%
layer_points()

# Can also do it with the help of keyboard’s left / right arrow keys
# TRY IT !! (you may have to mouse click on the plot once)
keys <- left_right(10, 1000, step = 50)
mtcars %>%
ggvis(y = ~mpg, x = ~disp, fill:= “yellow”,
# Now, you can change the size of points
# using  “<-” & “->” keys
size := keys,
# And also, the Opacity
# (Invisible (= 0) to fully visible (= 100))
opacity := input_slider(0, 1),
stroke := “red”, strokeWidth := 2,
shape = ~factor(cyl)) %>%
mutate(disp = disp / 61.0237) %>%
layer_points()
## Aaaahaa!!

Advertisements