Show Menu
Cheatography

Data Visualization in R: ggvis continued Cheat Sheet by

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

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

 

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

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

 

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

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­(er­upt­ions)) %>% 

    layer_­poi­nts() %>% 

    add_le­gen­d(c­("fi­ll", "­sha­pe", "­siz­e"), 

               title = "~ duration (m)", values = c(2, 3, 4, 5))
   
 

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