• Tidak ada hasil yang ditemukan

Histogram 57

1000 Random Weibull Distribution Values

Density

0.6 0.8 1.0 1.2

0.00.51.01.52.02.53.03.5

In order to superimpose a normal density we first calculate themeanand standard deviation of the data and set those values to m and s, respectively. We usecurveand graph the normal density with mean m and standard deviation s withdnorm. We color it blue and use a line width of 2. Lastly, add=TRUE adds this to the current plot instead of creating a new plot.

Code Review for the Graphing Chapter

abline(a=,b=,h=,v=) adds lines to a graph. The value of a and b are an intercept and slope. The values set to h are y values for horizontal lines. The values set to v arexvalues for vertical lines.

axis(i, at=, label=) adds tick marks to the graph at the location given by at with labels given by labels. The values of i are 1, 2, 3, and 4, for the bottom, left, top, and right axis, respectively.

Histogram 59 barplot(counts) creates a bar chart of data that is tabulated and

stored in counts. If counts is a matrix then a stacked or side-by-side bar chart will be created.

box(option) draws a box in the current graph with option set to

“plot”, “figure”, “inner”, and “outer”.

boxplot(data) creates side-by-side box plots where data is a list of vectors of data.

curve(f,a,b) graphs the functionf fromato b. When the argument add=TRUE is included the function is added to the current graph.

There are numerous options. Execute ?curve for optional graphing parameters.

dev.new() opens a new graphics device.

expression() is a text command that creates mathematical expres- sions with mostly standard syntax. For example, use xˆ2 forx2and pi forπ, but x[i] forxi.

function(x1, x2, . . . , xn){expression} defines a function of x1, x2, . . . , xn with expression. The function will return the last line of expression and for clarity use return().

grid(NULL,NULL) adds grid lines at thexand y tick marks of a plot. NULL can be replaced with, n, for the number of grid lines or NA for no grid lines.

hist(data) creates a histogram of the data. Two key options are freq, the default is TRUE, and breaks, to set the breakpoints between cells.

legend() adds a legend to a plot. There are numerous options. Run

?legend to learn more.

mean(data) returns the mean of the data set.

mtext(text, side=, line=) places the text in the margins on side 1 through 4 on the given line.

par(“usr”) returns a vector of four elements that are the values of the left, right, bottom, and top of the graphing frame. In other words the first and third elements represent the bottom left coordinate, while the second and fourth elements represent the top right corner. The function par has many uses, in particular with mfrow to set a grid for graphs. Run ?par to learn more.

paste() will concatenate characters, in quotes, and variables, where each term is separated by a comma in paste. The default is to place a space between terms and the option sep=“”, empty quotes, places no space between terms.

pie(counts) creates a pie chart of data that is tabulated and stored in counts.

plot(x,y) plots the points given by xand y, which may be vectors.

Plot can interpret and plot a variety of objects beyond a set ofxand yvalues. There are also numerous graphing options that can be used in plot.

points(x,y) adds the points given byxandy, which may be vectors, to the current graph.

replace(v,where,what) replaces terms in the vector v identified by where with what. The second option where can be a list of indices or a logical statement to identify location in v to be replaced.

segments(x1, y1, x2, y2) draws a line segment from (x1, y1) to (x2, y2).

stripchart(data) creates side-by-side strip charts where data is a list of vectors of data. If add=TRUE strip chart can be overlayed on box plots.

text(a,b,“text”) places text at the coordinate (a, b).

windows() opens a graphics device, with options for the dimensions of the window.

Selected Graphing Options

adj tells R how to justify text to its location with 0 left/bottom, 0.5 center, and 1 right/top. It can be set as one value or c(x,y) if the justification is different inxandy.

cex scales the size of an object such as the plot points and has addi- tional specifications such as cex.axis, cex.lab, and cex.main.

col sets the color of an object and has additional specifications such as col.axis, col.lab, and col.main.

font sets the type of font with 1=plain, 2=bold, 3=italic, and 4=bold italic. Font has additional specifications such as font.axis, font.lab, and font.main.

Exercises 61 las orients text either parallel, =0, to the axis or side or perpendicular,

=2.

lty defines the type of line with 1 being solid and 2-6 various dashed lines.

lwd scales the line width.

pch is the plot symbol with options from 1 through 25, plus a few special symbols such as %, #, and +.

side is set to 1, 2, 3, or 4 to place objects in the bottom, left, top, and right margins.

type is the way the points are plotted with common types of p=points, l=lines, and 0=points with lines.

Dalam dokumen BOOK College mathematics and statistics (Halaman 74-78)