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 09:18] – [Plotting Multiple Datasets] hkimscil | r:graphics [2017/11/27 09:57] (current) – [Adding Confidence Intervals to a Bar Chart] hkimscil | ||
---|---|---|---|
Line 193: | Line 193: | ||
</ | </ | ||
+ | |||
+ | ===== e.g. 1 ===== | ||
+ | < | ||
+ | attach(mtcars) | ||
+ | plot(wt, mpg) | ||
+ | abline(lm(mpg~wt)) | ||
+ | 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 378: | Line 421: | ||
+ | + | ||
+ | + | ||
- | {{: | + | {{: |
====== Adding Confidence Intervals to a Bar Chart ====== | ====== Adding Confidence Intervals to a Bar Chart ====== | ||
Line 389: | 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 426: | 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 ====== | ||
- | <code>> x <- rnorm(10, | + | < |
- | > y <- 2.5*x - 1.0 + rnorm(10, | + | y <- 2.5*x - 1.0 + rnorm(10, |
- | > cor(x,y) | + | cor(x,y) |
[1] 0.7400576 | [1] 0.7400576 | ||
- | > plot(x, | + | plot(x, |
- | > x1 <- runif(8, | + | x1 <- runif(8, |
- | > y1 <- 2.5*x1 - 1.0 + runif(8, | + | y1 <- 2.5*x1 - 1.0 + runif(8, |
- | > points(x1, | + | points(x1, |
+ | </ | ||
+ | |||
+ | {{: | ||
Line 449: | Line 507: | ||
====== Creating One Box Plot for Each Factor Level ====== | ====== Creating One Box Plot for Each Factor Level ====== | ||
< | < | ||
+ | {{: | ||
< | < | ||
> UScereal | > UScereal | ||
Line 724: | Line 782: | ||
xlab=" | xlab=" | ||
</ | </ | ||
+ | {{: | ||
====== Creating a Histogram ====== | ====== Creating a Histogram ====== | ||
Line 734: | Line 793: | ||
</ | </ | ||
+ | {{: | ||
====== Creating a Normal Quantile-Quantile (Q-Q) Plot ====== | ====== Creating a Normal Quantile-Quantile (Q-Q) Plot ====== | ||
Line 745: | Line 805: | ||
qqline(log(Cars93$Price))</ | qqline(log(Cars93$Price))</ | ||
+ | {{: | ||
+ | ====== 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: | ||
+ | # | ||
+ | # A bit of mucking around is required to get the second (whole-world) | ||
+ | # map positioned correctly; | ||
+ | # plotting function to perform calculations but do no drawing (see the | ||
+ | # second call to the map() function). | ||
+ | # | ||
+ | # Makes use of the " | ||
+ | # | ||
+ | |||
+ | |||
+ | library(maps) | ||
+ | par(mar=rep(0, | ||
+ | map(" | ||
+ | points(174.75, | ||
+ | arrows(172, -36.87, 174, -36.87, lwd=3) | ||
+ | text(172, -36.87, " | ||
+ | # mini world map as guide | ||
+ | maplocs <- map(projection=" | ||
+ | | ||
+ | | ||
+ | | ||
+ | xrange <- range(maplocs$x, | ||
+ | yrange <- range(maplocs$y, | ||
+ | aspect <- abs(diff(yrange))/ | ||
+ | # customised to 6.5 by 4.5 figure size | ||
+ | par(fig=c(0.99 - 0.5, 0.99, 0.01, 0.01 + 0.5*aspect*4.5/ | ||
+ | mar=rep(0, 4), new=TRUE) | ||
+ | plot.new() | ||
+ | plot.window(xlim=xrange, | ||
+ | ylim=yrange) | ||
+ | map(projection=" | ||
+ | interior=FALSE, | ||
+ | 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.1480639702.txt.gz · Last modified: 2016/12/02 09:18 by hkimscil