Details 330: Address 4.


52 views
Uploaded on:
Description
Sample: the cherry tree information. Assume we have a content record called cherry.txt (presumably made utilizing Notepad or possibly Word, yet spared as a content document) ...
Transcripts
Slide 1

Details 330: Lecture 4 Graphics: Doing it in R 330 address 4

Slide 2

Housekeeping My contact points of interest… . In addition much else on course website page www.stat.auckland.ac.nz/~lee/330/Or by means of Cecil 330 address 1

Slide 3

330 address 1

Slide 4

Today\'s address: R for illustrations Aim of the address: To demonstrate to you proper methodologies to utilize R to deliver the plots appeared in the last few addresses 330 address 4

Slide 5

Getting information into R In 330, as in most practice, information comes in 2 principle shapes As a content record As an Excel spreadsheet Need to change over from these organizations to R Data in R is sorted out in information outlines Row by section game plan of information (as in Excel) Variables are segments Rows are cases (people) 330 address 4

Slide 6

Text documents to R Suppose we have the information as a content record Edit the content record (use Notepad or comparative) so that The primary line comprises of the variable names Each column of information (i.e. information on a complete case) compares to one line of the record Suppose information fields are isolated by spaces and/or tabs Then, to make an information outline containing the information, we utilize the R capacity read.table 330 address 4

Slide 7

Example: the cherry tree information Suppose we have a content document called cherry.txt (most likely made utilizing Notepad or possibly Word, yet spared as a content record) First line: variable names Data for every tree on a different line, isolated by "white space" (spaces or tabs) 330 address 4

Slide 8

Creating the information outline In R, sort cherry.df = read.table(file.choose(), header=T) and press the arrival key Click here to choose document This raises the exchange to choose the document cherry.txt containing the information. Click here to load information 330 address 4

Slide 9

Check all is OK! 330 address 4

Slide 10

Getting information from a spreadsheet (1) Create the spreadsheet in Excel Save it as Comma Delimited Text (CSV) This is a content record with all phones isolated by commas File is called cherry.csv 330 address 4

Slide 11

Getting information from a spreadsheet (2) In R, sort cherry.df = read.table(file.choose(), header=T, sep=",") and continue as before 330 address 4

Slide 12

Data edges and variables Suppose we have perused in information and made an information outline At this point R doesn\'t think about the variables in the information outline, so we can\'t utilize e.g. the variable distance across in R orders We have to say attach(cherry.df) to make the variables in cherry.df noticeable to R. Then again, say cherry.df$diameter 330 address 4

Slide 13

Scatterplots In R, there are 2 particular arrangements of capacities for illustrations, one for standard design, one for trellis. Eg for scatterplots, we utilize either plot (normal R) or xyplot (Trellis) In the following few slides, we talk about plot. 330 address 4

Slide 14

Simple plotting plot(height,volume, xlab="Height (feet)", ylab="Volume (cubic feet)", main = "Volume versus tallness for 31 dark cherry trees") i.e. name tomahawks (give units if conceivable), give a title 330 address 4

Slide 15

330 address 4

Slide 16

Alternative type of plot plot(volume ~ tallness, xlab="Height (feet)", ylab="Volume (cubic feet)", main = "Volume versus stature for 31 dark cherry trees", data = cherry.df) Don\'t have to append with this structure, note inversion of x,y 330 address 4

Slide 17

Colors, focuses, and so on Type ?standard for more information par(bg="darkblue") plot(height,volume, xlab="Height (feet)", ylab="Volume (cubic feet)", main = "Volume versus tallness for 31 dark cherry trees", pch=19,fg="white", col.axis="lightblue",col.main="white", col.lab="white",col="white",cex=1.3) 330 address 4

Slide 18

330 address 4

Slide 19

Lines Suppose we need to sign up the rats on the rats plot. (see information next slide) We could attempt plot(day, development, type="l") however this won\'t work Points are plotted all together they show up in the information outline and every point is joined to the following 330 address 4

Slide 20

Rats: the information > rats.df development bunch rodent change day 1 240 1 2 250 1 8 3 255 1 15 4 260 1 22 5 262 1 29 6 258 1 36 7 266 1 2 43 8 266 1 2 44 9 265 1 2 50 10 272 1 2 57 11 278 1 2 64 12 225 1 2 1 12 230 1 2 1 8 ... More information 330 address 4

Slide 21

Don\'t need this! 330 address 4

Slide 22

Solution Various arrangements, however one is to plot every line independently, utilizing subsetting plot(day,growth,type="n") lines (day[rat==1],growth[rat==1]) lines (day[rat==2],growth[rat==2]) thus on … . (exhausting!), or (better) for(j in 1:16){ lines (day[rat==j],growth[rat==j]) } Draw tomahawks, names just 330 address 4

Slide 23

Indicating bunches Want to plot the litters with various hues, include a legend: Rats 1-8 are litter 1, 9-12 litter 2, 13-16 litter 3 plot(day,growth,type="n") for(j in 1:8)lines(day[rat==j], growth[rat==j],col="white") # litter 1 for(j in 9:12)lines (day[rat==j], growth[rat==j],col="yellow") # litter 2 for(j in 13:16)lines (day[rat==j], growth[rat==j],col="purple") # litter 3 Set shade of line 330 address 4

Slide 24

legend legend(13,380, legend = c("Litter 1", "Litter 2", "Litter 3"), col = c("white","yellow","purple"), lwd = c(2,2,2), horiz = T, cex = 0.7) (Type ?legend for a full clarification of these parameters) 330 address 4

Slide 25

330 address 4

Slide 26

Points and content x=1:25 y=1:25 plot(x,y, type="n") points(x,y,pch=1:25, col="red", cex=1.2)

Slide 28

Points and content (3) x=1:26 y=1:26 plot(x,y, type="n") text(x,y, letters, col="blue", cex=1.2)

Slide 30

Trellis Must load trellis library first library(lattice) General type of trellis plots xyplot(y~x|W*Z, data=some.df) Don\'t have to append information outline, trellis capacities can choose the variables, given the information outline 330 address 4

Slide 31

Main trellis capacities dotplot for dotplots, use when X is all out, Y is persistent bwplot for boxplots, use when X is clear cut, Y is nonstop xyplot for scramble plots, use when both x and y are consistent equal.count use to transform ceaseless molding variable into gatherings 330 address 4

Slide 32

Changing foundation shading To change trellis foundation to white trellis.par.set(background = list(col="white")) To change plotting images trellis.par.set(plot.symbol = list(pch=16, col="red", cex=1)) 330 address 4

Slide 33

Equal.count xyplot(volume~height|diameter, data=cherry.df) 330 address 4

Slide 34

Equal.count (2) diam.gp<- equal.count(diameter,number=4,overlap=0) xyplot(volume~height|diam.gp, data=cherry.df) 330 address 4

Slide 35

Changing plotting images To change plotting images trellis.par.set(plot.symbol = list(pch=16, col="red", cex=1)) 330 address 4

Slide 36

330 address 4

Slide 37

Non-trellis adaptation coplot(volume~height|diameter, data=cherry.df) 330 address 4

Slide 38

Non-trellis rendition (2) coplot(volume~height|diameter, data=cherry.df,number=4,overlap=0) 330 address 4

Slide 39

Other helpful capacities Regular R scatterplot3d (3d dissipate plot, load library scatterplot3d) form, persp (draws shape plots, surfaces) sets Trellis cloud (3d diffuse plot) 330 address 4

Recommended
View more...