Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using R Graphs in R.

Similar presentations


Presentation on theme: "Using R Graphs in R."— Presentation transcript:

1 Using R Graphs in R

2 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:

3 Learning Objectives Concepts Types Creating Exporting

4 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

5 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:

6 Different Types of Plots Available in R

7 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:

8 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

9 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)

10 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

11 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”

12 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()

13 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

14 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

15 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:

16 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

17 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 …

18 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:

19 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

20 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!

21 Thank you


Download ppt "Using R Graphs in R."

Similar presentations


Ads by Google