Using R Graphs in R.

Slides:



Advertisements
Similar presentations
1 SESSION 5 Graphs for data analysis. 2 Objectives To be able to use STATA to produce exploratory and presentation graphs In particular Bar Charts Histograms.
Advertisements

Introduction to Lattice Graphics Richard Pugh 4th December 2012.
Rich Pugh Andy Nicholls Head to Head: Lattice vs ggplot2 Rich Pugh
What is a spreadsheet? A spreadsheet is a program used for performing calculations on a table divided into rows and columns. Cells in a spreadsheet can.
PHP Sample Application Simple graphics and database.
Introduction to R Graphics
With Microsoft ® Excel 2010© 2011 Pearson Education, Inc. Publishing as Prentice Hall1 GO! with Microsoft ® Excel 2010 Chapter 1 Creating a Worksheet and.
Graphics in R data analysis and visualization Katia Oleinik Scientific Computing and Visualization Boston University
Spreadsheets A spreadsheet package is a general purpose computer package that is designed to perform calculations. A spreadsheet is a table which is divided.
Review. Microsoft Office Excel 2013 provides powerful tools to organize, analyze, manage, and share information Locations where work is done are cells,
R graphics  R has several graphics packages  The plotting functions are quick and easy to use  We will cover:  Bar charts – frequency, proportion 
Plotting with ggplot2: Part 1
Plotting Systems in R Dr. Jieh-Shan George YEH
Engineering H193 - Team Project Spring Quarter Gateway Engineering Education Coalition P. 1 Laboratory Quiz & Data Analysis and Presentation for Written.
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.
FISH 397C Winter 2009 Evan Girvetz More Complex Graphics in R © R Foundation, from
Data Visualization with R (II)
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 12 Spreadsheets and Business Graphics: Facts and Figures.
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.
Baburao Kamble (Ph.D) University of Nebraska-Lincoln
Copyright © 2003 by Prentice Hall Spreadsheets and Business Graphics What is a spreadsheet? Why and when a spreadsheet is used, advantages of spreadsheets.
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.
Computers: Tools for an Information Age Chapter 12 Spreadsheets and Business Graphics: Facts and Figures.
Examples of different formulas and their uses....
Graphs Graphs are used to display data. They visually represent relationships between data. All graphs should have a title that identifies the variables.
Computers Are Your Future © 2006 Prentice-Hall, Inc.
This is the first sheet of a spreadsheet workbook. The workbook begins initially with 3 work sheets. A spreadsheet is made of columns and rows. The intersection.
SPREADSHEET BASICS SPREADSHEET BASICS What are the benefits of using a spreadsheet to solve a problem?
Graphing Parameters Titles X-Axis Title Y-Axis Title Legend Scales Color Gridlines library(help="graphics") Basic Chart Types The R Graphics Package LineHistogram.
PowerBuilder Online Courses - by Prasad Bodepudi
An Introduction to R graphics Cody Chiuzan Division of Biostatistics and Epidemiology Computing for Research I, 2012.
Spreadsheet Basics chapter 7
Learning R hands on. Organization of Folders: Class Data folder has datasets (end in.csv or.rda) Rcode has the scripts of R commands that can cut and.
1Copyright © Prentice Hall 2000 Spreadsheets and Business Graphics Chapter 12 Facts and Figures.
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.
GRAPHICS MODULE 14 STUDY BOOK. Graphic commands SCREEN - puts the screen into graphics mode WINDOW - allows scaling of the screen LINE - 3 formats –LINE.
Ggplot2 A cool way for creating plots in R Maria Novosolov.
Plotting Complex Figures Using R
Mr. Magdi Morsi Statistician Department of Research and Studies, MOH
Introduction to Graphics in R 3/12/2014. First, let’s get some data Load the Duncan dataset It’s in the car package. Remember how to get it? – library(car)
Great way to show your data!. * In your journal, draw a 4 square grid.
Introduction to plotting data Fish 552: Lecture 4.
Spreadsheet Evidence By.... P2 – DEVELOP A COMPLEX SPREADSHEET MODEL TO MEET PARTICULAR NEEDS.
Statistical Programming Using the R Language Lecture 2 Basic Concepts II Darren J. Fitzpatrick, Ph.D April 2016.
Spreadsheet I n Concepts & operations. Concepts n Workbook: Excel file n Worksheet: sheet n Row: 1-???? n Column: A - Z, AA - ?? n Cell n Cell address.
Charts & Graphs CTEC V
Office tool for creating tables and charts
Computers: Tools for an Information Age
Lecture 25.
Statistical Programming Using the R Language
Computers: Tools for an Information Age
End to End data analysis in R
Microsoft Excel A Spreadsheet Program.
Lecture 25: Exploring data
Chapter 8: ODS Graphics ODS graphics were not available prior to SAS 9.2 They have been implemented across a wide range of procedures Functionality isn’t.
R Assignment #4: Making Plots with R (Due – by ) BIOL
Microsoft Office 2013 Coming to a PC near you!.
Introduction to Spreadsheets
Chapter 4 Application Software
Tell a Story with the Data
Recoding II: Numerical & Graphical Descriptives
INTRODUCTION TO SGPLOT Zahir Raihan OVERVIEW  ODS Graphics  SGPLOT overview  Plot Content  High value plot statements  High value plot options 
Code is on the Website Outline Comparison of Excel and R
General Purpose Packages
Unit G: Using Complex Formulas, Functions, and Tables
A drag and drop exercise can be created using Word quite easily using tables, text boxes and ensuring the document is saved properly.
Microsoft Excel.
Simulate Multiple Dice
Presentation transcript:

Using R Graphs in R

Development Team Bhaswati Ganguli University of Calcutta, India Principal Investigator: Arindam Basu University of Canterbury, New Zealand Paper Coordinator: Arindam Basu, University of Canterbury, New Zealand Content Writer: Bhaswati Ganguli, University of Calcutta, India Content Reviewer:

Learning Objectives Concepts Types Creating Exporting

Types of Graphics Functions in R High Level Functions (Produce Complete Plots) Low Level Functions (Adds Outputs to high level functions) In-built Graphics Systems allow high level plotting function Grid Systems based Lattice and ggplot2 allow complete plots

Diagram of Different Devices and Packages Source: Murrell, P. M. (2005). R graphics. Upper Saddle River, N.J.: Pearson/Prentice Hall. Retrieved from Google Books: http://www.crcnetbase.com/isbn/9781420035025

Different Types of Plots Available in R

Grid Systems: Trellis Plots in R Allows Multiple Panels Advantages of Trellis plots Allows Multiple Panels Data Can be Split Source: Murrell, P. M. (2005). R graphics. Upper Saddle River, N.J.: Pearson/Prentice Hall. Retrieved from Google Books: http://www.crcnetbase.com/isbn/9781420035025

Which Systems to Use? Stick to Any One System (if Graphics stick to graphics, else Grid, stick to grid based) Grid System based packages such as lattice and ggplot2 are feature rich Graphics (Traditional) is convenient and starts at runtime (most situations) If you are starting out and basic plots required start with Graphics For complex visualisations, use Lattice and or ggplot2

How do I Start Plotting Graphs in R? All basic plotting functions are generic In-built Graphics system, starts when you start R For lattice and ggplot2, start with library(lattice), library(ggplot2) For generic, start by typing plot() Within plot(), add parameters within parentheses For lattice and ggplot2, all graphs are saved as objects If you want to see graphs, print(object)

List of Basic High Level Plotting Commands Use plot() for scatterplots (can also output box plots) For bar plots of categorical variables, use barplot() For creating box plots, use boxplot(), and then specify one or two variables For generating histograms of continuous variables, type hist() Then draw density plots within them by using lines(density()) using density functions If you want to create pie charts of tables, use pie() The first argument within parentheses is always data or another variable

What goes in High Level Plotting Commands Do this: plot(variable or data, then write options here) If you want to set colours, use col = “name of the colour” If you want to set the type of lines in the graph, use lty=“type of line” If you want to change the plot points, use pch=“code number or letter goes here” Change the axis values by typing xlim=“your value ranges”, ylim=“value ranges for y axis” Type labels using xlab=“the label here”, or ylab=“the label text”, Add heading and subheading to the graph by using main=“text”, sub=“text”

How do I Annotate Plots? If you want to add lines to your plots, use lines() Within the lines() command, put your functions, commands, etc If you want to add data symbols, use points() If you want to annotate and identify points, use identify()

How do I put Multiple Plots in the Same Page? Use the par() function Specify the number of rows and columns by c(rows, columns) Example c(3,2) will produce 3 rows and 2 columns Then let R fill in the space row-wise or column-wise by specifying mfrow or mfcol So, the code looks like par(mfrow = c(3, 2)) tells R to: Fill the canvas space row wise and put three rows and two columns to it

Take Graphical Outputs out of R Step 1: Open a device by naming it: for example, png(“thisfile.png”) Step 2: Push all outputs to this device, type all your Step 3: Close the device by typing dev.off() You can only have one device active at any one session

List of Graphical Formats in R Souce: Murrell, P. M. (2005). R graphics. Upper Saddle River, N.J.: Pearson/Prentice Hall. Retrieved from Google Books: http://www.crcnetbase.com/isbn/9781420035025

Using Grid System Grid system is very feature rich and lets you customise Two good packages that use the Grid systems are lattice and ggplot2 These need to be installed and loaded, as follows: installing, install.packages(“ggplot2”) or install.packages(“lattice”) Loading is by issuing command library(ggplot2) or library(lattice) They save all graphs as objects and let you update the objects

How do I Use Lattice Plots Use install.packages(“lattice”) if not already installed,and then load the package Load it by library(lattice) First argument in lattice is a formula in the form of either ~x, or x ~ y Second argument in lattice is a data set data=mydata You can use conditional variables as x ~ y | z (note the pipe | ) The functions are similar to Traditional Graphics You can also put additional options for headers, xlim, ylim, xlab, ylab, others …

Comparison of Plots between Lattice and Traditional Source: Murrell, P. M. (2005). R graphics. Upper Saddle River, N.J.: Pearson/Prentice Hall. Retrieved from Google Books: http://www.crcnetbase.com/isbn/9781420035025

How to Use ggplot2 The package ggplot2 is based on “Grammar of Graphics” by Leland Wilkinson You need to install and load before using, install by install.packages(“ggplot2”) Load package by using library(ggplot2) Use qplot() to start using making simple plots and learn more by visiting the help pages General motif of using ggplot2 is qplot(x,y, data=mydata, options …) Where x,y are variables and data frame as in lattice, and options same as what we see in traditional Graphics systems Everything is saved as objects so use print(object) to see the outputs

Conclusion: What did we Learn? We Learned two types of graphic systems: Graphics and Grid Graphics System is in-built and loads when R loads Grid systems have lattice and ggplot2 and need to be installed and loaded We learned about high level and low level plotting We learned how to use high level commands to produce complete graphs You can use low level plotting commands by adding lines, points, headers When using lattice and ggplot2, they save the plots to objects that you can print and save Follow along the codebook and practice, practice, practice!

Thank you