Data Visualization in R: ggvis Cheat Sheet

A Programming › R cheat sheet in English by shanly3011
20,283 views 4,743 downloads 9 Apr 2015 History
4.9 avg · 1 rating

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(waiting) %>% 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 variab­le's density line, and resp_, the y values of the variab­le'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(waiting, fill := "­gre­en") %>% layer_­den­sit­ies()

Badges

Nice Cheat Sheet Good Cheat Sheet Downloaded from Unusual Locations × 1

Comments

No comments yet.

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.