Data Visualization in R: ggvis continued Cheat Sheet

A Programming › R cheat sheet in English by shanly3011
23,852 views 5,716 downloads 10 Apr 2015 History
4.4 avg · 4 ratings

ggvis & Group_by

When these 2 are used in conjun­ction, we can create powerful visual­iza­tions.

Code:
train_tbl %>%
group_­by(­season) %>%
ggvis(temp_f,count, stroke = ~facto­r(s­eason)) %>%
layer_­smo­oths()
Here, season is a catego­rical variable. And we have grouped it and then used stroke to highlight the different seasons.

Output

Output

In-Built plot types

`1. layer_points()`
`2. layer_lines()`
`3. layer_bars()`
`4. layer_smooths()`
`5. layer_histograms()`

Most popular ones cited

Global Vs Local properties

A property that is set inside ggvis() is applied globally. While a property set inside layer_­<ma­rks­>() is applied locally.
Local properties can override global properties when applic­able.

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_­log­ical(), scale_­nom­inal(), scale_­num­eric(), scale_­sin­gular()

Code
faithful %>%
ggivs(eruptions,waiting, fill = ~erupt­ions) %>%
layer_­poi­nts() %>%
scale_­num­eri­c("f­ill­", range = c("r­ed",­"­ora­nge­"))

Output

Output

ggvis & intera­ction ()

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.

ungroup() can remove the grouping inform­ation.
intera­ction() can map the properties to unique combin­ations of the variables

Code:

train_tbl %>%
group_­by(­sea­son­,ho­liday) %>%
ggvis(count, fill = intera­cti­on(­sea­son­,ho­liday)) %>%
layer_­den­sit­ies()

Output

Output

Model Prediction

layer_­mod­el_­pre­dic­tions() plots the prediction line of a model fitted to the data.
layer_­mod­el_­pre­dic­tio­ns(­model = "­lm")

Code:

faithful %>%
ggvis(eruptions,waiting) %>%
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")

Output

Output

Intera­ctive Plots

ggivs comes several widgets such as
input_­che­ckb­ox(),
input_­che­ckb­oxg­roup(),
input_­num­eric(),
input_­rad­iob­utt­ons(),
input_­sel­ect(),
input_­sli­der(), and input_­text().

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

Are the common arguments inside these functions.

Output

Output

Legends & Axis

Axis
You can add axes with add_axis()

Syntax:
faithful %>%
ggvis(eruptions,waiting) %>%
add_ax­is(­"­x", label = "­Eru­pti­ons­", values = c(1,2,­3,4), subdivide = 9, orient = top") %>%
layer_­poi­nts()

Legends
ggvis adds a legend for each property that is specified. To combine multiple legends into a single legend with common values, use a vector of property names.
add_le­gend()
hide_l­egend()

Syntax
faithful %>%
ggvis(waiting, eruptions, opacity := 0.6,
fill = factor­(ro­und­(er­upt­ions)), shape = factor­(ro­und­(er­upt­ions)),
size = round(­eru­pti­ons)) %>%
layer_­poi­nts() %>%
add_le­gen­d(c­("fi­ll", "­sha­pe", "­siz­e"),
title = " duration (m)", values = c(2, 3, 4, 5))

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.