Show Menu
Cheatography

Data Visualization in R: ggvis Cheat Sheet by

ggvis Grammar

The 4 essential components are -
1. Data
2. Coordinate system
3. Marks
4. Properties

Syntax Example-

faithful %>% 

ggvis(waiting,eruptions) %>% layer_­poi­nts() %>%

add_ax­is(­"­x", title = "­Waiting period­", values = c(1,2,­3,4­,5,­6,7), subdivide = 9)
We use the piping operator '%>%' for our syntaxes.

Mapping Vs Setting propeties

Mapping
Setting
=
maps property to a data value
:=
sets property to a specific size/c­olu­or/­width
Used for visual­ization of data
Used for custom­izing the appearance of plots
ggvis scales the values to a pre-de­fined scale of colour­/sizes
ggvis sends the colour value to vega - a javascript package for further processing
 

Properties for points

The properties for points are -
fill,x, y, stroke, stroke­Width, stroke­Opa­city, fill, opacity, fillOp­acity, shape, size


Sample code:
`
faithful %>% `
ggvis(waiting, eruptions, fillOp­acity = ~erupt­ions, size := 100,  fill := "­red­", stroke := "­red­", shape := "­cro­ss") %>% 

layer_­poi­nts()

Properties for lines

The properties for lines include -

x, y, fill, fillOp­acity, opacity, stroke, stroke­Dash, stroke­Opa­city, and stroke­Width
 

Transf­orm­ations

comput­e_s­mooth
comput­e_bin()
It transforms the data to generate a new dataframe.
It transforms the data to generate a new dataframe.
It returns a dataset with 2 variables, one named pred_ and the other resp_
It returns a dataset with 4 variables, x, x2 ,y ,y2.
Syntax: comput­e_s­mooth()
Long way:
mtcars %>% comput­e_s­moo­th(mpg  wt) %>% ggvis(pred_,­~resp_) %>% layer_­lines()


In-built:
mtcars %>% ggvis(wt,mpg) %>% layer_­smo­oths()


Syntax: comput­e_bin()
Long way:
faithful %>% comput­e_bin(waiting, width = 5) %>% ggvis(x =  xmin_, x2 =  xmax_, y = 0, y2 = count_) %>% layer_­rects()


In-built:
faithful %>% ggvis(­~wa­iting) %>% layer_­his­tog­ram­s(width = 5)
 

Transf­orm­ations

comput­e_d­ens­ity()

A density plot uses a line to display the density of a variable at each point in its range.

It returns a data frame with two columns: pred_, the x values of the variable's density line, and resp_, the y values of the variable's density line.

Similarly, we have comput­e_c­ount() or the in- built function layer_­bars()
Syntax: comput­e_s­mooth()
Long way:
faithful %>% comput­e_d­ensity(waiting) %>% ggvis(pred_, ~resp_) %>% layer_­lines()


In-built:
faithful %>% ggvis(­~wa­iting, fill := "­gre­en") %>% layer_­den­sit­ies()
   
 

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

          Data Visualization in R through ggvis Cheat Sheet
          ggvis_18BCE2193 Cheat Sheet

          More Cheat Sheets by shanly3011