User Tools

Site Tools


r:graphics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:graphics [2016/12/02 09:22] – [Plotting Multiple Datasets] hkimscilr:graphics [2017/11/27 09:57] (current) – [Adding Confidence Intervals to a Bar Chart] hkimscil
Line 193: Line 193:
  
 </code> </code>
 +
 +===== e.g. 1 =====
 +<code># Creating a Graph
 +attach(mtcars)
 +plot(wt, mpg) 
 +abline(lm(mpg~wt))
 +title("Regression of MPG on Weight")</code>
 +{{:R:regression_line.png}}
 +
 +===== e.g. 2 =====
 +
 +<code>?LifeCycleSavings
 +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      3Q     Max 
 +-8.2422 -2.6857 -0.2488  2.4280  9.7509 
 +
 +Coefficients:
 +              Estimate Std. Error t value Pr(>|t|)    
 +(Intercept) 28.5660865  7.3545161   3.884 0.000334 ***
 +pop15       -0.4611931  0.1446422  -3.189 0.002603 ** 
 +pop75       -1.6914977  1.0835989  -1.561 0.125530    
 +dpi         -0.0003369  0.0009311  -0.362 0.719173    
 +ddpi         0.4096949  0.1961971   2.088 0.042471 *  
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 3.803 on 45 degrees of freedom
 +Multiple R-squared:  0.3385, Adjusted R-squared:  0.2797 
 +F-statistic: 5.756 on 4 and 45 DF,  p-value: 0.0007904
 +
 +> plot(LifeCycleSavings)
 +> plot(lm.SR)
 +</code>
 +
 +{{:r:LifeCycleSavings.jpeg}}
 +{{:R:regression_model.png}}
  
 ====== Plotting All Variables Against All Other Variables ====== ====== Plotting All Variables Against All Other Variables ======
Line 378: Line 421:
 +         names.arg=c("May", "Jun", "Jul", "Aug", "Sep"), +         names.arg=c("May", "Jun", "Jul", "Aug", "Sep"),
 +         ylab="Temp (deg. F)")</code> +         ylab="Temp (deg. F)")</code>
-{{:R:barplot_heights.png}}+{{:R:barplot_heights.2.png}}
  
 ====== Adding Confidence Intervals to a Bar Chart ====== ====== Adding Confidence Intervals to a Bar Chart ======
Line 389: Line 432:
 </code> </code>
  
-<code>tmean <- tapply(Temp, list(as.factor(Month)), mean)  # mean for each month +<code>tmean <- tapply(Temp, list(as.factor(Month)), mean)   
-tsd <- tapply(Temp, list(as.factor(Month)),sd)  # sd for each month +# mean for each month 
-tse <- tsd/sqrt(length(Temp))  # se for each month+tsd <- tapply(Temp, list(as.factor(Month)),sd)   
 +# sd for each month 
 +tse <- tsd/sqrt(length(Temp))   
 +# se for each month
  
 lower <- tmean - 1.96*tse lower <- tmean - 1.96*tse
Line 426: Line 472:
 plot(pressure, type="l")</code> plot(pressure, type="l")</code>
 {{:R:barplot_points_line.png}} {{:R:barplot_points_line.png}}
 +
 +<code>> par(mfrow=c(2, 2), cex=0.6, mar=c(4, 4, 1, 1))
 +> y <- rnorm(20)
 +> plot(y, type="p")
 +> plot(y, type="l")
 +> plot(y, type="b")
 +> plot(y, type="h")
 +></code> 
 +{{:r:plot_types.png}}
 ====== Plotting Multiple Datasets ====== ====== Plotting Multiple Datasets ======
 <code>x <- rnorm(10,sd=5,mean=20) <code>x <- rnorm(10,sd=5,mean=20)
Line 452: Line 507:
 ====== Creating One Box Plot for Each Factor Level ====== ====== Creating One Box Plot for Each Factor Level ======
 <code>boxplot(Cars93$MPG.city~Cars93$Origin)</code> <code>boxplot(Cars93$MPG.city~Cars93$Origin)</code>
 +{{:R:boxplot.png}}
 <code>> library(MASS) <code>> library(MASS)
 > UScereal > UScereal
Line 727: Line 782:
         xlab="Shelf", ylab="Sugar (grams per portion)")         xlab="Shelf", ylab="Sugar (grams per portion)")
 </code> </code>
 +{{:R:boxplot2.png}}
  
 ====== Creating a Histogram ====== ====== Creating a Histogram ======
Line 737: Line 793:
 </code> </code>
  
 +{{:r:hist_Cars93.png}}
 ====== Creating a Normal Quantile-Quantile (Q-Q) Plot ====== ====== Creating a Normal Quantile-Quantile (Q-Q) Plot ======
  
Line 748: Line 805:
 qqline(log(Cars93$Price))</code> qqline(log(Cars93$Price))</code>
  
 +{{:R:qqplot.png}}
  
 +====== E.G. ======
 +<code>
 +
 +#
 +#  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, 2))
 +
 +# 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), c(0, 6))
 +lines(x, y1)
 +lines(x, y2)
 +points(x, y1, pch=16, cex=2)
 +points(x, y2, pch=21, bg="white", cex=2)
 +par(col="grey50", fg="grey50", col.axis="grey50")
 +axis(1, at=seq(0, 16, 4))
 +axis(2, at=seq(0, 6, 2))
 +axis(4, at=seq(0, 6, 2))
 +box(bty="u")
 +mtext("Travel Time (s)", side=1, line=2, cex=0.8)
 +mtext("Responses per Travel", side=2, line=2, las=0, cex=0.8)
 +mtext("Responses per Second", side=4, line=2, las=0, cex=0.8)
 +text(4, 5, "Bird 131")
 +par(mar=c(5.1, 4.1, 4.1, 2.1), col="black", fg="black", col.axis="black")
 +
 +# 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, 4.1, 3.1, 0))
 +hist(Y, breaks=seq(-3.5, 3.5), ylim=c(0, 0.5), 
 +     col="grey80", freq=FALSE)
 +lines(x, dnorm(x), lwd=2)
 +par(mar=c(5.1, 4.1, 4.1, 2.1))
 +
 +# Barplot
 +# Modified from example(barplot)
 +par(mar=c(2, 3.1, 2, 2.1))
 +midpts <- barplot(VADeaths, col=grey(0.5 + 1:5/12), 
 +                  names=rep("", 4))
 +mtext(sub(" ", "\n", colnames(VADeaths)),
 +      at=midpts, side=1, line=0.5, cex=0.5)
 +text(rep(midpts, each=5), apply(VADeaths, 2, cumsum) - VADeaths/2,
 +     VADeaths, col=rep(c("white", "black"), times=2:3, cex=0.8))
 +par(mar=c(5.1, 4.1, 4.1, 2.1))
 +
 +# Boxplot
 +# Modified example(boxplot) - itself from suggestion by Roger Bivand
 +par(mar=c(3, 4.1, 2, 0))
 +     boxplot(len ~ dose, data = ToothGrowth,
 +             boxwex = 0.25, at = 1:3 - 0.2,
 +             subset= supp == "VC", col="grey90",
 +             xlab="",
 +             ylab="tooth length", ylim=c(0,35))
 +     mtext("Vitamin C dose (mg)", side=1, line=2.5, cex=0.8)
 +     boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
 +             boxwex = 0.25, at = 1:3 + 0.2,
 +             subset= supp == "OJ", col="grey70")
 +     legend(1.5, 9, c("Ascorbic acid", "Orange juice"), bty="n",
 +            fill = c("grey90", "grey70"))
 +par(mar=c(5.1, 4.1, 4.1, 2.1))
 +
 +# Persp
 +# Almost exactly example(persp)
 +    x <- seq(-10, 10, length= 30)
 +     y <- x
 +     f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
 +     z <- outer(x, y, f)
 +     z[is.na(z)] <- 1
 +# 0.5 to include z axis label
 +par(mar=c(0, 0.5, 0, 0), lwd=0.1)
 +     persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "grey80")
 +par(mar=c(5.1, 4.1, 4.1, 2.1), lwd=1)
 +
 +# Piechart
 +# Example 4 from help(pie)
 +par(mar=c(0, 2, 1, 2), xpd=FALSE, cex=0.5)
 +     pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
 +     names(pie.sales) <- c("Blueberry", "Cherry",
 +         "Apple", "Boston Cream", "Other", "Vanilla")
 +     pie(pie.sales, col = gray(seq(0.4,1.0,length=6)))
 +
 +
 +
 +</code>
 +{{:R:various_graphs.png}}
 +
 +<code>#
 +# Comment:
 +#
 +# A bit of mucking around is required to get the second (whole-world)
 +# map positioned correctly;  this provides an example of calling a 
 +# plotting function to perform calculations but do no drawing (see the
 +# second call to the map() function).
 +#
 +# Makes use of the "maps" and "mapproj" packages to draw the maps.
 +#
 +
 +
 +library(maps)
 +par(mar=rep(0, 4))
 +map("nz", fill=TRUE, col="grey80")
 +points(174.75, -36.87, pch=16, cex=2)
 +arrows(172, -36.87, 174, -36.87, lwd=3)
 +text(172, -36.87, "Auckland  ", adj=1, cex=2)
 +# mini world map as guide
 +maplocs <- map(projection="sp_mercator", wrap=TRUE, lwd=0.1, 
 +               col="grey", ylim=c(-60, 75),
 +               interior=FALSE, orientation=c(90, 180, 0), add=TRUE,
 +               plot=FALSE)
 +xrange <- range(maplocs$x, na.rm=TRUE)
 +yrange <- range(maplocs$y, na.rm=TRUE)
 +aspect <- abs(diff(yrange))/abs(diff(xrange))
 +# 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/6.5), 
 +    mar=rep(0, 4), new=TRUE)
 +plot.new()
 +plot.window(xlim=xrange,
 +            ylim=yrange)
 +map(projection="sp_mercator", wrap=TRUE, lwd=0.1, ylim=c(-60, 75),
 +    interior=FALSE, orientation=c(90, 180, 0), add=TRUE)
 +symbols(-.13, -0.8, circles=1, inches=0.1, add=TRUE)</code>
 +{{:R:using_maps_R.png}}
 +
 +<code>par(mfrow=c(2, 2))
 +z <- 2 * volcano        # Exaggerate the relief
 +x <- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
 +y <- 10 * (1:ncol(z))   # 10 meter spacing (E to W)
 +# Don't draw the grid lines :  border = NA
 +par(mar=rep(0, 4))
 +persp(x, y, z, theta = 135, phi = 30, col = "light grey", scale = FALSE,
 +      ltheta = -120, shade = 0.75, border = NA, box = FALSE)
 +mtext("persp()", side=3, line=-2)
 +par(mar=c(3, 3, 2, 0.5))
 +# Note that  example(trees)  shows more sensible plots!
 +N <- nrow(trees)
 +attach(trees)
 +# Girth is diameter in inches
 +symbols(Height, Volume, circles=Girth/24, inches=FALSE,
 +        main="", xlab="", ylab="", bg=grey(Girth/max(Girth)))
 +mtext("symbols()", side=3, line=0.5)
 +par(mar=rep(0.5, 4))
 +contour(x, y, z, asp=1, labcex=0.35, axes=FALSE)
 +rect(0, 0, 870, 620)
 +mtext("contour()", side=3, line=-1.5)
 +image(x, y, z, asp=1, col=grey(0.5 + 1:12/24), xlab="", ylab="", axes=FALSE)
 +rect(min(x)-5, min(y)-5, max(x)+5, max(y)+5)
 +mtext("image()", side=3, line=-1.5)
 +
 +
 +</code>
 +{{:R:poly_graph.png}}
  
r/graphics.1480639921.txt.gz · Last modified: 2016/12/02 09:22 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki