customizing graphs base graphics options. plot() the workhorse plotting function plot(x) plots...

28
Customizing Graphs Base graphics options

Post on 19-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

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

Customizing Graphs

Base graphics options

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

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces
Page 7: 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
Page 8: 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
Page 9: 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

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces
Page 12: 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
Page 13: 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

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces
Page 15: 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
Page 16: 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
Page 17: 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
Page 18: 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
Page 19: 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
Page 20: 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

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces
Page 22: 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

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

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 Base graphics options. plot() The workhorse plotting function plot(x) plots values of x in sequence or a barplot plot(x, y) produces

Multiple Graphs

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

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