R in Action Obtaining and installing R and RStudio

Slides:



Advertisements
Similar presentations
Internet Basics & Way Beyond!
Advertisements

MS® PowerPoint.
Chapter 3 Creating a Business Letter with a Letterhead and Table
Microsoft Excel 2003 Illustrated Complete A Worksheet Formatting.
DIGITAL GRAPHICS & ANIMATION Complete LESSON 4 ADDING TEXT TO 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.
SPSS Statistical Package for the Social Sciences is a statistical analysis and data management software package. SPSS can take data from almost any type.
Abstract # 0000 Make the Main Title with Large Bold Type Your Name Here Your Department Here Texas A&M Health Science Center Make the Main Title with Large.
Baburao Kamble (Ph.D) University of Nebraska-Lincoln Data Analysis Using R Week5: Charts/Plots in R.
Use a Large Bold Type for the Main Title Use Smaller Type for the Subtitle. Above type is 96 pt, this type is 66 pt Make Authors’ names smaller. This is.
Basic R Programming for Life Science Undergraduate Students Introductory Workshop (Session 1) 1.
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.
Make the Main Title with Large Bold Type Your Name and Title Here Your Department Here Texas A&M Health Science Center Make the Main Title with Large Bold.
R Graphics Lori Shepherd-Kirchgraber May 22, 2012.
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...)
Abstract # 0000 Make the Main Title with Large Bold Type Use Smaller Type for the Subtitle. Above Type is 105pt. This Type is 70pt. Make authors’ names.
Microsoft Office Illustrated Introductory, Premium Edition A Worksheet Formatting.
Microsoft Office XP Illustrated Introductory, Enhanced A Worksheet Formatting.
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.
Microsoft ® Office Excel 2007 Working with Charts.
Plotting risk perceptions vs. political orientation & science comprehension Steve Genco August 19, 2015.
Basic Editing Lesson 2.
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.
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.
CRSD Technology Training Tony Judice. Quick Access Toolbar – can be modifiedSave as… allows you to save the file to a different location and also as an.
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
Lesson 6 Formatting Cells and Ranges. Objectives:  Insert and delete cells  Manually format cell contents  Copy cell formatting with the Format Painter.
Spreadsheets What is Excel?. Objectives 1. Identify the parts of the Excel Screen 2. Identify the functions of a spreadsheet 3. Identify how spreadsheets.
Introduction to Drafting and Design In order to begin our drawing we have to set the drawing limits or the paper size.
Introduction to Technology. Parts of MSWord Screen Title Bar Quick Access Toolbar Button Ribbon Status Bar (views and zoom)
Data & Graphing vectors data frames importing data contingency tables barplots 18 September 2014 Sherubtse Training.
R PROGRAMMING FOR SQL DEVELOPERS Kiran Math Developer : Proterra in Greenville SC
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
Make the Main Title with Large Bold Type Use Smaller Type for the Subtitle. Above Type is 110pt. This Type is 80pt. Make authors’ names smaller. This is.
This is only an introduction!!
CNIT 131 HTML5 - Tables.
Permeability (% of Control)
Permeability (% of Control)
Programming in R Intro, data and programming structures
Static picture effects for PowerPoint slides
Working with Tabs and Tables
Excel Part 4 Working with Charts and Graphics
Excel Part 4 Working with Charts and Graphics
Trevor A. Branch FISH 552 Introduction to R
Page Layout Header & Footer Font Styles Image wrapping List Styles
MODULE 6 Microsoft Excel 2010
Static picture effects for PowerPoint slides
Using HTML Tables SWBAT: - create tables using HTML
exploring Microsoft Office 2013 Plus
Objectives At the end of this session, students will be able to:
R Course 1st Lecture.
Stat 251 (2009, Summer) Lab 2 TA: Yu, Chi Wai.
Creating a dataset in R Instructor: Li, Han
Introduction to R plot Instructor: Li, Han
Layout Terms Visual Hierarchy
Excel Part 4 Working with Charts and Graphics
Welcome To Microsoft Word 2016
Microsoft Excel 2007 – Level 2
Use a Large Bold Type for the Main Title (80 pt):
Permeability (% of Control)
Use a Large Bold Type for the Main Title (80 pt):
Use a Large Bold Type for the Main Title (70 pt):
Permeability (% of Control)
Use a Large Bold Type for the Main Title (70 pt):
Presentation transcript:

R in Action Obtaining and installing R and RStudio http://kek.ksu.ru/eos/DataMining/1379968983.pdf Obtaining and installing R and RStudio R is freely available from the Comprehensive R Archive Network (CRAN) at http://cran.r-project.org Y R studio: https://www.rstudio.com/ #Working with R set.seed(34) x <- rnorm(5) Comments are preceded by the # symbol set.seed:  Set the seed of R's random number generator, which is useful for creating random objects that can be reproduced Creates a vector object named x containing five random deviates from a standard normal distribución

Working with R #The function c()combines arguments into a vector age <- c(1,3,5,2,11,9,3,9,12,3) weight <- c(4.4,5.3,7.2,5.2,8.5,7.3,6.0,10.4,10.2,6.1) mean(weight) #arithmetic mean sd(weight) #standard deviation cor(age,weight) #correlation of x and y plot(age,weight) #plot of x and y #Randon uniform distribution x <- runif(20) summary(x) #summary hist(x) # histogram #Help help.start() #General help data() #List all available example datasets

Installing a package (with internet) Packages Packages are collections of R functions, data, and compiled code in a well-defined format #Packages library() #shows you what packages you’ve saved in your library search() #tells you which packages are loaded and ready to use R comes with a standard set of packages (including base, datasets, utils, grDevices, graphics, stats, and methods). Installing a package (with internet) #Installing a package install.packages("mlbench") # install the package mlbench Load a package #Load a package library(mlbench) # load and attach the package mlbench

Using output as input—reusing results Mtcars # 1974 Motor Trend US magazine head(mtcars) # Returns the first parts of a vector, matrix, table, or data frame fit <- lm(mpg~wt, data=mtcars) # lm(): fit linear models, regression summary(fit) plot(mtcars$wt,mtcars$mpg) title("Regression of MPG on Weight") abline(lmfit) # Add Straight Lines to a Plot

Vectors Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data. The combine function c() is used to form the vector #Vectors a <- c(1, 2, 5, 3, 6, -2, 4) b <- c("one", "two", "three") c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE) The data in a vector must only be one type or mode (numeric, character, or logical). You can’t mix modes in the same vector You can refer to elements of a vector using a numeric vector of positions within brackets # Refer to elements of a vector a <- c(1, 2, 5, 3, 6, -2, 4) a[3] a[c(1, 3, 5)] a[2:6]

y <- matrix(1:20, nrow=5, ncol=4) y Matrices A matrix is a two-dimensional array where each element has the same mode (numeric, character, or logical) # Matrices y <- matrix(1:20, nrow=5, ncol=4) y You can identify elements of a matrix X[i,] refers to the ith row X[,j] refers to jth column X[i, j] refers to the ijth element # identify elements x <- matrix(1:10, nrow=2) X x[2,] x[,2] x[1,4] x[1, c(4,5)]

Data frames A data frame is more general than a matrix in that different columns can contain different modes of data (numeric, character, etc.) #Data frames patientID <- c(1, 2, 3, 4) age <- c(25, 34, 28, 52) diabetes <- c("Type1", "Type2", "Type1", "Type1") status <- c("Poor", "Improved", "Excellent", "Poor") patientdata <- data.frame(patientID, age, diabetes, status) patientdata Specifying elements of a data frame # Specifying elements patientdata[1:2] patientdata[c(3,4)] patientdata$age # Contingency table of the counts at each combination of factor levels table(patientdata$diabetes, patientdata$status)

Attach, Detach, and With #Attach attach(mtcars) summary(mpg) plot(mpg, disp) detach(mtcars) #With with(mtcars, { summary(disp) plot(mpg, wt) })

Using factors #Factors patientID <- c(1, 2, 3, 4) age <- c(25, 34, 28, 52) diabetes <- c("Type1", "Type2", "Type1", "Type1") status <- c("Poor", "Improved", "Excellent", "Poor") diabetes <- factor(diabetes) status <- factor(status, order=TRUE) patientdata <- data.frame(patientID, age, diabetes, status) str(patientdata) summary(patientdata)

Lists #List g <- "My First List" h <- c(25, 26, 18, 39) j <- matrix(1:10, nrow=5) k <- c("one", "two", "three") mylist <- list(title=g, ages=h, j, k) Mylist mylist[[2]] mylist[["ages"]]

Entering data from the keyboard The edit() function invoke a text editor # Text editor mydata <- data.frame(age=numeric(0), gender=character(0), weight=numeric(0)) mydata <- edit(mydata) Entering data from Excel # Data from clipboard gastos<-read.table("clipboard",header=TRUE) # Data from csv file Gastos<-read.csv(“Gastos.cvs",head=T)

Working with graphs # Graph example dose <- c(20, 30, 40, 45, 60) drugA <- c(16, 20, 27, 40, 60) drugB <- c(15, 18, 25, 31, 40) plot(dose, drugA, type="b") title("Drug A") Use help(plot) to view other options

opar <- par(no.readonly=TRUE) #opar: current graphical settings Graphical parameters # Graphical settings par() #list of the current graphical settings par(no.readonly=TRUE) #list of current graphical settings that can be modified # Graphical settings example opar <- par(no.readonly=TRUE) #opar: current graphical settings par(lty=2, pch=17) plot(dose, drugA, type="b") # or: plot(dose, drugA, type="b", lty=2, pch=17) par(opar) #reset graphical settings

Parameters for specifying symbols and lines pch Specifies the symbol to use when plotting points cex Specifies the symbol size. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller lty Specifies the line type lwd Specifies the line width. lwd=2 generates a line twice as wide as the default # symbols and lines plot(dose, drugA, type="b", lty=3, lwd=3, pch=15, cex=2)

Parameters for specifying color col Default plotting color. col=“red” col.axis Color for axis text col.lab Color for axis labels col.main Color for titles col.sub Color for subtitles fg The plot’s foreground color bg The plot’s background color

Functions that can be used to create vectors of contiguous colors rainbow(), heat.colors(), terrain.colors(), topo.colors(), and cm.colors() # contiguous rainbow colors n <- 10 mycolors <- rainbow(n) pie(rep(1, n), labels=mycolors, col=mycolors) Parameters specifying text size cex plotted text. 1=default, 1.5 is 50% larger, 0.5 is 50% smaller cex.axis Magnification of axis text relative to cex cex.lab Magnification of axis labels relative to cex cex.main Magnification of titles relative to cex cex.sub Magnification of subtitles relative to cex par(font.lab=3, cex.lab=1.5, font.main=4, cex.main=2)

Parameters specifying font family, size, and style font Integer specifying font to use for plotted text.. 1=plain, 2=bold, 3=italic, 4=bold italic font.axis Font for axis text font.lab Font for axis labels font.main Font for titles font.sub Font for subtitles family Font family for drawing text. Standard values are serif, sans, and mono. Parameters for graph and margin dimensions pin Plot dimensions (width, height) in inches mai Numerical vector indicating margin size where c(bottom, left, top, right) is expressed in inches mar margin size where c(bottom, left, top, right) is expressed in lines. The default is c(5, 4, 4, 2) + 0.1. par(pin=c(4,3), mai=c(1,.5, 1, .2))

Using graphical parameters to control graph appearance #Two plots dose <- c(20, 30, 40, 45, 60) drugA <- c(16, 20, 27, 40, 60) drugB <- c(15, 18, 25, 31, 40) opar <- par(no.readonly=TRUE) par(pin=c(2, 3)) par(lwd=2, cex=1.5) par(cex.axis=.75, font.axis=3) plot(dose, drugA, type="b", pch=19, lty=2, col="red") plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green") #Two graphs in same plot lines(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green") par(opar)

Adding text, customized axes, and legends plot(dose, drugA, type="b", col="red", lty=2, pch=2, lwd=2, main="Clinical Trials for Drug A", sub="This is hypothetical data", xlab="Dosage", ylab="Drug Response", xlim=c(0, 60), ylim=c(0, 70)) Axis options Side (1=bottom, 2=left, 3=top, 4=right) at A numeric vector indicating where tick marks should be drawn labels A character vector of labels to be placed at the tick marks pos The coordinate at which the axis line is to be drawn lty Line type. col The line and tick mark color. las Labels are parallel (=0) or perpendicular (=2) to the axis.

#An example of custom axes x <- c(1:10) y <- x z <- 10/x opar <- par(no.readonly=TRUE) par(mar=c(4, 4, 2, 6) + 0.1) plot(x, y, type="b", pch=21, col="red", yaxt="n", lty=3, ann=FALSE) lines(x, z, type="b", pch=22, col="blue", lty=2) axis(2, at=x, labels=x, col.axis="red", las=2) axis(4, at=z, labels=round(z, digits=2), col.axis="blue", las=2, cex.axis=0.7, tck=-.01) mtext("y=1/x", side=4, line=3, cex.lab=1, las=2, col="blue") title("An Example of Creative Axes", xlab="X values", ylab="Y=X") par(opar)

Reference lines The abline() function is used to add reference lines to our graph. The format is abline(h=yvalues, v=xvalues) #Add reference lines abline(h=c(1,5,7)) abline(v=c(1, 3, 5, 7, 9), lty=2, col="blue") Legend legend(location, title, legend, ...) location You can give an x, y coordinate for the upper-left corner of the legend. You can use locator(1), in which case you use the mouse to indicate the location of the legend. You can also use the keywords bottom, bottomleft, left, topleft, top, topright, right, bottomright, or center to place the legend in the graph. title A character string for the legend title (optional). legend A character vector with the labels.

Comparing Drug A and Drug B response by dose dose <- c(20, 30, 40, 45, 60) drugA <- c(16, 20, 27, 40, 60) drugB <- c(15, 18, 25, 31, 40) opar <- par(no.readonly=TRUE) par(lwd=2, cex=1.5, font.lab=2) plot(dose, drugA, type="b", pch=15, lty=1, col="red", ylim=c(0, 60), main="Drug A vs. Drug B", xlab="Drug Dosage", ylab="Drug Response") lines(dose, drugB, type="b", pch=17, lty=2, col="blue") abline(h=c(30), lwd=1.5, lty=2, col="gray") library(Hmisc) minor.tick(nx=3, ny=3, tick.ratio=0.5) legend("topleft", inset=.05, title="Drug Type", c("A","B"), lty=c(1, 2), pch=c(15, 17), col=c("red", "blue")) par(opar)

text(location, "text to place", pos, ...) Text annotations text() places text within the graph text(location, "text to place", pos, ...) mtext() places text in one of the four margins mtext("text to place", side, line=n, ...) location Location can be an (x, y) coordinate, or via mouse by specifying location as locator(1) pos Position relative to location. 1 = below, 2 = left, 3 = above, 4 = right. side Which margin to place text in, where 1 = bottom, 2 = left, 3 = top, 4 = right You can also specify adj=0 for left/bottom alignment or adj=1 for top/right alignment.

Example of a scatter plot (car weight vs Example of a scatter plot (car weight vs. mileage) with labeled points (car make) # scatter plot with labeled points attach(mtcars) plot(wt, mpg, main="Mileage vs. Car Weight", xlab="Weight", ylab="Mileage", pch=18, col="blue") text(wt, mpg, row.names(mtcars), cex=0.6, pos=4, col="red") detach(mtcars) #Display font families opar <- par(no.readonly=TRUE) par(cex=1.5) plot(1:7,1:7,type="n") text(3,3,"Example of default text") text(4,4,family="mono","Example of mono-spaced text") text(5,5,family="serif","Example of serif text") par(opar)

opar <- par(no.readonly=TRUE) par(mfrow=c(2,2)) Combining graphs R makes it easy to combine several graphs into one overall graph, using either the par() or layout() function With par() mfrow=c(nrows, ncols) to create a matrix of nrows x ncols plots that are filled in by row. mfcol=c(nrows, ncols) to fill the matrix by columns. # plots filled by row attach(mtcars) opar <- par(no.readonly=TRUE) par(mfrow=c(2,2)) plot(wt,mpg, main="Scatterplot of wt vs. mpg") plot(wt,disp, main="Scatterplot of wt vs disp") hist(wt, main="Histogram of wt") boxplot(wt, main="Boxplot of wt") par(opar) detach(mtcars)

opar <- par(no.readonly=TRUE) par(mfrow=c(3,1)) hist(wt) hist(mpg) #plots filled by row attach(mtcars) opar <- par(no.readonly=TRUE) par(mfrow=c(3,1)) hist(wt) hist(mpg) hist(disp) par(opar) detach(mtcars) #Creating a figure arrangement with fine control opar <- par(no.readonly=TRUE) par(mar = c(4, 4, 2, 2)) par(fig=c(0, 0.8, 0, 0.8)) plot(mtcars$wt, mtcars$mpg, Xlab=”miles Per Gallon”, ylab=“Car Weigth”) par(fig=c(0, 0.8, 0.55, 1), new=TRUE boxplot(mtcars$wt, horizontal=TRUE, axes=FALSE) par(fig=c(0.65, 1, 0, 0.8), new=TRUE) boxplot(mtcars$mpg, axes=FALSE) mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3) par(opar) The full graph area as going from (0,0) in the lower-left corner to (1,1) in the upper-right corner fig= parameter is a numerical vector of the form c(x1, x2, y1, y2)

Useful functions for working with data objects length(object) Number of elements/components. dim(object) Dimensions of an object. str(object) Structure of an object. class(object) Class or type of an object. mode(object) How an object is stored. names(object) Names of components in an object. c(object, object,...) Combines objects into a vector. cbind(object, object, ...) Combines objects as columns. rbind(object, object, ...) Combines objects as rows. object Prints the object. head(object) Lists the first part of the object. tail(object) Lists the last part of the object. ls() Lists current objects. rm(object, object, ...) Deletes one or more objects. The statement rm(list = ls()) will remove most objects from the working environment. newobject <- edit(object) Edits object and saves as newobject. fix(object) Edits in place.