Ggplot2 A cool way for creating plots in R Maria Novosolov.

Slides:



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

Lesson 14 Creating Formulas and Charting Data
® Microsoft Office 2010 Excel Tutorial 4: Enhancing a Workbook with Charts and Graphs.
Chapter 3 Drawing and Composing an Illustration. Objectives Draw straight lines Draw curved lines Draw elements of an illustration Apply attributes to.
Plotting with ggplot2: Part 1
Introduction to FX Stat 3. Getting Started When you open FX Stat you will see three separate areas.
Reading Graphs and Charts are more attractive and easy to understand than tables enable the reader to ‘see’ patterns in the data are easy to use for comparisons.
FIRST COURSE Excel Tutorial 4 Working with Charts and Graphics.
Data Visualization with R (II)
COMPREHENSIVE Excel Tutorial 4 Working with Charts and Graphics.
Using Charts in a Presentation Lesson 6. Software Orientation Charts can help your audience understand relationships among numerical values. The figure.
Understanding and Comparing Distributions
Excel Lesson 6 Enhancing a Worksheet
Create a chart. Overview: Charts make data visual Lesson 1: Create a basic chart Lesson 2: Customize your chart COURSE CONTENTS Each lesson includes a.
Pasewark & Pasewark 1 Excel Lesson 8 Working with Charts Microsoft Office 2007: Introductory.
Instructor: Professor Cora Martinez, PhD Department of Civil and Environmental Engineering Florida International University.
WORKING WITH CHARTS. OBJECTIVES  Plan a chart  Create a chart  Move and resize a chart  Change the chart design  Change the chart layout  Format.
Some Possibly Useful Graphics Functions Lunch presentation.
Charts and Graphs V
Computer Literacy BASICS
11 Chapter 3: Getting Started with Tasks 3.1 Introduction to Tasks and Wizards 3.2 Creating a Frequency Report 3.3 Generating HTML, PDF, and RTF Output.
Microsoft Office Illustrated Fundamentals
Baburao Kamble (Ph.D) University of Nebraska-Lincoln
C51BR Applications of Spreadsheets 1 Chapter 16 Getting Started Making Charts.
Lecture 3 + Seminar 3 A workshop on graphing using ggplot2.
Exploring Excel 2003 Revised - Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 3 – Graphs and Charts: Delivering a.
Examples of different formulas and their uses....
Chapter 9 Creating and Designing Graphs. Creating a Graph A graph is a diagram of data that shows relationship among a set of numbers. Data can be represented.
The Scientific Method Honors Biology Laboratory Skills.
Microsoft Office Illustrated Introductory, Premium Edition with Charts Working.
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
Graphs An Introduction. What is a graph?  A graph is a visual representation of a relationship between, but not restricted to, two variables.  A graph.
Graphing Parameters Titles X-Axis Title Y-Axis Title Legend Scales Color Gridlines library(help="graphics") Basic Chart Types The R Graphics Package LineHistogram.
Microsoft ® Office Excel 2007 Working with Charts.
Microsoft ® Office Excel ® 2007 Training Create a chart.
An Internet of Things: People, Processes, and Products in the Spotfire Cloud Library Dr. Brand Niemann Director and Senior Data Scientist/Data Journalist.
© 2012 The McGraw-Hill Companies, Inc. All rights reserved. word 2010 Chapter 3 Formatting Documents.
Advanced GIS Using ESRI ArcGIS 9.3 3D Analyst part 2.
Computer Literacy BASICS: A Comprehensive Guide to IC 3, 5 th Edition Lesson 19 Organizing and Enhancing Worksheets 1 Morrison / Wells / Ruffolo.
CTS130 Spreadsheet Lesson 9 - Building Charts. What is a Chart? A chart is a visual display of information in a worksheet. Charts can help you make comparisons,
1. Chapter 15 Creating Charts 3 Charting Data in Word A chart or graph presents data visually. A chart depicts numeric data in a graphical format. If.
McGraw-Hill Career Education© 2008 by the McGraw-Hill Companies, Inc. All Rights Reserved. Office Excel 2007 Lab 2 Charting Worksheet Data.
Outline Research Question: What determines height? Data Input Look at One Variable Compare Two Variables Children’s Height and Parents Height Children’s.
Excel Part 4 Working with Charts and Graphics. XP Objectives Create an embedded chart Work with chart titles and legends Create and format a pie chart.
Excel Part 4 Working with Charts and Graphics. XP Objectives Create an embedded chart Work with chart titles and legends Create and format a pie chart.
ENGR 1181 College of Engineering Engineering Education Innovation Center Excel Graphing.
Lesson 3: Working with Forms. Learning Objectives After studying this lesson, you will be able to: Identify form design elements Create and print forms.
Microsoft Excel 2007 Noris Bt. Ismail Faculty of Information and Communication Technology Tel : (Ext 8408) BCOMP0101.
R PROGRAMMING FOR SQL DEVELOPERS Kiran Math Developer : Proterra in Greenville SC
Microsoft Office XP Illustrated Introductory, Enhanced With Charts Working.
Adding Tables to Slides Lesson 5. Software Orientation Tables are designed to organize data in columns and rows, as shown at right. The Table Tools Design.
R PROGRAMMING FOR SQL DEVELOPERS Kiran Math Developer : Proterra in Greenville SC
Overview of R and ggplot2 for graphics
Working with Charts © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using R Graphs in R.
Adobe Flash Professional CS5 – Illustrated
ggplot2 Merrill Rudd TAs: Brooke Davis and Megsie Siple
Tutorial 4: Enhancing a Workbook with Charts and Graphs
Next Generation R tidyr, dplyr, ggplot2
Summary Statistics in R Commander
Data visualization in Python
Ggplot2 I EPID 799C Mon Sep
Microsoft Excel Illustrated
Microsoft Office Illustrated Introductory, Windows XP Edition
R Programming For Sql Developers ETL USING R
Overview of R and ggplot2 for graphics
R course 6th lecture.
Using Charts in a Presentation
Chapter 8 Using Document Collaboration and Integration Tools
Data visualization and graphic design
Presentation transcript:

ggplot2 A cool way for creating plots in R Maria Novosolov

Your scientific graphic options

Why not only Excel? Excel is relatively limited in its support of scientific graphing It’s options provide limited control over the output Limited selection of graph types Limited number of data points that can be plotted

Why ggplot2? Helps creating good looking graphs for publishing The package format allows manipulation on every step of the way Create new graphics tailored for your problem Easy to create complex plots

The idea behind ggplot2 Combining all the good and leaving out all the bad of all R packages for plotting

Some examples

Attention!! It doesn’t suggest what graphics you should use to answer the questions you are interested in

The basics of ggplot2 Works as a layer by layer system Based on the Grammar of Graphics (Wilkinson, 2005) The plots can be edited after they are ready

How it works Two options of working with the package 1. Easy – using qplot() (=quick plot) 2. Complicated – using ggplot

qplot Basically very similar to the function plot() in R The first two arguments to qplot() are x and y An optional data argument (recommended) Exp: qplot(x, y, data = mydata)

qplot You can change color, size, shape and transparency (all of this is called aesthetics) This can either be set to the default aesthetics or manually using the function I(). You can decide on the type of the plot (scatterplot, box-plot, histogram) by using “geom” function.

ggplot

Works by creating the plot layer by layer A layer is composed of four parts: data and aesthetic mapping a statistical transformation (stat) a geometric object (geom) and a position adjustment.

How it works A plot is made up of multiple layers. A layer consists of data, a set of mappings between variables and aesthetics, a geometric object and a statistical transformation. Scales control the details of the mapping. All components are independent and reusable.

Layers Iteratively update a plot Change a single feature at a time Think about the high level aspects of the plot in isolation Instead of choosing a static type of plot, create new types of plots on the fly Cure against immobility Developers can easily develop new layers without affecting other layers

Lets dive into the syntax

Anatomy of a plot 1. Data – the data name that you want to present 2. Aesthetics – the visualization of the plot 3. Geom – geometric objects. Represent what you see in the plot: points, lines, polygon etc. 4. Themes – control presentation of non- data elements 5. Stat – statistical transformations 6. Scale – intersect between the data and the aesthetics.

Anatomy of a plot 7. Coord – coordinate system. describes how data coordinates are mapped to the plane of the graphic. 8. Position adjustments 9. Facet - describes how to break up the data into subsets and how to display those subsets as small multiples.

Anatomy of a plot 1. Data * 2. Aesthetic mapping * 3. Geometric Object * 4. Statistical transformation 5. Scales 6. Facets 7. Themes 8. Position adjustments * Mandatory layers

1. Data In ggplot2, we always explicitly specify the data qplot(displ, hwy, data = mpg)

2. Aesthetic Mapping In ggplot land aesthetic means "something you can see“ Includes: Position (i.e., on the x and y axes) Color ("outside" color) Fill ("inside" color) Shape (of points) Line type Size Aesthetic mappings are set with the aes() function

3. Geometric Object A geom can only display certain aesthetics A plot must have at least one geom; there is no upper limit

Example ggplot(data=mpg, aes(x=cty, y=hwy)) + geom_point() Aesthetic Mappings The data.frame to plot What geom to use in plotting

4. Statistical Transformations Each geom has a default statistic, but these can be changed Some plot types (such as scatterplots) do not require transformations–each point is plotted at x and y coordinates equal to the original value Other plots, such as boxplots, histograms, prediction lines etc. require statistical transformations

5. Scales Control mapping from data to aesthetic attributes One scale per aesthetic

5. Scales In ggplot2 scales include: position color and fill size shape line type Modified with scale_ _

5. Scales Common Scale Arguments: name: the first argument gives the axis or legend title limits: the minimum and maximum of the scale breaks: the points along the scale where labels should appear labels: the labels that appear at each break

5. Scales Partial combination matrix of available scales

6. Faceting Lay out multiple plots on a page Split data into subsets Plot subsets into different panels

6. Faceting Faceting is ggplot2 parlance for small multiples The idea is to create separate graphs for subsets of data ggplot2 offers two functions for creating small multiples: facet_wrap() : define subsets as the levels of a single grouping variable facet_grid() : define subsets as the crossing of two grouping variables Facilitates comparison among plots, not just of geoms within a plot

Facet Types 2D grid of panels: 1D ribbon of panels wrapped into 2D:

Faceting Formula

Scales in Facets

7. Themes The ggplot2 theme system handles non-data plot elements such as Axis labels Plot background Facet label background Legend appearance Specific theme elements can be overridden using theme()

8. Position adjustments Tweak positioning of geometric objects Avoid overlaps

Exp: position_jitter Avoid overplotting by jittering points ggplot(overplotted, aes(x,y)) + geom_point(position=position_jitter (w=0.1, h=0.1))

ggplot Creating a plot -Adding data -Mapping aesthetics Adding layers* * In order to start seeing the plot you need to first declare a geom Exp: p <- ggplot(diamonds, aes(carat, price, colour = cut)) Exp: p <- p + layer(geom = "point")

Summary Allows creating high quality plots Many options Many blogs and webpages explaining how to do different plots in ggplot Disadvantage – takes time to learn the grammar There is a book: ggplot2: Elegant Graphics for Data Analysis

Useful links content/uploads/2011/09/handout_ggplot2. pdf content/uploads/2011/09/handout_ggplot2. pdf