class: center, middle, inverse, title-slide # ggvis @ Rfun ### John Little ### 2017-03-06 --- ## Visualization with ggvis When in the data exploratory phase, you can quickly generate basic visualizations with minimal effort. --- ## Grammar of Graphics - The ggvis visualization package combines - the grammar of graphics in ggplot - reactive programming from shiny - data transformations pipelines (e.g. dplyr/tidyverse) - Video: https://www.youtube.com/watch?v=qtgQMxhAJLQ --- ## Impressions - Very similar to ggplot2 - layers - data - geom - coordinate system - a transferable training for ggplot2 - easier to learn if you are into the tidyverse - same syntax, pipes `%>%` - generates basic plots - see [cookbook](http://ggvis.rstudio.com/cookbook.html) --- ## Impressions: Continued - works with shiny - everything you can do in ggvis you can transfer to ggplot2 (or shiny) - not a replacement for advanced graphics of ggplot2 - Still "under development" - version 0.4.3 But, very capable, quick, good for development, great for casual vis and can be fine for formal vis --- ```r library(tidyverse) library(ggvis) cars <- mtcars ``` --- ### Scatter Plot Linear model and 95% confidence interval ```r cars %>% ggvis(~wt, ~mpg) %>% layer_points() %>% layer_model_predictions(model = "lm", se=TRUE) ```
Renderer:
SVG
|
Canvas
Download
--- class: bottom, center background-image: url(images/scatter1.svg) See [cookbook for more information on ggvis scatterplots](http://ggvis.rstudio.com/cookbook.html#scatterplots) --- ### Bar Graph ```r cars %>% mutate(cyl = factor(cyl, labels = c("four","six","eight"))) %>% ggvis(~cyl) %>% layer_bars() ```
Renderer:
SVG
|
Canvas
Download
--- class: bottom, center background-image: url(images/bar1.svg) See [cookbook for more information on ggvis bar graphs](http://ggvis.rstudio.com/cookbook.html#bar-graphs) --- ### Bargraph 2 managing color and sort order ```r mpg %>% ggvis(~factor(mpg$class, levels = names(sort(table(mpg$class), decreasing = TRUE))), ~hwy, fill := "navy", stroke := "black") ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/bar2.svg) --- ### Histogram ```r cars %>% ggvis(~mpg) %>% layer_histograms() ```
Renderer:
SVG
|
Canvas
Download
--- class: bottom, center background-image: url(images/hist1.svg) See [cookbook for more information on ggvis histograms](http://ggvis.rstudio.com/cookbook.html#histograms) --- ### Line graph ```r cars %>% ggvis( ~wt, ~mpg) %>% group_by(cyl) %>% layer_lines(stroke = ~factor(cyl)) %>% layer_points(~wt, ~mpg, stroke := "black", fill = ~factor(cyl)) ```
Renderer:
SVG
|
Canvas
Download
--- class: bottom, center background-image: url(images/line1.svg) See [cookbook for more information on line graphs](http://ggvis.rstudio.com/cookbook.html#line-graphs) --- ```r cars %>% ggvis(~wt, ~mpg) %>% group_by(cyl) %>% layer_model_predictions(model="lm", se = TRUE) %>% layer_points(~wt, ~mpg, stroke := "black", fill = ~factor(cyl), size := ~hp, opacity := 0.7) ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/line2.svg) --- ### Boxplot ```r cars %>% mutate(cyl = factor(cyl, labels = c("four","six","eight"))) %>% ggvis(~cyl, ~mpg) %>% layer_boxplots() ```
Renderer:
SVG
|
Canvas
Download
--- class: bottom, center background-image: url(images/box1.svg) See [cookbook for more information on boxplots](http://ggvis.rstudio.com/cookbook.html) Hint: the info is at the bottom of the page --- ### BoxPlot with *mpg* dataset ```r mpg %>% ggvis(~class, ~hwy) %>% layer_boxplots() ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/box2.svg) --- ## Comparison to ggplot2 - [Comparison - Stack Exchange](http://stats.stackexchange.com/questions/117078/for-plotting-with-r-should-i-learn-ggplot2-or-ggvis/125124#125124) - [Comparison - @ggvis v ggplot2](http://ggvis.rstudio.com/ggplot2.html) --- ### Scatterplot: X Y axis ```r # ggplot2 # # ggplot(mpg, aes(displ, hwy)) + # geom_point() #ggivs mpg %>% ggvis(~displ, ~hwy) ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/mpg-scatter.svg) --- ### Color Points fill = ~class fill = "#339999" fill = "red" ```r # ggplot2 # ggplot(mpg, aes(displ, hwy)) + # geom_point(aes(color = class)) # ggvis mpg %>% ggvis(~displ, ~hwy) %>% layer_points(fill = ~class) ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/scatter-color.svg) --- ### Labels add_axis("x", title = "foo title here") ```r # ggplot2 # ggplot(mpg, aes(displ, hwy)) + # geom_point(aes(color = class)) + # labs(x = "Engine Displacement, in Liters", # y="Highway Miles per Gallon") # ggvis mpg %>% ggvis(~displ, ~hwy) %>% layer_points(fill = ~class) %>% add_axis("x", title = "Engine Displacement, in Liters") %>% add_axis("y", title = "Highway Miles per Gallon") ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/scatter-legend.svg) --- ### Size size:= 150 ```r # ggplot2 # ggplot(mpg, aes(displ, hwy)) + # geom_point(aes(color = class), size=7) + # labs(x = "Engine Displacement, in Liters", # y="Highway Miles per Gallon") # ggviz mpg %>% ggvis(~displ, ~hwy) %>% layer_points(fill = ~class, size := 150) %>% add_axis("x", title = "Engine Displacement, in Liters") %>% add_axis("y", title = "Highway Miles per Gallon") ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/mpg-size.svg) --- ### Opacity opacity := 0.5 ```r # ggplot2 # ggplot(mpg, aes(displ, hwy)) + # geom_point(aes(color = class), size=7, alpha=0.5) + # labs(x = "Engine Displacement, in Liters", # y="Highway Miles per Gallon") # ggviz mpg %>% ggvis(~displ, ~hwy) %>% layer_points(fill = ~class, size := 150, opacity := 0.5) %>% add_axis("x", title = "Engine Displacement, in Liters") %>% add_axis("y", title = "Highway Miles per Gallon") ```
Renderer:
SVG
|
Canvas
Download
--- background-image: url(images/mpg-opacity.svg) --- ### Interactivity size := input_slider(10, 700, value = 75) ```{} mpg %>% ggvis(~displ, ~hwy) %>% layer_points(fill = ~class, size := input_slider(10, 700, value = 75), opacity := 0.5) %>% add_axis("x", title = "Engine Displacement, in Liters") %>% add_axis("y", title = "Highway Miles per Gallon") ``` To see this output, run this in your RStudio. The output wil be in the Viewer pane. Ideally, this interactive output is done in concert with Shiny. Learn more about itneractivity at the [ggvis-interactive documentation](http://ggvis.rstudio.com/interactivity.html). --- class: center, middle ### Shareable under CC BY-NC license Data, presentation, and handouts are shareable under [CC BY-NC license](https://creativecommons.org/licenses/by-nc/4.0/) ![This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.](https://licensebuttons.net/l/by-nc/4.0/88x31.png "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License")