Plotting Systems in R Dr. Jieh-Shan George YEH

Slides:



Advertisements
Similar presentations
Introduction to Lattice Graphics Richard Pugh 4th December 2012.
Advertisements

Lab # 03- SS Basic Graphic Commands. Lab Objectives: To understand M-files principle. To plot multiple plots on a single graph. To use different parameters.
Working with Tables for Page Design – Lesson 41 Working with Tables for Page Design Lesson 4.
Introduction to R Graphics
THE PROFESSIONAL APPROACH SERIES © 2008 The McGraw-Hill Companies, Inc. All rights reserved. 1 Lesson Objectives Lesson 5 objectives Use a template to.
Plotting with ggplot2: Part 1
Customizing Graphs Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces.
CS320n –Visual Programming LabVIEW Foundations. Visual ProgrammingLabVIEW Foundations2 What We Will Do Today Hand back and review the midterm Look at.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 3 1 Microsoft Office Excel 2003 Tutorial 3 – Developing a Professional- Looking.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Excel 2003 Lab 2 Charting Worksheet Data.
Data Tutorial Tutorial on Types of Graphs Used for Data Analysis, Along with How to Enter Them in MS Excel Carryn Bellomo University of Nevada, Las Vegas.
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 12 Spreadsheets and Business Graphics: Facts and Figures.
SPSS Statistical Package for the Social Sciences is a statistical analysis and data management software package. SPSS can take data from almost any type.
InDesign CS3 Lesson 3 Working with Frames. Using Frames Frames are containers in which you place graphics or text. Frames can also be used as graphic.
Baburao Kamble (Ph.D) University of Nebraska-Lincoln Data Analysis Using R Week5: Charts/Plots in R.
CHAPTER 14 Formatting a Workbook Part 1. Learning Objectives Format text, numbers, dates, and time Format cells and ranges CMPTR Chapter 14: Formatting.
8 Copyright © 2004, Oracle. All rights reserved. Creating LOVs and Editors.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
ITP 104.  While you can do things like this:  Better to use styles instead:
AN INTRODUCTION TO GRAPHICS IN R. Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level.
CO1552 – Web Application Development Lists, Special Characters, and Tables.
XP New Perspectives on Microsoft Access 2002 Tutorial 51 Microsoft Access 2002 Tutorial 5 – Enhancing a Table’s Design, and Creating Advanced Queries and.
ENG College of Engineering Engineering Education Innovation Center 1 2D Plots 1 in MATLAB Topics Covered: 1.Plotting basic 2-D plots The plot()
R-Graphics Day 2 Stephen Opiyo. Basic Graphs One of the main reasons data analysts turn to R is for its strong graphic capabilities. R generates publication-ready.
Creating Graphs in R Graphing techniques Sharon style! (I am not sure if this is a good or bad thing but this is what I do...)
Computers: Tools for an Information Age Chapter 12 Spreadsheets and Business Graphics: Facts and Figures.
Microsoft Excel Spreadsheet Review. Templates  Templates can be produced for the following elements:  Text and Graphics  Formatting Information – Layouts,
Microsoft Office Excel 2003 Tutorial 3 – Developing a Professional-Looking Worksheet.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® Excel 2010 © 2011 The McGraw-Hill Companies,
Graphing Guidelines  Often the goal of an experiment is to find the relationship between two variables.  As one variable changes, so does the other.
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
Microsoft ® Office Excel 2007 Working with Charts.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 15 Advanced Tables.
Advanced Stata Workshop FHSS Research Support Center.
Return to Outline Copyright © 2011 by Maribeth H. Price 3-1 Labeling and annotation.
DAY 6: MICROSOFT EXCEL – CHAPTER 3 Sravanthi Lakkimsetty September 2, 2015.
1 Guide to Oracle10G CHAPTER 7: Creating Database Reports 7.
R-Graphics Stephen Opiyo. Basic Graphs One of the main reasons data analysts turn to R is for its strong graphic capabilities. R generates publication-ready.
PROC GPLOT GPLOT is used to make two dimensional scatter-plots. General Syntax: proc gplot data=data-set options; plot y-variable*x-variable/options; run;
CRSD Technology Training Tony Judice. Quick Access Toolbar – can be modifiedSave as… allows you to save the file to a different location and also as an.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Recap Chapter 5 “Plotting” Two Dimensional Plots Simple x-y Plots Titles, Labels and Grids Multiple Plots.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
Ggplot2 A cool way for creating plots in R Maria Novosolov.
Plotting Complex Figures Using R
Lesson 6 Formatting Cells and Ranges. Objectives:  Insert and delete cells  Manually format cell contents  Copy cell formatting with the Format Painter.
Creating Tables LESSON 6 - #1.06 ESSENTIAL STANDARD #1 - UNDERSTAND WORD PROCESSING INDICATOR #6 – CREATING TABLES.
Unit 3: Text, Fields & Tables DT2510: Advanced CAD Methods.
Positioning Objects with CSS and Tables
Lattice- xyplot Yufeng Lin Haipeng Yao. How to use xyplot Xyplot(formula, data = parent.frame(), panel = if (is.null(groups)) "panel.xyplot" else "panel.superpose",
XP New Perspectives on Macromedia Dreamweaver MX 2004 Tutorial 5 1 Adding Shared Site Elements.
Introduction to plotting data Fish 552: Lecture 4.
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Boxes. boxes- learning targets o I will be able to display buttons (boxes) o I will be able to organize boxes o I will be able to create an animation.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
CSS Box Model.
CSS Box Model.
Using R Graphs in R.
Flash Interface, Commands and Functions
Lecture 25: Exploring data
Two-Dimensional Plots
Unit Six: Labels In this unit… Review Adding Text to Maps
MATLAB How to use (using M-files) Double click this icon
CSS Box Model.
MATLAB How to use (using M-files) Double click this icon
Formatting a Workbook Part 1
MATLAB How to use (using M-files)
Plotting Signals in MATLAB
CSS Box Model.
Presentation transcript:

Plotting Systems in R Dr. Jieh-Shan George YEH

The Base Plotting System "Artist's palette" model Start with blank canvas and build up from there Start with plot function (or similar) Use annotation functions to add/modify (text, lines, points, axis)

The Base Plotting System Convenient, mirrors how we think of building plots and analyzing data Can’t go back once plot has started (i.e. to adjust margins); need to plan in advance Difficult to "translate" to others once a new plot has been created (no graphical "language") Plot is just a series of R commands

Base Plot library(datasets) data(cars) with(cars, plot(speed, dist))

The Lattice System Plots are created with a single function call (xyplot, bwplot, etc.) Most useful for conditioning types of plots: Looking at how y changes with x across levels of z Things like margins/spacing set automatically because entire plot is specified at once Good for putting many many plots on a screen

The Lattice System Sometimes awkward to specify an entire plot in a single function call Annotation in plot is not especially intuitive Use of panel functions and subscripts difficult to wield and requires intense preparation Cannot "add" to the plot once it is created

Lattice Plot library(lattice) state <- data.frame(state.x77, region = state.region) xyplot(Life.Exp ~ Income | region, data = state, layout = c(4, 1))

The ggplot2 System Splits the difference between base and lattice in a number of ways Automatically deals with spacings, text, titles but also allows you to annotate by "adding" to a plot Superficial similarity to lattice but generally easier/more intuitive to use Default mode makes many choices for you (but you can still customize to your heart's desire)

ggplot2 Plot library(ggplot2) data(mpg) qplot(displ, hwy, data = mpg)

THE PROCESS OF MAKING A PLOT

The Process of Making a Plot What graphics system will you use: base, lattice, or ggplot2? These generally cannot be mixed. Base graphics are usually constructed piecemeal, with each aspect of the plot handled separately through a series of function calls; this is often conceptually simpler and allows plotting to mirror the thought process Lattice graphics are usually created in a single function call, so all of the graphics parameters have to specified at once; specifying everything at once allows R to automatically calculate the necessary spacings and font sizes. ggplot2 combines concepts from both base and lattice graphics but uses an independent implementation 11

Principles of Analytic Graphics Principle 1: show comparisons Principle 2: show causality, mechanism, explanation Principle 3: Show multivariate data Principle 4: Integrate multiple modes of evidence Principle 5: Describe and document the evidence Principle 6: Content is king 12

BASE GRAPHICS 13

Base Graphics Base graphics are used most commonly and are a very powerful system for creating 2-D graphics. There are two phases to creating a base plot – Initializing a new plot – Annotating (adding to) an existing plot Calling plot(x, y) or hist(x) will launch a graphics device (if one is not already open) and draw a new plot on the device If the arguments to plot are not of some special class, then the default method for plot is called; this function has many arguments, letting you set the title, x axis label, y axis label, etc. The base graphics system has many parameters that can set and tweaked; these parameters are documented in ?par; it wouldn’t hurt to try to memorize this help page! 14

Simple Base Graphics: Histogram library(datasets) hist(airquality$Ozone) ## Draw a new plot 15

Simple Base Graphics: Scatterplot library(datasets) with(airquality, plot(Wind, Ozone)) 16

Simple Base Graphics: Boxplot library(datasets) airquality <- transform(airquality, Month = factor(Month)) boxplot(Ozone ~ Month, airquality, xlab = "Month", ylab = "Ozone (ppb)") 17

Some Important Base Graphics Parameters Many base plotting functions share a set of parameters. Here are a few key ones: – pch: the plotting symbol (default is open circle) – lty: the line type (default is solid line), can be dashed, dotted, etc. – lwd: the line width, specified as an integer multiple – col: the plotting color, specified as a number, string, or hex code; the colors() function gives you a vector of colors by name – xlab: character string for the x-axis label – ylab: character string for the y-axis label 18

Some Important Base Graphics Parameters The par() function is used to specify global graphics parameters that affect all plots in an R session. These parameters can be overridden when specified as arguments to specific plotting functions. – las: the orientation of the axis labels on the plot – bg: the background color – mar: the margin size – oma: the outer margin size (default is 0 for all sides) – mfrow: number of plots per row, column (plots are filled row-wise) – mfcol: number of plots per row, column (plots are filled column-wise) 19

Some Important Base Graphics Parameters > par("lty") [1] "solid" > par("col") [1] "black" > par("pch") [1] 1 > par("bg") [1] "white" > par("mar") [1] > par("mfrow") [1]

Base Plotting Functions plot: make a scatterplot, or other type of plot depending on the class of the object being plotted lines: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots points: add points to a plot text: add text labels to a plot using specified x, y coordinates title: add annotations to x, y axis labels, title, subtitle, outer margin mtext: add arbitrary text to the margins (inner or outer) of the plot axis: adding axis ticks/labels 21

Base Plot with Annotation with(airquality, plot(Wind, Ozone, main = "Ozone and Wind in New York City")) with(subset(airquality, Month == 5), points(Wind, Ozone, col = "blue")) 22

Base Plot with Annotation with(airquality, plot(Wind, Ozone, main = “Ozone and Wind in New York City”, type = "n")) with(subset(airquality, Month == 5), points(Wind, Ozone, col = "blue")) with(subset(airquality, Month != 5), points(Wind, Ozone, col = "red")) legend("topright", pch = 1, col = c("blue", "red"), legend = c("May", "Other Months")) 23

Multiple Base Plots par(mfrow = c(1, 2)) with(airquality, { plot(Wind, Ozone, main = "Ozone and Wind") plot(Solar.R, Ozone, main = "Ozone and Solar Radiation") }) 24

Multiple Base Plots par(mfrow = c(1, 3), mar = c(4, 4, 2, 1), oma = c(0, 0, 2, 0)) with(airquality, { plot(Wind, Ozone, main = "Ozone and Wind") plot(Solar.R, Ozone, main = "Ozone and Solar Radiation") plot(Temp, Ozone, main = "Ozone and Temperature") mtext("Ozone and Weather in New York City", outer = TRUE) }) 25

LATTICE PLOTTING SYSTEM 26

Lattice Plotting System The lattice plotting system is implemented using the following packages: lattice: contains code for producing Trellis graphics, which are independent of the “base” graphics system; includes functions like xyplot, bwplot, levelplot grid: implements a different graphing system independent of the “base” system; the lattice package builds on top of grid – We seldom call functions from the grid package directly The lattice plotting system does not have a “two-phase” aspect with separate plotting and annotation like in base plotting All plotting/annotation is done at once with a single function call 27

Lattice Functions xyplot: this is the main function for creating scatterplots bwplot: box-and-whiskers plots (“boxplots”) histogram: histograms stripplot: like a boxplot but with actual points dotplot: plot dots on "violin strings" splom: scatterplot matrix; like pairs in base plotting system levelplot, contourplot: for plotting "image" data 28

Lattice Functions Lattice functions generally take a formula for their first argument, usually of the form xyplot(y ~ x | f * g, data) We use the formula notation here, hence the ~. On the left of the ~ is the y-axis variable, on the right is the x-axis variable f and g are conditioning variables — they are optional – the * indicates an interaction between two variables The second argument is the data frame or list from which the variables in the formula should be looked up – If no other arguments are passed, there are defaults that can be used. – If no data frame or list is passed, then the parent frame is used. 29

Simple Lattice Plot library(datasets) library(lattice) ## Convert 'Month' to a factor variable airquality <- transform(airquality, Month = factor(Month)) xyplot(Ozone ~ Wind | Month, data = airquality, layout = c(5, 1)) 30

Lattice Behavior Lattice functions behave differently from base graphics functions in one critical way. Base graphics functions plot data directly to the graphics device (screen, PDF file, etc.) Lattice graphics functions return an object of class trellis The print methods for lattice functions actually do the work of plotting the data on the graphics device. Lattice functions return "plot objects" that can, in principle, be stored (but it’s usually better to just save the code + data). On the command line, trellis objects are auto-printed so that it appears the function is plotting the data 31

Lattice Behavior p <- xyplot(Ozone ~ Wind, data = airquality) ## Nothing happens! print(p) ## Plot appears xyplot(Ozone ~ Wind, data = airquality) ## Auto-printing 32

Lattice Panel Functions Lattice functions have a panel function which controls what happens inside each panel of the plot. The lattice package comes with default panel functions, but you can supply your own if you want to customize what happens in each panel Panel functions receive the x/y coordinates of the data points in their panel (along with any optional arguments) 33

Lattice Panel Functions set.seed(10) x <- rnorm(100) f <- rep(0:1, each = 50) y <- x + f - f * x + rnorm(100, sd = 0.5) f <- factor(f, labels = c("Group 1", "Group 2")) xyplot(y ~ x | f, layout = c(2, 1)) ## Plot with 2 panels 34

Lattice Panel Functions ## Custom panel function xyplot(y ~ x | f, layout = c(2, 1), panel = function(x, y,...) { panel.xyplot(x, y,...) ## First call the default panel function for 'xyplot' panel.abline(h = median(y), lty = 2) ## Add a horizontal line at the median }) 35

Lattice Panel Functions: Regression line ## Custom panel function xyplot(y ~ x | f, layout = c(2, 1), panel = function(x, y,...) { panel.xyplot(x, y,...) ## First call default panel function panel.lmline(x, y, col = 2) ## Overlay a simple linear regression line }) 36

INPUT/OUTPUT GRAPHS

Input/Output By default, launching R starts an interactive session with input from the keyboard and output to the screen. However, you can have input come from a script file (a file containing R commands) and direct output to a variety of destinations.

Input The source( ) function runs a script in the current session. If the filename does not include a path, the file is taken from the current working directory. # input a script source("myfile")

Output The sink( ) function defines the direction of the output. # direct output to a file sink("myfile", append=FALSE, split=FALSE) …. # return output to the terminal sink()

Graphs output FunctionOutput to pdf("mygraph.pdf")pdf file win.metafile("mygraph.wmf")windows metafile png("mygraph.png")png file jpeg("mygraph.jpg")jpeg file bmp("mygraph.bmp")bmp file postscript("mygraph.ps")postscript file sink( ) will not redirect graphic output. To redirect graphic output use one of the following functions. Use dev.off( ) to return output to the terminal. Use a full path in the file name to save the graph outside of the current working directory. # example - output graph to jpeg file jpeg("c:/mygraphs/myplot.jpg") plot(x) dev.off()

More Example png(filename = "plot1.png", width = 480, height = 480, units = "px", pointsize = 12, bg = "white", res = NA, family = "", restoreConsole = TRUE, type = c("windows", "cairo", "cairo-png")) hist(df2$Global_active_power, col="red", main="Global Active Power", xlab="Global Active Power(Kilowatts)") dev.off()