ggplot2 II EPID 799C Wed Sep New Packages (install now!)

Slides:



Advertisements
Similar presentations
Rich Pugh Andy Nicholls Head to Head: Lattice vs ggplot2 Rich Pugh
Advertisements

Animating Web Graphics Using Adobe Edge. 1. Setting up your project Create a new folder on your desktop Call it [YOURNAME]infographic INSIDE THAT FOLDER,
Plotting with ggplot2: Part 1
BIOMES Lab 5 ArcGIS. Making Maps Minimize white space Minimize white space Always include: Always include: Title Title Legend Legend Your name Your name.
Python plotting for lab folk Only the stuff you need to know to make publishable figures of your data. For all else: ask Sourish.
Copyright © Allyn & Bacon 2008 POWER PRACTICE Chapter 6 Academic Software START This multimedia product and its contents are protected under copyright.
Baburao Kamble (Ph.D) University of Nebraska-Lincoln
Introduction to Flash FYS100 Creative Discovery in Digital Art Forms Spring 2007 Burg.
Ranjeet Department of Physics & Astrophysics University of Delhi Working with Origin.
Graphing Parameters Titles X-Axis Title Y-Axis Title Legend Scales Color Gridlines library(help="graphics") Basic Chart Types The R Graphics Package LineHistogram.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Ggplot2 A cool way for creating plots in R Maria Novosolov.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Fraction Stax. Naming the Pieces Stack the black piece on one of the pegs. This is one whole. Stack the two orange pieces on the peg. The orange pieces.
MIS2502: Data Analytics Introduction to Advanced Analytics and R.
R PROGRAMMING FOR SQL DEVELOPERS Kiran Math Developer : Proterra in Greenville SC
Title Subtitle. Statement #1 Delete this text box, and add a powerful image from your “Go Green” folder. Make the image fill the screen (and don’t place.
Visual Displays of Data Chapter 3. Uses of Graphs Positive and negative uses – Can accurately and succinctly present information – Can reveal/conceal.
Professional Template for a 48x36 poster presentation
PAPER TITLE (TEMPLATE FOR A 841MM X 594MM POSTER PRESENTATION)
Overview of R and ggplot2 for graphics
Ggplot2 Wu Shaohuan.
Introduction to Illustrator
TIP │Use color to tell story, arrange multiple graphics together, add icons on top of charts to create custom graphics. EDITABLE GRAPHIC AT END OF PRESENTATION.
By Dr. Madhukar H. Dalvi Nagindas Khandwala college
ggplot2 Merrill Rudd TAs: Brooke Davis and Megsie Siple
Professional Template for a poster presentation
Introduction to R Programming with AzureML
Next Generation R tidyr, dplyr, ggplot2
Introduction to R Studio
Summary Statistics in R Commander
Research Conclave 2017 Template for a 4 ft (height) x 3ft (width) poster presentation Your name and the names of the people who have contributed to this.
Template for a 36x48 poster presentation
QM222 A1 Visualizing data using Excel graphs
PowerPoint Day 1 Credibility Builder
Professional Template for a 48x36 poster presentation
Skull/Height Project We will count off by threes. Then we will measure the head (skull height) and overall height of each 2nd person.. Add more rows.
Comprehend. Create. Communicate. Achieve More.
Data visualization in Python
Dplyr I EPID 799C Mon Sep
Ggplot2 I EPID 799C Mon Sep
Data Visualization using R
Professional Template for a 48x36 poster presentation
The names of the authors The affiliation names go here
Professional Template for a 36x48 poster presentation
Basic Graphics Drawing Shapes 1.
Types of Maps and Parts of Maps
Statistical Methods For Engineers
Recoding II: Numerical & Graphical Descriptives
R Programming For Sql Developers ETL USING R
Add some WordArt to your cover slide
Installing Packages Introduction to R, Part II
Overview of R and ggplot2 for graphics
Lecture 7 – Delivering Results with R
Generalized Linear Models (GLM) in R: Part 2
Professional Template for a 36x48 poster presentation
R course 6th lecture.
Professional Template for a 36x48 poster presentation
Now watch me solve (Do it. ) Measures of center (Yeah
Chapter 1 Introducing Small Basic
R for Epi Workshop Module 3: Data visualization with ggplot
DATA VISUALISATION (QUANTITATIVE).
A brief introduction to the nutrient tool-kit, getting R Studio to work and checking the data Martyn Kelly
Digital Mapping Welcome to a short presentation on how to create a very basic event route using digital mapping. In this example, we will be using Quo.
The Grammar of Graphics
Spark with R Martijn Tennekes
Presentation transcript:

ggplot2 II EPID 799C Wed Sep 19 2017 New Packages (install now!) RColorBrewer ggrepel plotly

Today’s Overview More ggplot: Homework 1: due today! Scales Colors ggplot extension packages Themes Homework 1: due today! Homework 2: out by tomorrow morning!

ggplot2 stats, scales, colors, themes

Ggplot components data aesthetic mapping geometric object statistical transformations scales coordinate system position adjustments faceting

Ggplot components

Let’s Try Review: make a scatterplot ggplot of the mpg dataset where displ and hwy are x and y. Overlay a linear relationship line by group using geom_smooth(). Color all of it by the class variable. Add and tweak some geoms add a layer with a black line modeling the overall relationship of the data. Hint: override… something! We have overplotting going on (how you can tell?). Lower the points alpha and jitter to see the problem. Add a geom_rug, just cuz. Jitter it as well.

Answers # start with ggplot(mpg, aes(displ, hwy, color=class))+ geom_smooth()+ geom_point(alpha=0.2)+ # add geoms geom_jitter()+ geom_smooth(color="black")+ geom_rug(position = "jitter")

scales: position Some form of: scale_x_continuous(limits=c(a,b), breaks=a:b) …is pretty common A note on clipping:

Let’s Try: Playing with scales

Answers ggplot(mpg, aes(displ, hwy, color=class))+ geom_jitter()+ geom_smooth()+ geom_smooth(color="black")+ #scale_x_continuous(limits=c(0,NA))+ #scale_y_continuous(limits=c(0,NA)) #scale_x_continuous(limits=c(0,5)) coord_cartesian(xlim = c(0,5)) #change the "window" (needs all limits)

scales: color Typically using brewer or manual

scales: color brewer RColorBrewer::display.brewer.all() http://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3 RColorBrewer::display.brewer.all() Also, not covered, if you’re making maps, see tmaptools::palette_explorer()

Let’s Try

Answers ggplot(mpg, aes(displ, hwy, color=class))+ geom_point()+ #scale_color_discrete() #scale_color_manual(values = c("red", "yellow", "green", "purple", "orange", "blue", "black")) #scale_color_brewer("Blues") #scale_color_brewer(type="seq", palette="OrRd", guide="colorbar") #scale_color_brewer(type="div", palette="Spectral", guide="legend") #scale_color_brewer(type="div", palette="OrRd") #scale_color_manual(values = RColorBrewer::brewer.pal(7, "Blues"))

positions

positions dodge fill jitter nudge stack ggplot(mpg, aes(fl, fill=drv))+ geom_bar(position=“<POSITION>”) Where <POSITION> is one of: dodge fill jitter nudge stack Can be tweaked further with the function equivalents position = position_dodge(width=1) Or a few more detailed versions …+geom_label(aes(label=mylabs), nudge_y=1)

Let’s Try

Answers # Positions ggplot(mpg, aes(fl, fill=drv))+geom_bar() ggplot(mpg, aes(fl, fill=drv))+geom_bar(position="stack") ggplot(mpg, aes(fl, fill=drv))+geom_bar(position="dodge") ggplot(mpg, aes(fl, fill=drv))+geom_bar(position="fill") ggplot(mpg, aes(fl, fill=drv))+geom_bar(position="jitter") #ugh. ggplot(mpg, aes(cty, hwy, color=drv))+geom_point(position="jitter") ggplot(mpg, aes(cty, hwy, color=drv, label=drv))+geom_point()+geom_text(nudge_y = 2)

coordinate systems Rarely used, except for coord_flip() Another solution in helper packages

labels labs(), like last time, covers almost every label: x y title subtitle caption And can also do legend titles… …or do in the scale_*() function Use g+annotate() to just stick something (anything!) right where you want it.

Let’s Try

Answers # Labels ggplot(mpg, aes(displ, hwy, color=class))+ geom_point()+ annotate(geom="text", x=1, y=4, label="Note: nothing in \n this corner")+ annotate(geom="point", x=1, y=4, color="red")+ scale_y_continuous(limits = c(0,NA))+ scale_x_continuous(limits = c(0,NA)) # Add labs #coord_flip()

Slightly More Advanced: stats Put on your thinking caps

Layer=data+stats+geoms Hadley’s (package author) underlying theory from grammar of graphics: layer=data+stats+geoms. Often stat or geom imply the other, so each has a default parameter of the other. http://vita.had.co.nz/papers/layered-grammar.pdf Excellent Stack Overflow Review https://stackoverflow.com/questions/38775661/what- is-the-difference-between-geoms-and-stats-in-ggplot2

Let’s Try http://ggplot2.tidyverse.org/reference/

Let’s dig in… Default stats for geoms: http://sape.inf.usi.ch/quick- reference/ggplot2/geom

stat variables geoms, behind the scenes, often calculate a new dataframe to actually plot on screen. stat_<thing> calculates a new dataframe explicitly That dataframe has some “secret” (documented) variable names, accessible by special inline variables ..count.. ..ncount.. ..density.. ..ndensity.. ..count.. ..prop.. Etc. What if I want to refer to those new variables elsewhere in the ggplot call? (kinda guts / rare use)

Let’s Try

Answers # Stats and geoms ggplot(mpg, aes(displ))+geom_bar(stat="count") ggplot(mpg, aes(displ))+stat_count(geom) ggplot(mpg, aes(displ))+geom_bar(stat="density", aes(fill=..density..), color=NA) ggplot(mpg, aes(displ))+geom_bar(stat="density", aes(color=..scaled..)) ggplot(mpg, aes(displ))+geom_bar(stat="density", aes(color=..count..)) ggplot(mpg, aes(displ))+geom_bar(stat="bin") ggplot(mpg, aes(displ, hwy))+geom_tile(stat="bin_2d") ggplot(mpg, aes(displ, hwy))+geom_hex(stat="") ggplot(mpg, aes(displ, hwy))+geom_bar(stat="bin_2d") ggplot(mpg, aes(cty, hwy, z=displ))+ geom_point(alpha=0.2)+geom_jitter(stat="unique")+ stat_ellipse() ggplot(mpg, aes(cty))+geom_bar(stat="density", aes(fill=..count..)) # stat = count or bin ggplot(mpg, aes(y=cty, x=manufacturer))+geom_boxplot(aes(color=..lower..)) #ggsave()

And: Saving a ggplot The usual GUI / Rstudio way Simplest version is super easy. Save my last ggplot: ggsave(“filename.png”) ggsave(“filename.pdf”) etc. Can fool with size (width, height in inches, pixels, etc.)

ggplot2 Extensions Extending the language

Overview Extensions to ggplot2 add geometries, statistics, themes… all within the language you now know. Dedicated website for them: http://www.ggplot2-exts.org/ And many in development elsewhere. Let’s look at some greatest hits

plotly https://plot.ly/ Minimal use? ggplotly() Can also send plots to your account online, integrate with shiny (renderPlotly and outputPlotly), etc.

ggrepel Already have geom_text() and geom_label (label draws a text box, text is just text) Now have geom_text_repel() and geom_label_repel() to use a “force” to spread out data and labels.

Let’s Try

Answers library(plotly) ggplot(mpg, aes(y=cty, x=manufacturer))+geom_boxplot(aes(color=..middle..)) ggplotly() ggplot(mpg, aes(cty, hwy))+geom_label(aes(label=manufacturer)) library(ggrepel) ggplot(mpg, aes(cty, hwy))+geom_jitter()+geom_text(aes(label=manufacturer)) ggplot(mpg, aes(cty, hwy, color=manufacturer))+geom_jitter()+geom_text(aes(label=manufacturer), nudge_y=1) ggplot(mpg, aes(displ, cyl, color=drv))+geom_point(stat="unique")+geom_text_repel(aes(label=manufacturer), stat="unique",force=0.01)

ggforce https://github.com/thomasp85/ggforce https://cran.r- project.org/web/packages/ggforce/vignettes/Visual _Guide.html

ggstance coord_flip() alternative

ggmosaic https://github.com/haleyjeppson/ggmosaic/blob/ master/vignettes/ggmosaic.Rmd

Survminer (also GGAlly::ggsurv())

ggridges https://cran.r- project.org/web/packages/ggridges/vignettes/intro duction.html

GGally

So,so many themes https://github.com/cttobin/ggthemr ggthemes, etc.

ggsci https://ggsci.net/ : ggsci offers a collection of ggplot2 color palettes inspired by scientific journals, data visualization libraries, science fiction movies, and TV shows

Special mention: broom Broom… tidies data! Available Tidiers Currently broom provides tidying methods for many S3 objects from the built-in stats package, including lm glm htest anova nls kmeans manova TukeyHSD arima It also provides methods for S3 objects in popular third-party packages, including Lme4 glmnet boot gam survival Lfe zoo multcomp sp maps

Thinking big: other packages For example, treemapify https://github.com/wilkox/treemapify How to install dev versions of things…

Thinking big: Sankey diagrams How many ways to cut it: https://stackoverflow.com/questions/9968433/san key-diagrams-in-r Good example

Next week: dplyr! Highly recommend doing something this weekend about dplyr.