More Complex Graphics in R Fish 552: Lecture 8. Recommended Readings How to Display Data Badly (Howard Wainer, 1984) –http://www.jstor.org/stable/2683253http://www.jstor.org/stable/2683253.

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
Advertisements

Chapter 2 Exploring Data with Graphs and Numerical Summaries
Graphics in R data analysis and visualization Katia Oleinik Scientific Computing and Visualization Boston University
Jeopardy Multiple Choice Fill in the Blank Modified T/F Vocabulary Positioning of data Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400.
Contingency Tables Chapters Seven, Sixteen, and Eighteen Chapter Seven –Definition of Contingency Tables –Basic Statistics –SPSS program (Crosstabulation)
® Microsoft Office 2010 Excel Tutorial 4: Enhancing a Workbook with Charts and Graphs.
Height of 5 different tomato plant populations from different areas tomato plants height (cm)pop1pop2pop3pop4pop
Tutorial 4: Designing a Web Page with Tables
Creating a Web Page with Tables. Objectives Create a text table with preformatted text Create the basic structure of a graphical table Organize table.
Introducing Web Tables
XP Creating Web Pages with HTML Using Tables. XP Objectives Create a text table Create a table using the,, and tags Create table headers and captions.
1 Committed to Shaping the Next Generation of IT Experts. Chapter 3 – Graphs and Charts: Delivering a Message Robert Grauer and Maryann Barber Exploring.
Using HTML Tables.
FISH 397C Winter 2009 Evan Girvetz More Complex Graphics in R © R Foundation, from
The Power of Tables They aren't just for sitting stuff on anymore...
Create a Web Site with Frames
Chapter 9 Creating Graphs in Illustrator. Objectives Create a graph Edit a graph using the Graph Data window Use the Group Selection tool Use the Graph.
1 Create a Questionnaire Learning Objective  To learn how to use the features in Microsoft Publisher to create a questionnaire.
Chapter 6 Working with Frames.
XP Tutorial 5New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Designing a Web Site with Frames Using Frames to Display Multiple Web Pages Tutorial.
The gchart Procedure The gchart Procedure is used to create bar charts of various types (it can also create pie charts. It’s most basic form would look.
Laboratory Exercise # 13 – Font and Number Format Styles Office Productivity Tools 1 Laboratory Exercise # 13 Font and Number Format Styles Objectives:
LISA Short Course Series R Basics Ana Maria Ortega Villa Fall 2013 LISA: R BasicsFall 2013.
XP 1 Tutorial 5 Using Frames in a Web Site. XP 2 Tutorial Objectives  Describe the uses of frames in a Web site  Lay out frames within a browser window.
XP Using Frames in a Web Site Ali Alfayly. XP Tutorial Objectives Create frames for a Web site Control the appearance and placement of frames Control.
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.
Exploring Excel 2003 Revised - Grauer and Barber 1 Committed to Shaping the Next Generation of IT Experts. Chapter 3 – Graphs and Charts: Delivering a.
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.
Measurements, Tables, and Graphs. Linear Measurements This is how we measure the length, width, or height of an object. Standard measurements include.
CNIT 132 – Week 7 HTML - Tables. Tables on the World Wide Web A table can be displayed on a Web page either in a text or graphical format. A text table:
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
76 © 1998, 1999, 2000 David T. Gray, Howard Duncan, Jane Kernan Frames When displaying information in a browser, it is sometimes useful to divide the display.
Tutorial 8 Designing a Web Site with Frames. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Explore the uses of frames.
Computer Information Technology – Section 4-12 Some text and examples used with permission from: Note: We not endorsing or promoting.
Tables and Graphing. Displaying Data Sometimes it is easier to read data in a visual format. This can come in the form of tables, graphs, charts, etc.
CIS234A- Lecture 7 Instructor Greg D’Andrea. Tables A table can be displayed on a Web page either in a text or graphical format. A text table: – contains.
Learn R Toolkit D Kelly O'DayBox, Dot, Histogram, Strip Charts Mod 5 –Box Charts: 1 Module 5 Box, Dot, Histogram, Strip Charts Do See & HearRead Learn.
Creating a poster is easier than you think.
June 21, Objectives  Enable the Data Analysis Add-In  Quickly calculate descriptive statistics using the Data Analysis Add-In  Create a histogram.
Creating Graphs in Illustrator
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.
Excel 2007 Part (3) Dr. Susan Al Naqshbandi
Plotting Complex Figures Using R
100 Metros Analysis User’s Guide Atlanta Regional Commission For more information contact:
MSOffice EXCEL 1 Part 2 ® Microsoft® Office 2010: Illustrated Introductory.
1 Lecture 5 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
Tutorial 4 Creating Page Layouts with CSS
Displaying Data  Data: Categorical and Numerical  Dot Plots  Stem and Leaf Plots  Back-to-Back Stem and Leaf Plots  Grouped Frequency Tables  Histograms.
HTML Frames. Advantages to Using Frames n flexibility in design n information in different Web pages n remove redundancy. n site easier to manage. n update.
20-753: Fundamentals of Web Programming 1 Lecture 6: Advanced HTML Fundamentals of Web Programming Lecture 6: Advanced HTML.
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",
Data & Graphing vectors data frames importing data contingency tables barplots 18 September 2014 Sherubtse Training.
Introduction to plotting data Fish 552: Lecture 4.
1 Berger Jean-Baptiste
Review > head(tripData) > table(speciesData$SpeciesCode) > grep("a", c("aa","ab","bb")) > c(2,3,8) %in% c(1,2,3,5,7,9) > bocTrip
Descriptive Statistics using R. Summary Commands An essential starting point with any set of data is to get an overview of what you are dealing with You.
Statistics Unit Test Review Chapters 11 & /11-2 Mean(average): the sum of the data divided by the number of pieces of data Median: the value appearing.
Review > mean(humidity, na.rm=T) > humidity[!is.na(humidity)] > X x Y Y[[1]][2,3] > zone.fac
Working with data in R 2 Fish 552: Lecture 3. Recommended Reading An Introduction to R (R Development Core Team) –
Using R Graphs in R.
AP Biology: Normal Distribution
Objectives Create a reset style sheet Explore page layout designs
Introduction to Exploratory Descriptive Data Analysis in S-Plus II
CHAPTER 1 Exploring Data
Exercise 39 - Skills A table on your Web page provides one of the best ways to organize and align graphics, text and other objects on the page. You can.
Using HTML Tables SWBAT: - create tables using HTML
exploring Microsoft Office 2013 Plus
Presentation transcript:

More Complex Graphics in R Fish 552: Lecture 8

Recommended Readings How to Display Data Badly (Howard Wainer, 1984) – Fish 507H Beautiful Graphics in R (Trevor Branch) –

Outline The aim of this lecture is to explore more of the options in par in order to create more informative plots and introduce other high-level plotting routines in R –Working with multiple layouts –Box plots –Bar plots

Possum Data The possum data frame consists of nine morphometric measurements on each of 104 mountain brushtail possums, trapped at seven sites from Southern Victoria to central Queensland Australia –Data comes from DAAG package in R –Available on class website > possum <- read.csv(" > head(possum, n=3) case site Pop sex age hdlngth skullw totlngth taill... C3 1 1 Vic m C5 2 1 Vic f C Vic f

Possum Data Case - observation number Site - one of seven locations where possums were trapped Pop - a factor which classifies the sites as Vic Victoria, other New South Wales or Queensland Sex - a factor with levels f female, m male Age - age Hdlngth - head length Skullw - skull width Totlngth - total length Taill - tail length Footlgth - foot length Earconch - ear conch length Eye - distance from medial canthus to lateral canthus of right eye Chest - chest girth (in cm) Belly - belly girth (in cm)

Multiple graphs It’s often useful to put multiple graphs on the same active R Graphics device This is achieved with mfrow / mfcol specifications in par –Takes as input a vector specifying the number of rows and number of columns: c(nr, nc) –mfrow : multiple plots on one graph filled by row –mfcol : same but by column This command needs to be given prior to making the plots

Default way to make 3 plots (only specify mfrow)

Changing the margins When multiple graphs are laid out space can be optimized by modifying the default margins par(mar=c(bottom, left, top, right)) –Default is c(5, 4, 4, 2) Note that if these specifications are not chosen carefully each individual plot may not be the same size making plot comparisons on the same scale misleading

Make use of mar

What’s sub-optimal about this plot?

Hands-on Exercise 1 As a plotting review, make changes to the previous plot based on our discussion and the contributed suggestions. Some of the changes I had in mind were: –Redundant labeling and spacing –Optimize space –Putting the plots on the same scale

Making customized layouts The layout() function provides an alternative to the mfrow and mfcol settings. The primary difference is that the layout() function allows the creation of multiple figure regions of unequal sizes and is much more flexible The first argument (and the only required argument) to the layout() function is a matrix. The number of rows and columns in the matrix determines the number of rows and columns in the layout. The contents of the matrix are integer values that determine which rows and columns each figure will occupy.

layout() layout(mat, widths = rep(1, ncol(mat)), heights = rep(1, nrow(mat)), respect = FALSE) mat a matrix object specifying the location of the next N figures on the output device. Each value in the matrix must be 0 or a positive integer. If N is the largest positive integer in the matrix, then the integers {1,...,N-1} must also appear at least once in the matrix. widths a vector of values for the widths of columns on the device. Relative widths are specified with numeric values. Absolute widths (in centimetres) are specified with the lcm() function (see examples). heights a vector of values for the heights of rows on the device. Relative and absolute heights can be specified, see widths above. respect argument controls whether a unit column-width is the same physical measurement on the device as a unit row-height

Understanding layout() First create a matrix with numbers for each location to plot > ( lay1.mat <- matrix(c(1,1,0,2), 2, 2, byrow = TRUE) ) [,1] [,2] [1,] 1 1 [2,] 0 2 Assign and show the layout > lay1 <- layout(mat = lay1.mat) > layout.show(lay1)

Understanding layout() > ( lay2.mat <- matrix(c(1,1,3,2), 2, 2, byrow = TRUE) ) [,1] [,2] [1,] 1 1 [2,] 3 2 > lay2 <- layout(mat = lay2.mat) > layout.show(lay2)

Understanding layout() > ( lay3.mat <- matrix(c(1,1,1,1,2,3), nrow=3, byrow = TRUE) ) [,1] [,2] [1,] 1 1 [2,] 1 1 [3,] 2 3 > lay3 <- layout(mat = lay3.mat) > layout.show(lay3) plot(possum$hdlngth ~ possum$totlngth) hist(possum$hdlngth) hist(possum$totlngth)

Understanding layout() In this example we will make a more custom layout > ( lay4.mat <- matrix(c(2,0,1,3),nrow = 2,byrow=TRUE) ) [,1] [,2] [1,] 2 0 [2,] 1 3 > lay4 <- layout(lay4.mat, c(3,1), c(1,3), respect=TRUE) > layout.show(lay4) Note the use of widths/heights to control the size of the regions

Putting it all together Design the layout –Note that the width / heights argument is now specified lay4.mat <- matrix(c(2,0,1,3),nrow = 2,byrow = TRUE) lay4 <- layout(lay4.mat, c(3,1), c(1,3), respect = TRUE) layout.show(lay4) Get histogram counts to use in barplot() hdlngth.hist <- hist(possum$hdlngth, plot = FALSE) totlngth.hist <- hist(possum$totlngth, plot = FALSE) Don’t plot anything

Putting it all together Specify margins and plot the figures –Note use of horizontal = TRUE par(mar = c(3,2.5,1,1)) plot(possum$hdlngth ~ possum$totlngth, main = "") par(mar = c(0,3,1,1)) barplot(totlngth.hist$counts) par(mar = c(3,0,1,1)) barplot(hdlngth.hist$counts, horiz = TRUE)

Box plots Normally boxplots expect a vector for the response and another vector for a factor describing how to organize the response boxplot(FL ~ sex, data = crabs) What if your data isn’t in this form?

Habitat data > habitat = read.csv(" > head(habitat) Animal Cropland Grassland Shrub_Scrub Percent 1 ZIP OSC_b OSC_b OSC_b OSC_b OSC_b

Organize data in format required > area = c(habitat$Cropland, habitat$Grassland, habitat$Shrub_Scrub) > type = factor( rep( names(habitat)[2:4], each = nrow(habitat) ) ) Combine columns into a single response vector Repeat column names for the number of rows This gives us a vector with the name of the column each observation came from

Create data frame > habitat = data.frame(area, type) > head(habitat) area type Cropland Cropland Cropland Cropland Cropland Cropland Now we have a new data frame with 3 times the rows

boxplot(area ~ type, data = habitat, ylab = "area") What if we didn’t want to plot the 0 values?

boxplot(area ~ type, data = habitat[habitat$area != 0,], ylab = "area") Select the data where area isn’t 0

Boxplots with continuous data > head(airquality) Ozone Solar.R Wind Temp Month Day NA NA NA

plot(Ozone ~ Temp, data = airquality, xlab = "Temperature", ylab = "Ozone")

plot(Ozone ~ cut(Temp, breaks = 4), data = airquality, xlab = "Temperature", ylab = "Ozone") Create factor from continuous data Bin sizes

Plotting multiple box plots boxplot(len ~ dose, data = ToothGrowth, boxwex = 0.25, at = 1: , subset = (supp == "VC"), col = "yellow", main="Guinea Pigs' Tooth Growth", xlab="Vitamin C dose mg", ylab="tooth length", ylim = c(0,35)) boxplot(len ~ dose, data = ToothGrowth, add = TRUE, boxwex = 0.25, at = 1: , subset = (supp == "OJ"), col = "orange") legend("topleft", legend = c("Ascorbic acid", "Orange juice"), col = "black", pt.bg = c("yellow", "orange"), pch = 22) Add to same plot Specify which to plot Don’t overlap

Barplots Expect data to be in a vector or a table Row names and/or column names are used by default (or you can specify) > VADeaths Rural Male Rural Female Urban Male Urban Female

barplot(VADeaths, legend = TRUE, ylab = "Death rate per 1000") barplot(VADeaths, legend = TRUE, ylab = "Death rate per 1000", beside = TRUE)

barplot(t(VADeaths),..., args.legend = list( x = "topleft")) barplot(t(VADeaths),..., args.legend = list( x = ”bottomright"), horiz = TRUE) Transpose the data

barplot(VADeaths[,3], ylab = "Death rate per 1000", col = "azure", main = "Virginia rual female death rate by age") Color can also be a vector Plot single vector

What if data isn’t in table? Want to plot: # days good/moderate/unhealthy by month Data: ozone values for each day Ozone Solar.R Wind Temp Month Day How do we get data in right format? Ozone (ppb)Index Good Moderate > 75Unhealthy

Table of ozone data ozoneIndex <- cut(airquality$Ozone, breaks = c(0, 59, 75, Inf), labels = c("Good", "Moderate", "Unhealthy")) ( ozoneIndexByMonth <- table(ozoneIndex, airquality$Month) ) ozoneIndex Good Moderate Unhealthy

barplot(ozoneIndexByMonth, ylab = "# days", beside = TRUE, legend = TRUE, args.legend = list(x = "top"), names.arg = c("May", "June", "July", "August", "September"), main = "New York Ozone Index 1973")

Hands-on Exercise 2 Create a box plot comparing sepal and petal lengths and widths from the iris data for all species combined Create a bar plot comparing the ozone index on days that are warm and cool using the airquality data –Use the median to define warm and cool days