r:graphics
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| r:graphics [2016/12/02 01:12] – [Creating a Normal Quantile-Quantile (Q-Q) Plot] hkimscil | r:graphics [2017/11/27 01:27] (current) – [Adding Confidence Intervals to a Bar Chart] hkimscil | ||
|---|---|---|---|
| Line 201: | Line 201: | ||
| title(" | title(" | ||
| {{: | {{: | ||
| + | |||
| + | ===== e.g. 2 ===== | ||
| + | |||
| + | < | ||
| + | lm.SR <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) | ||
| + | |||
| + | > summary(lm.SR) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = sr ~ pop15 + pop75 + dpi + ddpi, data = LifeCycleSavings) | ||
| + | |||
| + | Residuals: | ||
| + | Min 1Q Median | ||
| + | -8.2422 -2.6857 -0.2488 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) 28.5660865 | ||
| + | pop15 | ||
| + | pop75 | ||
| + | dpi | ||
| + | ddpi | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 3.803 on 45 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > plot(LifeCycleSavings) | ||
| + | > plot(lm.SR) | ||
| + | </ | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | |||
| ====== Plotting All Variables Against All Other Variables ====== | ====== Plotting All Variables Against All Other Variables ====== | ||
| < | < | ||
| Line 385: | Line 421: | ||
| + | + | ||
| + | + | ||
| - | {{: | + | {{: |
| ====== Adding Confidence Intervals to a Bar Chart ====== | ====== Adding Confidence Intervals to a Bar Chart ====== | ||
| Line 396: | Line 432: | ||
| </ | </ | ||
| - | < | + | < |
| - | tsd <- tapply(Temp, | + | # mean for each month |
| - | tse <- tsd/ | + | tsd <- tapply(Temp, |
| + | # sd for each month | ||
| + | tse <- tsd/ | ||
| + | # se for each month | ||
| lower <- tmean - 1.96*tse | lower <- tmean - 1.96*tse | ||
| Line 433: | Line 472: | ||
| plot(pressure, | plot(pressure, | ||
| {{: | {{: | ||
| + | |||
| + | < | ||
| + | > y <- rnorm(20) | ||
| + | > plot(y, type=" | ||
| + | > plot(y, type=" | ||
| + | > plot(y, type=" | ||
| + | > plot(y, type=" | ||
| + | ></ | ||
| + | {{: | ||
| ====== Plotting Multiple Datasets ====== | ====== Plotting Multiple Datasets ====== | ||
| < | < | ||
| Line 760: | Line 808: | ||
| ====== E.G. ====== | ====== E.G. ====== | ||
| + | < | ||
| + | |||
| + | # | ||
| + | # Comment: | ||
| + | # | ||
| + | # Examples of the use of standard high-level plotting functions. | ||
| + | # | ||
| + | # In each case, extra output is also added using low-level | ||
| + | # plotting functions. | ||
| + | # | ||
| + | |||
| + | |||
| + | par(mfrow=c(3, | ||
| + | |||
| + | # Scatterplot | ||
| + | x <- c(0.5, 2, 4, 8, 12, 16) | ||
| + | y1 <- c(1, 1.3, 1.9, 3.4, 3.9, 4.8) | ||
| + | y2 <- c(4, .8, .5, .45, .4, .3) | ||
| + | par(las=1, mar=c(4, 4, 2, 4)) | ||
| + | plot.new() | ||
| + | plot.window(range(x), | ||
| + | lines(x, y1) | ||
| + | lines(x, y2) | ||
| + | points(x, y1, pch=16, cex=2) | ||
| + | points(x, y2, pch=21, bg=" | ||
| + | par(col=" | ||
| + | axis(1, at=seq(0, 16, 4)) | ||
| + | axis(2, at=seq(0, 6, 2)) | ||
| + | axis(4, at=seq(0, 6, 2)) | ||
| + | box(bty=" | ||
| + | mtext(" | ||
| + | mtext(" | ||
| + | mtext(" | ||
| + | text(4, 5, "Bird 131") | ||
| + | par(mar=c(5.1, | ||
| + | |||
| + | # Histogram | ||
| + | # Random data | ||
| + | Y <- rnorm(50) | ||
| + | # Make sure no Y exceed [-3.5, 3.5] | ||
| + | Y[Y < -3.5 | Y > 3.5] <- NA | ||
| + | x <- seq(-3.5, 3.5, .1) | ||
| + | dn <- dnorm(x) | ||
| + | par(mar=c(4.5, | ||
| + | hist(Y, breaks=seq(-3.5, | ||
| + | | ||
| + | lines(x, dnorm(x), lwd=2) | ||
| + | par(mar=c(5.1, | ||
| + | |||
| + | # Barplot | ||
| + | # Modified from example(barplot) | ||
| + | par(mar=c(2, | ||
| + | midpts <- barplot(VADeaths, | ||
| + | names=rep("", | ||
| + | mtext(sub(" | ||
| + | at=midpts, side=1, line=0.5, cex=0.5) | ||
| + | text(rep(midpts, | ||
| + | | ||
| + | par(mar=c(5.1, | ||
| + | |||
| + | # Boxplot | ||
| + | # Modified example(boxplot) - itself from suggestion by Roger Bivand | ||
| + | par(mar=c(3, | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | fill = c(" | ||
| + | par(mar=c(5.1, | ||
| + | |||
| + | # Persp | ||
| + | # Almost exactly example(persp) | ||
| + | x <- seq(-10, 10, length= 30) | ||
| + | y <- x | ||
| + | f <- function(x, | ||
| + | z <- outer(x, y, f) | ||
| + | | ||
| + | # 0.5 to include z axis label | ||
| + | par(mar=c(0, | ||
| + | | ||
| + | par(mar=c(5.1, | ||
| + | |||
| + | # Piechart | ||
| + | # Example 4 from help(pie) | ||
| + | par(mar=c(0, | ||
| + | | ||
| + | | ||
| + | " | ||
| + | | ||
| + | |||
| + | |||
| + | |||
| + | </ | ||
| + | {{: | ||
| + | |||
| < | < | ||
| # Comment: | # Comment: | ||
| Line 795: | Line 944: | ||
| interior=FALSE, | interior=FALSE, | ||
| symbols(-.13, | symbols(-.13, | ||
| + | {{: | ||
| + | |||
| + | < | ||
| + | z <- 2 * volcano | ||
| + | x <- 10 * (1: | ||
| + | y <- 10 * (1: | ||
| + | # Don't draw the grid lines : border = NA | ||
| + | par(mar=rep(0, | ||
| + | persp(x, y, z, theta = 135, phi = 30, col = "light grey", scale = FALSE, | ||
| + | ltheta = -120, shade = 0.75, border = NA, box = FALSE) | ||
| + | mtext(" | ||
| + | par(mar=c(3, | ||
| + | # Note that example(trees) | ||
| + | N <- nrow(trees) | ||
| + | attach(trees) | ||
| + | # Girth is diameter in inches | ||
| + | symbols(Height, | ||
| + | main="", | ||
| + | mtext(" | ||
| + | par(mar=rep(0.5, | ||
| + | contour(x, y, z, asp=1, labcex=0.35, | ||
| + | rect(0, 0, 870, 620) | ||
| + | mtext(" | ||
| + | image(x, y, z, asp=1, col=grey(0.5 + 1:12/24), xlab="", | ||
| + | rect(min(x)-5, | ||
| + | mtext(" | ||
| + | |||
| + | |||
| + | </ | ||
| + | {{: | ||
r/graphics.1480641121.txt.gz · Last modified: by hkimscil
