= R Plot = '''`plot`''' is a built-in R function for [[R/BuiltinFunctions#Plotting_functions|plotting]]. <> ---- == Example == {{{ x <- seq(0, 2*pi, length.out=100) y <- sin(x) plot(x,y) }}} ---- == Plots == The `plot` function takes some object and creates a plot. Most generically, this function takes two numeric vectors and creates a scatterplot. === Configuration === There are many optional arguments to the `plot` function. These include: * `type="p"` to draw points. Alternatively... * `"l"` for lines connecting points * `"h"` for 'histogram like vertical lines'; this draws a line from the x-axis up to the plotted point * `"s"` for 'stair steps'; this draws lines connecting points, but as a horizontal line and a vertical line rather than a single direct line * `main="Main title"` * `sub="Subtitle"` * `xlab="x-axis"` * `ylab="x-axis"` * `pch=1` to draw points as empty circles. Alternatively... * `0` as empty squares * `5` as empty diamonds * `2` as empty upward triangles * `6` as empty downward triangles * `16` as filled circles * `15` as filled squares * `18` as filled diamond * `17` as filled upward triangles * `"*"` as asterisks * can be any single character * `col="black"` to draw points in black. Alternatively... * `"red"` in red, "`blue"` in blue, and so on, any color name that is recognized by R * run `colors()` to see the list of recognized color names * `"#000000"` in black, or any hex code * `lty=1` to draw a solid line. Alternatively... * `"solid"` * `2` or "dashed" for a dashed line * `3` or "dotted" for a dotted line * `4` or "dotdash" for a dash-dotted line === density === Kernel density estimates can be plotted using the `plot` function. Try: {{{ plot(density(x, na.rm=TRUE), main="Distribution of x") }}} === lm === [[R/BuiltinFunctions/Lm|Linear model]] estimates have several plots associated. 1. a [[Statistics/TukeyAnscombePlot|Tukey-Anscombe]] (a.k.a. residuals vs. fitted) plot 2. a [[Statistics/Residuals|residuals]] [[Statistics/QQPlot|Q-Q plot]] 3. a scale-location plot 4. a Cook's distance plot 5. a residuals vs. leverage plot 6. a Cook's distance vs leverage plot These plots are selected using the `which` option. For example: {{{ plot(lm(y ~ x), which=1) }}} === emmeans === The [[R/Emmeans|pairs]] function calculates pairwise comparisons. The confidence intervals can be plotted like: {{{ plot(pairs(emmeans(mod, spec = ~ treatment))) }}} ---- == Lines == The `abline` function bundles a regression line into a plot. Try: {{{ plot(x, y) abline(lm(y ~ x)) }}} This function takes some of the same options as above; in particular `col` and `lty`. ---- == Legends == The `legend` function bundles a legend into a plot. The first arguments (one or two) set the position. These can be coordinates (in the same units as the plot itself), or it can be a single descriptive word ("topleft", "topright", "bottomleft", or "bottomright"). For example, with two overlaid plots, try: {{{ plot(x, y1, pch=1, col="red") plot(x, y2, pch=0, col="blue") legend("topright", pch=c(1,0), col=c("red", "blue"), legend=c("y1", "y2"), text.col=c("red", "blue")) }}} Beyond that, the `legend` function takes these options: * `pch=...` for how to draw the points of the legend. If a scalar is provided, it is used for all points. * `col=...` for the color to draw the points of the legend. If a scalar is provided, it is used for all points. * `legend=...` for the text that labels the points of the legend. * `text.col=...` for the color to write the text labels. If a scalar is provided, it is used for all points. ---- CategoryRicottone