280 likes | 337 Views
Learn how to customize graphs in R using base graphics options. Understand how to plot values, change plot types, control symbols, lines, colors, and fonts. Explore color palettes, use different symbol and line styles, and add labels and axes to enhance your graphs. Discover techniques for adding elements like gridlines, legends, rectangles, and multiple plots on one canvas.
E N D
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 • Many statistical functions produce results that can be plotted using plot
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
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
Symbol/Line Styles • pch = (plot character) specifies the symbol set • cex = symbol size • lty = specifies the line type • lwd = specifies the line thickness
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
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)
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
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
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)
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
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
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
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
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
Multiple Graphs • par(mfrow=c(rows, cols)) plots in rows • Par(mfcol=c(rows, cols)) plots in columns