Show Menu
Cheatography

Data Visualization in R through ggvis Cheat Sheet by

This cheat sheet is made for the ggvis library in R for effective visualization of data

Layers

 

Simple Layer


mtcars %>% ggvis(mpg, disp, fill = ~vs) %>% layer_points()
Here I am using the dataset mtcars and visual­ising it through layer points.

Output

Multiple Layer

mtcars %>% ggvis(wt, mpg) %>%
  layer_smooths(span = 1) %>%
  layer_smooths(span = 0.3, stroke := "red")
I have taken the mtcars dataset and visualized the multiple layers using different strokes

Output

Instal­lation

instal­l.p­ack­age­s("g­gvi­s")
librar­y(g­gvis)
-insta­­ll.p­­ac­­kag­­es­(­"­­ggv­­is­") will install all the required packages you need for visual­­iz­ation through ggvis
-libra­­ry­(­g­gvis) will call the ggvis package to be used in your visual­­iz­ation
 

Global Vs Local properties

A property that is set inside ggvis() is applied globally. While a property set inside layer­­_<m­­ar­k­s­>() is applied locally.
Local properties can override global properties when applic­­able.

Graphics

The graphics produced by ggvis are fundam­entally web graphics and work very differ­ently from tradit­ional R graphics. This allows us to implement exciting new features like intera­ctivity
The goal of ggvis is to make it easy to build intera­ctive graphics for explor­atory data analysis. ggvis has a similar underlying theory to ggplot2 (the grammar of graphics).

Popular In-Built plot types

1. layer_­poi­nts()

2. layer_­lines()

3. layer_­bars()

4. layer_­smo­oths()

5. layer_­his­tog­rams()

Output

Group_by

train_tbl %>%
group­_by­(se­ason) %>%
ggvis­(t­emp­_f,­count, stroke = ~facto­r(s­eason)) %>%
layer­_sm­oot­hs()
I have taken season dataset here, and season is a catego­­rical variable. And we have grouped it and then used stroke to highlight the different seasons.
 

Scale Types

Any visual property in the visual­ization can be adjusted with scale().
ggvis provides several different functions for creating scales:
scale_­dat­eti­me(), scale­_lo­gic­al(), scale_­nom­inal(), scale_­num­eric(), scale_­sin­gul­ar()

Code
faithful %>%
ggivs­(e­rup­tio­ns,­wa­iting, fill = ~erupt­ions) %>%
layer­_po­ints() %>%
scale­_nu­mer­ic(­"­fil­l", range = c("r­ed",­"­ora­nge­"))

Output

ggvis & intera­­ction ()

train_tbl %>%
group­_by­(se­aso­n,h­oliday) %>%
ggvis­(c­ount, fill = inter­act­ion­(se­aso­n,h­oli­day)) %>%
layer­_de­nsi­ties()
We can also group data based on intera­­ction of two or more variables. group­­_by() creates unique groups for each distinct combin­­ation of values within the grouping variables.

ungro­up() can remove the grouping inform­­ation.
inter­­act­­ion() can map the properties to unique combin­­ations of the variables

Output

 

Model Prediction

faithful %>%
ggvis(­er­upt­ion­s,­wai­ting) %>%
layer_­poi­nts­(fill := "­gre­en", fillOp­acity := 0.5) %>%
layer_­mod­el_­pre­dic­tio­ns(­model = "­lm", stroke := "­red­") %>%
layer_­smo­oth­s(s­troke := "­sky­blu­e")
layer_­­mo­d­e­l_­­pre­­di­c­t­ions() plots the prediction line of a model fitted to the data.
layer­­_mo­­de­l­_­pr­­edi­­ct­i­o­ns­­(model = "­­lm­")

Output

Intera­­ctive Plots

ggivs comes several widgets such as

input_­­ch­e­c­kb­­ox(),
input­­_ch­­ec­k­b­ox­­gro­­up(),
input­­_nu­­me­r­ic(),
input­­_ra­­di­o­b­ut­­ton­­s(),
input_­­se­l­e­ct(),
input­­_sl­­id­er(), and input_­­te­x­t().

label = "ABCD " , choices = c("r­­ed­"­,­"­­bla­­ck­") -
value = "­­bl­a­c­k" - Used with input_­­text()
map = as.name used when we want to return variable names

Are the common arguments inside these functions.

Output

           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          ggvis_18BCE2193 Cheat Sheet