Trevor A. Branch FISH 552 Introduction to R

Slides:



Advertisements
Similar presentations
Using Microsoft PowerPoint in the Classroom
Advertisements

Introduction to R Graphics
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.
Scripts and Flow Control. Scripts So far we have been entering commands directly into the command line But there is a better way Script files (and functions)
Evan Girvetz Winkenwerder Introduction to Graphics in R © R Foundation, from
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.
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.
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.
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.
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
Microsoft ® Office Excel 2007 Working with Charts.
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.
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.
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
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.
Introduction to plotting data Fish 552: Lecture 4.
Review > mean(humidity, na.rm=T) > humidity[!is.na(humidity)] > X x Y Y[[1]][2,3] > zone.fac
Statistical Programming Using the R Language Lecture 2 Basic Concepts II Darren J. Fitzpatrick, Ph.D April 2016.
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.
Chapter 5: Charts & Sparklines Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Excel Working with Charts and Graphs
Professional Template for a 48x36 poster presentation
The poster title goes here and here
Reading and Writing Image Files
CUS POWERPOINT PRESENTATION
Release Numbers MATLAB is updated regularly
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.
Lecture 25.
Excel Part 4 Working with Charts and Graphics
Tutorial 4: Enhancing a Workbook with Charts and Graphs
Lecture 25: Exploring data
Professional Template for a poster presentation
Introduction to R Studio
Template for a 36x48 poster presentation
Excel Part 4 Working with Charts and Graphics
More on Graphical User Interfaces
The poster title goes here and here
Professional Template for a 36x48 poster presentation
Poster title Author(s) Institution(s) Corresponding author’s Name
MATLAB How to use (using M-files) Double click this icon
Poster title Author(s) Institution(s) Corresponding author’s Name
Microsoft Office Illustrated Introductory, Windows XP Edition
Using Charts in a Presentation
MATLAB How to use (using M-files) Double click this icon
CSc4730/6730 Scientific Visualization
Introduction to TouchDevelop
MATLAB How to use (using M-files)
Introduction to PowerPoint
The poster title goes here and here
The poster title goes here and here
Plotting Signals in MATLAB
Professional Template for a 36x48 poster presentation
Professional Template for a 36x48 poster presentation
Using Charts in a Presentation
Introduction to R plot Instructor: Li, Han
Introduction to Excel 2007 Part 3: Bar Graphs and Histograms
Excel Part 4 Working with Charts and Graphics
Microsoft Office Illustrated Fundamentals
The poster title goes here and here
Presentation transcript:

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

Recommended reading R graphics 2nd Edition (Paul Murrell, 2011) Chapters 1 and 2 Chapter 1 of first edition here: https://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html R code available for all plots in 2nd edition: https://www.stat.auckland.ac.nz/~paul/RG2e/

Projects in RStudio Getting R to figure out where the files are (directories) is key when reading in data from files RStudio has projects which do this in a very slick manner You can store different analyses in different projects and quickly switch between them Each project has a separate set of .r files that are open, and a separate R workspace (saved objects in console)

Using projects Click on top-right option Choose "Create Project" -> "Existing Directory" (or "New Project" if a directory does not exist) Select a directory, e.g. "Lectures" for me The project will be saved as a file in that directory called "Lectures.Rproj" Opening that will open the RStudio project Automatically sets the R working directory to that directory

In-class exercise (singly) Create a Project in your U:/username directory Download from the course website the file "primates.csv", and save it into the same directory Create a new R script file "Lecture 4.r" and save it (in the same directory) Add R code to the script file to read in the data file > read.csv(file="primates.dat", header=T) X Bodywt Brainwt 1 Potar monkey 10.0 115 2 Gorilla 207.0 406

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 10.0 115 2 Gorilla 207.0 406 3 Human 62.0 1320 4 Rhesus monkey 6.8 179 5 Chimp 52.2 440

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 http://research. stowers-institute 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 > 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)

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, are recycled Different point characters: pch=1:5 Different point letters: pch=c("a","g","t","c") 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] 207.6493 $y [1] 305.7384 Add text at those coordinates text(x=207, y=306, label="Gorilla") Omit the 1 for multiple clicks, press <esc> 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 lwd values ?lines gives values for lty, the line types For line widths use lwd 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 The equation for the standard normal density is exp(-x^2)/sqrt(2*pi) Create the plot on the right to illustrate where 95% of the area falls: -1.96 ≤ x ≤ 1.96 Hint: use type in two different ways