Review > mean(humidity, na.rm=T) > humidity[!is.na(humidity)] > X <- matrix(...) > x <- array(...) > Y <- list(X, x) > Y[[1]][2,3] > zone.fac <- factor(zone,

Slides:



Advertisements
Similar presentations
Chapter 3 – Web Design Tables & Page Layout
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.
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Introduction to R Graphics
® Microsoft Office 2010 Excel Tutorial 4: Enhancing a Workbook with Charts and Graphs.
MA/CS375 Fall MA/CS 375 Fall 2002 Lecture 4.
FIRST COURSE Excel Tutorial 4 Working with Charts and Graphics.
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.
Microsoft Excel 2010 Chapter 7
Evan Girvetz Winkenwerder Introduction to Graphics in R © R Foundation, from
Template for a 48x36 poster presentation Your name and the names of the people who have contributed to this presentation go here. The names and addresses.
MATLAB - Lecture 22A Two Dimensional Plots / Chapter 5 Topics Covered:
COMPREHENSIVE Excel Tutorial 4 Working with Charts and Graphics.
1 Computing for Todays Lecture 10 Yumei Huo Fall 2006.
Using Charts in a Presentation Lesson 6. Software Orientation Charts can help your audience understand relationships among numerical values. The figure.
NU Data Excel Orientation Graphing of Screening Data and Basic Graphing Functions.
An introduction to Plotting in MATLAB Rikard Johansson Department of Biomedical Engineering (IMT) Linköping University
Baburao Kamble (Ph.D) University of Nebraska-Lincoln Data Analysis Using R Week5: Charts/Plots in R.
Instructor: Professor Cora Martinez, PhD Department of Civil and Environmental Engineering Florida International University.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
An Introduction to R graphics Elizabeth Garrett-Mayer Slides borrowed from Cody Chiuzan & Delia Voronca March 24, 2014.
AN INTRODUCTION TO GRAPHICS IN R. Today Overview Overview –Gallery of R Graph examples High-Level Plotting Functions High-Level Plotting Functions Low-Level.
POSTER TEMPLATE BY: Professional Template for a 48x36 poster presentation Your name and the names of the people who have contributed.
PLOTS AND FIGURES DAVID COOPER SUMMER Plots One of the primary uses for MATLAB is to be able to create publication quality figures from you data.
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...)
Data, graphics, and programming in R 28.1, 30.1, Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen.
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 introduction to SPSS Ⅱ.Tables and Graphs for one variable ---Descriptive Statistics & Graphs.
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
Microsoft ® Office Excel 2007 Working with Charts.
MAE 1202: AEROSPACE PRACTICUM An Introduction to MATLAB: Part 2 Mechanical and Aerospace Engineering Department Florida Institute of Technology Developed.
© 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Creating Charts Lecture 19.
Microsoft Access 2010 Chapter 8 Advanced Form Techniques.
HS115 Unit 7 Seminar December 22, 2010 David Rudnick, Instructor.
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.
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.
Graphing Tutorial William Hornick CS 101. Overview You will be given a brief description, example, and “how to create” for each of the following: You.
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.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
R (3) Introduction to Graphics. The main guide R in Action Data Analysis and Graphics with R Robert I. Kabacoff
Scientific Computing (w1) R Computing Workshops An Introduction to Scientific Computing workshop 1.
Plotting Complex Figures Using R
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Progress and Outcome Measures - Part 3 Progress and Outcome Measures Part 3, Slide 1Copyright © 2004, Jim Schwab, University of Texas at Austin.
Introduction to Engineering MATLAB – 9 Plotting - 2 Agenda Formatting plots.
S-PLUS Lecture 6 Jaeyong Lee. Graphical Parameters type = “c”: c =p (default), l, b,s,o,h,n. pch=“+” : character or numbers 1 – 18 lty=1 : numbers lwd=2.
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.
POSTER TEMPLATE BY: Professional Template for a 36x24 poster presentation Your name and the names of the people who have contributed.
Introduction to plotting data Fish 552: Lecture 4.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
Review > x[-c(1,4,6)] > Y[1:3,2:8] > island.data fishData$weight[1] > fishData[fishData$weight < 20 & fishData$condition.
Review > plot(x=primates$Bodywt, y=primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), cex=2, pch=21, col="black", bg="salmon", xlab = "Body weight (kg)",
Lesson 1 Notes.  A chart is a representation of worksheet data. A chart can enhance and simplify the of numerical data in a worksheet because the between.
Excel Working with Charts and Graphs
Professional Template for a 48x36 poster presentation
Excel Part 4 Working with Charts and Graphics
Tutorial 4: Enhancing a Workbook with Charts and Graphs
Excel Part 4 Working with Charts and Graphics
Trevor A. Branch FISH 552 Introduction to R
MATLAB How to use (using M-files) Double click this icon
Microsoft Office Illustrated Introductory, Windows XP Edition
MATLAB How to use (using M-files) Double click this icon
CSc4730/6730 Scientific Visualization
MATLAB How to use (using M-files)
Plotting Signals in MATLAB
Introduction to R plot Instructor: Li, Han
Excel Part 4 Working with Charts and Graphics
Presentation transcript:

Review > mean(humidity, na.rm=T) > humidity[!is.na(humidity)] > X <- matrix(...) > x <- array(...) > Y <- list(X, x) > Y[[1]][2,3] > zone.fac <- factor(zone, labels=c("demersal", "pelagic")) > read.table(file="dat_df1.dat", header=T) Removing NAs Extract non-NAs Create a matrix Combine objects into a list Creating a factor Create an array Row 2, column 3, element 1 of list Y Creating a new project Reading in data from a file with headers above each column

Reading in as text not factors Most often I am using a database where I want the text to be text, and not factors > x <- read.csv(file="Data/dat_df1.csv", header=T) > x$sex [1] M F F M F M M F F M Levels: F M > x <- read.csv(file="Data/dat_df1.csv", header=T, + stringsAsFactors=F) > x$sex [1] "M" "F" "F" "M" "F" "M" "M" "F" "F" "M" stringsAsFactors controls how text is read in

Lecture 4 Plotting data Trevor A. Branch FISH 552 Introduction to R

Recommended reading R graphics 2 nd Edition (Paul Murrell, 2011) – Chapters 1 and 2 – Pdf of the 1 st edition here: – – R code available for all plots in 2 nd edition: –

Graphics in R: overview There are several distinct ways of doing graphics in R – base graphics (changes to layout are fairly easy, highly modifiable) – lattice (used less now) – ggplot2 (good for multipanel plots, quick alternative views of data, changes to basic layout can be difficult) I almost exclusively use base graphics, and will teach only this here In FISH 554 Beautiful Graphics in R (Winter) I teach how to make complex and beautiful figures

Base graphics: plot() Plot is the generic function for plotting R objects – points, lines, etc. Read in the "primates.csv" data > primates <- read.csv(file="primates.csv", header=T) > primates X Bodywt Brainwt 1 Potar monkey Gorilla Human Rhesus monkey Chimp

Three ways to plot > plot(x = primates$Bodywt, y = primates$Brainwt) > plot(Brainwt ~ Bodywt, data = primates) > attach(primates) > plot(x = Brainwt, y = Bodywt) > detach(primates) My preferred way Sometimes used Confusing: in the second line, where did object Brainwt come from?

To be frank, this plot is a little boring!

Labels on the axes By default R uses the name of the variables as axis labels Use the xlab and ylab options to change the labels plot(x = primates$Bodywt, y = primates$Brainwt, xlab = "Body weight (kg)", ylab = "Brain weight (g)")

Limits of the axes By default, R chooses x and y limits that are just larger (4%) than the range of your data May or may not include zero To change the default x and y values use xlim and ylim plot(x = primates$Bodywt, y = primates$Brainwt, xlim=c(0,300), ylim=c(0,1400))

Remove space around zero My pet peeve: R adds space between the axis and 0 This makes true zeros look like they are non-zeros To remove this, use xaxs="i" and yaxs="i" together with xlim and ylim plot(x = primates$Bodywt, y = primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), xaxs="i", yaxs="i")

Using colors in R Colors of points, lines, text etc. can all be specified Colors can be applied to various plot parts – col (default color) – col.axis (tick mark labels) – col.lab (x label and y label) – col.main (title of the plot) Colors can be specified as numbers or text strings – col=1 or col="red" plot(x = primates$Bodywt, y = primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), xaxs="i", yaxs="i", col="blue")

In-class exercise 1 Copy and paste the following command into your R script plot(x = primates$Bodywt, y = primates$Brainwt, xlim = c(0,300), ylim = c(0,1400), cex = 2, pch = 19, col = "blue") Experiment with different color names: col="red" Try different color numbers: col=1, col=2 Try a vector of color numbers: col=c(2,4) Experiment with changing the values for cex and pch

Controlling point characteristics Default is an open circle ( pch=1 ) of size 1 ( cex=1 ) – pch short for plotting character – cex short for character expansion Source: R Reference Card 2.0

Color naming There are 657 named colors colors() point.colors <- c("red", "orange", "green", "blue", "magenta") plot(x = primates$Bodywt, y = primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), cex=2, pch=19, col=point.colors)

R color chart: keep handy FISH 554 Beautiful Graphics in R: custom palettes, translucent colors, RGB or HSV colors, color blindness, divergent color schemes, hexadecimal, Color Brewer, etc.

Useful options for points Useful code and options under ?points Use pch=21 for filled circles, for example: – Specify circle color with col – Specify fill color with bg plot(x = primates$Bodywt, y = primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), cex=2, pch=21, col="black", bg="salmon")

Full list of plot parameters: par() If you ask for help on the plot() command using ?plot, only a handful of commands are listed There are numerous extra commands listed under ?par that can be added to all plotting commands, not just plot() Using par() by itself applies commands to multiple graphs (avoid!) > par() $xlog [1] FALSE...

Using par() for global changes avoid this whenever possible Save default par values old.par <- par() Change to a new value par(col.axis="red") plot(x=primates$Bodywt, y=primates$Brainwt, xlim=c(0,300), ylim=c(0,1400), cex=2, pch=21, col="black", bg="salmon") plot(1:3) Restore defaults par(old.par) plot(1:3)

To return to default plotting Selecting the Clear All command in the plotting window resets all figures and sets par() to the default values. Use this option when you have gone too far and can’t get back to a nice simple plotting screen.

Vector options for plotting Many plotting options can handle vectors, each element applies to one point Vectors are recycled if you supply too few numbers Different point characters: pch=1:5 Different point letters: pch=c("a","t","c","g") Different colors: col=1:5 Different sizes: cex=1:5

First letter of each primate’s name

Circle size proportional to body weight

Adding legends Legends do not use anything in the plot Look up help on the legend function ( ?legend ), note that most options in par() can be used too legend(x="topright", legend=primates[,1], pt.bg=1:5, pch=21:25, bty="n") also "bottomleft" etc., and can use x=100, y=100 vector of text strings background color of points vector of symbol type no box type

If you want the legend to correspond to the plot, you need to specify identical symbols, sizes, and colors for the plot and the legend

Axis properties Tick mark labelling using yaxp and xaxp – c(min, max, number of spaces between intervals) plot(x = primates$Bodywt, y = primates$Brainwt,... yaxp = c(0, 1500, 3))

Hands-on exercise 2 Try to replicate as closely as possible this graph Colors are 1:5 Note the axes Figure out how to add a title

More advanced axis properties For more control over axes, use the axis() function First create the plot but suppress the x or y axis using xaxt="n" and yaxt="n" Then add axes to whichever side they are needed plot(x = primates$Bodywt, y = primates$Brainwt,... yaxt = "n") axis(side = 2, at = seq(0,1500,300), labels = c(0,300,600,900,1200,">1500"))

Adding text using locator() Interactive function: click on the plot and it returns the x and y coordinates > locator(1) $x [1] $y [1] Add text at those coordinates text(x=207, y=306, label="Gorilla") Omit the 1 for multiple clicks, press to exit

Labeling points using text() Look up the help on ?text Can use vectors for x, y, and the text strings After creating the plot, call text() – pos=1 below – pos=2 to the left – pos=3 above – pos=4 to the right text(x = primates$Bodywt, y = primates$Brainwt, labels = primates[,1], pos=4)

Interactive point labeling If you don’t want to label all your points but there are a few outliers plot(x = primates$Bodywt, y = primates$Brainwt,...) identify(x = primates$Bodywt, y = primates$Brainwt, labels = primates[,1], n = 2) Click near n = 2 of the points

Points and lines ?lines gives values for lty, the line types For line widths use lwd lwd values lty values

More plot types In the plot() command, type specifies the type of plot to be drawn – "p" points – "l" lines – "b" both lines and points – "c" lines part alone of “b” – "o" overplotted – "h" histogram-like (or high-density) vertical lines – "s" stair steps – "n" for no plotting

Adding points or lines You can add a series of points or lines to the current plot using points() and lines() lines(x=seq(50,200,50), y=c(200,450,500,300), type="b", lwd=3, lty=2) points(x=seq(100,250,50), y=seq(100,250,50), cex=3, pch=17)

Hands-on exercise 3 [difficult] A normal curve is: exp(-x^2)/sqrt(2*pi) Create the plot on the right to illustrate where 95% of the area falls: ≤ x ≤ 1.96 Hint: use type in two different ways