28
Customizing Graphs Base graphics options

Customizing Graphs

  • Upload
    lois

  • View
    50

  • Download
    1

Embed Size (px)

DESCRIPTION

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 a scatterplot plot( y~x , data= data.frame ) produces a scatterplot - PowerPoint PPT Presentation

Citation preview

Page 1: Customizing Graphs

Customizing Graphs

Base graphics options

Page 2: Customizing Graphs

plot()• The workhorse plotting function• plot(x) plots values of x in

sequence or a barplot• plot(x, y) produces a scatterplot• plot(y~x, data=data.frame)

produces a scatterplot• Many statistical functions produce

results that can be plotted using plot

Page 3: Customizing Graphs

Variations on plot()• type = “p”, “l”, “b”, or “n” for

points, lines, both, or no plot (also “c”, “o”, “h”, “s”, “S”)

• asp = sets the y/x aspect ratio (asp=1 for mapping)

• Full control over labels, axes, symbols, lines, etc

Page 4: Customizing Graphs

Parameter cex=• cex controls the size of text and

symbols in a graph• cex = 1 is the default size (except

for main= where it is 2)• cex.main, cex.sub, cex.lab,

cex.axis control specific parts of the plot

• cex = alone usually controls the size of pch, the plot character

Page 5: Customizing Graphs

Symbol/Line Styles• pch = (plot character) specifies the

symbol set• cex = symbol size• lty = specifies the line type• lwd = specifies the line thickness

Page 6: Customizing Graphs
Page 7: Customizing Graphs
Page 8: Customizing Graphs
Page 9: Customizing Graphs

Parameter col=• col = controls the color of a symbol

or line• col.axis, col.lab, col.main, col.sub

control colors of other parts of the graph

• bg = background color• col takes a name or hexadecimal

color value

Page 10: Customizing Graphs

Color• colors() for list of 657 named

colors• palette() gives the default list of

eight colors• gray() gives grayscale from black -

gray(0) to white – gray(1)

Page 11: Customizing Graphs
Page 12: Customizing Graphs
Page 13: Customizing Graphs

Color Palettes• rainbow(number) generates a

spectrum from red to violet based on hue, saturation, and value (hsv)

• rainbow_hcl(number) similar, but using hue, chroma, and luminance color space (hcl)

• Help pages for these give several other palettes

Page 14: Customizing Graphs
Page 15: Customizing Graphs
Page 16: Customizing Graphs
Page 17: Customizing Graphs
Page 18: Customizing Graphs
Page 19: Customizing Graphs
Page 20: Customizing Graphs

Fonts• family = sets the font family –

“serif”, “sans”, and “mono” – others may be defined by specific graphic devices

• font = sets the font, 1 plain, 2 bold, 3 italic, 4 bold italic, 5 symbol

• Fonts may be device dependent

Page 21: Customizing Graphs
Page 22: Customizing Graphs

Labels• main = “This is the title”,

cex.main, col.main, font.main• sub = “This is the subtitle”,

cex.sub, col.sub, font.sub• Axis labels

– lab = “This is the x-axis label”– ylab = “This is the y-axis label”– cex.lab, col.lab, font.lab, las (0, 1, 2,

3)

Page 23: Customizing Graphs

Axes• Default usually selects reasonable

axes• xlim, ylim set limits for axes• xaxp, yaxp set tick marks• xlog, ylog to use log scale• xaxt , yaxt “n” suppresses plotting

Page 24: Customizing Graphs

axis()• Use to add a customized axis or

position an axis on the top or right of the plot or in the margin of the plot

• at = specifies location of tickmarks• labels = specifies labels

Page 25: Customizing Graphs

Adding to a Graph 1• Plot sets up coordinates• grid – add gridlines• abline – add regression, vertical, or

horizontal lines (full plot window)• points – add points• lines – add lines• segment – add line segments

Page 26: Customizing Graphs

Adding to a Graph 2• text – add text to plot• legend (locator, position, coords)• arrows – draw arrows• rect – draw rectangles• polygon – draw polygons• ellipse – draw probability ellipse• matlines – draw multiple lines

Page 27: Customizing Graphs

Margins• Must be set before calling plot• oldpar <- par() # to save current• par(mar= c(bottom, left, top,

right)) if you need more space for labels, titles, etc

Page 28: Customizing Graphs

Multiple Graphs• par(mfrow=c(rows, cols)) plots in

rows• Par(mfcol=c(rows, cols)) plots in

columns