200 likes | 361 Vues
6601 Project---Graphics. --Enhancing Plots --Fine Control of Graphics Xingyan Bai, Qian Zhang, Ying Li. Enhancing Plots. Multiple figures on one plot Adding information Using Color Identifying points interactively Adding further axes and grids Adding legends Mathematics in labels.
E N D
6601 Project---Graphics --Enhancing Plots --Fine Control of Graphics Xingyan Bai, Qian Zhang, Ying Li
Enhancing Plots • Multiple figures on one plot • Adding information • Using Color • Identifying points interactively • Adding further axes and grids • Adding legends • Mathematics in labels
Multiple figures on one plot • par(mfrow = c(…)) par(mfcol = c(…)) • split.screen(figs, screen, …) screen(n = , …) erase.screen(n = ) close.screen(n, …) • subplot(fun, …) in S-plus
Multiple figures on one plot Example Code: split.screen(figs=c(3,2)) [1] 1 2 3 4 5 6 hist(t.stars, nclass=20, prob=TRUE) screen(3) hist(t.stars, nclass=10, prob=TRUE) screen(6) hist(t.stars, nclass=30, prob=TRUE)
Multiple figures on one plot Example Code: par(mfrow=c(2,3)) hist(t.stars, nclass=20, prob=TRUE) hist(t.stars, nclass=10, prob=TRUE) hist(t.stars, nclass=30, prob=TRUE) hist(t.stars, nclass=5, prob=TRUE) hist(t.stars, nclass=15, prob=TRUE) hist(t.stars, nclass=50, prob=TRUE)
Using Color Code par(mfrow=c(2,1)) hist(t.stars, nclass=20, prob=TRUE, col='gray', border='white') hist(t.stars, nclass=10, prob=TRUE, col='green', border='red')
Identifying points interactively Code par(mfrow=c(1,1)) plot(data$x, data$y) identify(data$x,data$y) [1] 3 26 38 56 83 87 90
Identifying points using locator() Code locator(5,type='l') $x [1] -99.78800 -25.17453 51.60166 124.05243 182.44558 $y [1] -49.04550 40.17314 111.34755 138.41388 164.47775
Adding further axes and grids Code > library(MASS) > attach(wtloss) > oldpar <- par() > par(mar = c(5.1, 4.1, 4.1, 4.1)) > plot(Days, Weight, type = "p", ylab = "Weight (kg)") > Wt.lbs <- pretty(range(Weight*2.205)) > axis(side = 4, at = Wt.lbs/2.205, lab = Wt.lbs, srt = 90) > mtext("Weight (lb)", side = 4, line = 3) > detach() > par(oldpar)
Adding legends Code plot(ecdf(y), main='Empirical Distribution of Y') curve(pexp(x, 1/mean(y)), add=TRUE, col='red') legend(200, 0.6, c('EDF','Fitted model (Exponential)'), + lty=c(1,1), col=c('black','red'))
Fine Control of Graphics • Par Command • Figure region and layout parameters • Common axes for figures
Par Command • Par ( name = value) set value to name • Par (name) return the value of name • Par () return a list of all the graphics parameters
Figure region and layout parameters • Regions: plot region, margins, figure region, device region • Layout parameters: control size and position of figure and plot regions mar()/mai(): for margin region, fig(): for figure as a fraction of device region, plt(): for plot as a fraction of figure region. • User coordinates: for positions in the plot region
Example for layout parameters Code x <- rnorm(50) y <- rnorm(x) z <- rexp(x) par(fig = c(0, 2/3, 0, 1)) plot(x, y) par(fig = c(2/3, 1, 0, 1)) plot(x, z) par("usr") [1] -2.1027427 2.1591728 -0.1081944 3.7349138
Common axes for figures • Use the same xlim or ylim • Set up the desired axis system with the first plot and then use Par to set the low-level parameter xaxs = “d” & yaxs = “d”, which (direct) specifies that the current axis should be used on subsequent plots. • Some of the more commonly used general and high-level graphics parameters see Table4.4(page 87)