グラフィックス参考実例集:凡例 legend
(グラフィックス参考実例集に戻る。Rのグラフィックスパラメータを参照する。)
関数legendで凡例を追加することができ、legend() 関数はグラフィックス中の線や点記号の解説を小さなボックス中に表示します。凡例の位置は自動計算されますが、指定の位置におくこともできます。一般的な呼びだし形式は以下の通りです。
legend(x, y, legend, ...)
最初の引数は凡例をつける位置です。座標が一つならば凡例の枠の左上隅、二つならば対角二隅の位置と見なします。legendにそれぞれの内訳を表す文字列のベクトルを指定します。残りの引数で、記号の付け方などを制御します。 棒グラフに凡例をつける場合には、引数angle、density、 fill を使います。angleとdensityにはそれぞれ、棒グラフが斜線によるハッチングで分けてある場合の角度と密度のベクトルを指定します。ハッチングに使う色はcolに指定します。棒グラフの中身を色分けして塗りつぶす場合はfillに塗りつぶしに使う色のベクトルを指定します。
棒グラフではなく線プロット、点プロットに凡例をつける場合には、引数lty、marks、pchをそれぞれ使用します。線分、絵記号、プロット文字などに使われた色はcolに指定します。lty、 marks、 pchを同時に二つ以上指定することも可能です。 引数ncolに列数を指定することにより、複数列に分かれた凡例を作ることもできます。
なお、対話的に凡例を書き込む場合は、
legend(locator(1), legend=names, ...)
などとして、locatorにより対話的に位置を決定すると便利です。
legend1 <- function () { leg.txt <- c("Setosa Petals", "Setosa Sepals", "Versicolor Petals", "Versicolor Sepals") # 凡例解説テキスト y.leg <- c(4.5, 3, 2.1, 1.4, .7) cexv <- c(1.2, 1, 4/5, 2/3, 1/2) matplot(c(1,8), c(0,4.5), type = "n", xlab = "Length", ylab = "Width", main = "Petal and Sepal Dimensions in Iris Blossoms") for (i in seq(cexv)) { text (1, y.leg[i]-.1, paste("cex=",formatC(cexv[i])), cex=.8, adj = 0) legend(3, y.leg[i], leg.txt, pch = "sSvV", col = c(1, 3), cex = cexv[i]) } # sSvV はプロットシンボル(この順に4つの解説テキストと対応) }
文字列がはみ出るのを制御するには文字列の最後に空白を適当な数加える
leg.txt <- c("Setosa Petals ", "Setosa Sepals ", "Versicolor Petals ", "Versicolor Sepals" )
legend2 <- function () { x <- seq(-pi, pi, len = 65) plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2) points(x, cos(x), pch = 3, col = 4) lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6) title("legend(..., lty = c(2, -1, 1), pch = c(-1,3,4), merge = TRUE)", cex.main = 1.1) legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3,4,6), lty = c(2, -1, 1), pch = c(-1, 3, 4), merge = TRUE, bg='gray90') # 最初の -1, 1.9 は凡例ボックスの左上隅座標、次は説明文字列、col は凡例記号の色 # lty はプロット中で使われた線の種類番号(値 -1 は欠如を示す) # pch はプロットシンボル番号(値 -1 は欠如を示す) # lty, pch 双方で指定された時はオプション merge=TRUE で合成されて表示される # bg は背景色の指定 }
legend3 <- function () { ## right-justifying a set of labels: thanks to Uwe Ligges x <- 1:5; y1 <- 1/x; y2 <- 2/x plot(rep(x, 2), c(y1, y2), type="n", xlab="x", ylab="y") lines(x, y1); lines(x, y2, lty=2) temp <- legend(5, 2, legend = c(" ", " "), text.width = strwidth("1,000,000"), lty = 1:2, xjust = 1, yjust = 1) text(temp$rect$left + temp$rect$w, temp$text$y, c("1,000", "1,000,000"), pos=2) }
legend4 <- function () { ##--- log scaled Examples ------------------------------ leg.txt <- c("a one", "a two") par(mfrow = c(2,2)) for(ll in c("","x","y","xy")) { plot(2:10, log=ll, main=paste("log = '",ll,"'", sep="")) abline(1,1) lines(2:3,3:4, col=2) # points(2,2, col=3) # rect(2,3,3,2, col=4) text(c(3,3),2:3, c("rect(2,3,3,2, col=4)", "text(c(3,3),2:3,\"c(rect(...)\")"), adj = c(0,.3)) legend(list(x=2,y=8), legend = leg.txt, col=2:3, pch=1:2, lty=1, merge=TRUE)#, trace=TRUE) } par(mfrow=c(1,1)) }
legend5 <- function () { plot(x, sin(x), type="l", col = 2,xlab=expression(phi),ylab=expression(f(phi))) abline(h=-1:1, v=pi/2*(-6:6), col="gray90") lines(x, cos(x), col = 3, lty = 2) ex.cs1 <- expression(plain(sin) * phi, paste("cos", phi))# 2 ways str(legend(-3, .9, ex.cs1, lty=1:2, plot=FALSE, adj = c(0, .6)))# adj y ! legend(-3, .9, ex.cs1, lty=1:2, col=2:3, adj = c(0, .6)) x <- rexp(100, rate = .5) hist(x, main = "Mean and Median of a Skewed Distribution") abline(v = mean(x), col=2, lty=2, lwd=2) abline(v = median(x), col=3, lty=3, lwd=2) ex12 <- expression(bar(x) == sum(over(x[i], n), i==1, n), hat(x) == median(x[i], i==1,n)) str(legend(4.1, 30, ex12, col = 2:3, lty=2:3, lwd=2)) }
ablineは切片、傾きを指定して、直線を描く関数です。使い方は、以下の通りです。
ablineを使って、図に格子を入れることができます。
plot(1:10) abline(a=0, b=1) # 切片0 傾き1の直線 abline(h=1:10, v=1:10, lty=2) # 格子を描く
legend7 <- function () { ## `Filled' boxes -- for more, see example(plotfactor) data(PlantGrowth) plot(cut(weight, 3) ~ group, data = PlantGrowth, col = NULL, density = 16*(1:3)) }
legend8 <- function () { x <- 0:64/64 matplot(x, outer(x, 1:7, function(x, k) sin(k * pi * x)), type = "o", col = 1:7, ylim = c(-1, 1.5), pch = "*") op <- par(bg="antiquewhite1") legend(0, 1.5, paste("sin(",1:7,"pi * x)"), col=1:7, lty=1:7, pch = "*", ncol = 4, cex=.8) legend(.8,1.2, paste("sin(",1:7,"pi * x)"), col=1:7, lty=1:7, pch = "*",cex=.8) legend(0, -.1, paste("sin(",1:4,"pi * x)"), col=1:4, lty=1:4, ncol=2, cex=.8) legend(0, -.4, paste("sin(",5:7,"pi * x)"), col=5:7, pch=24, ncol=2, cex=1.5, pt.bg="pink") par(op) }
legend9 <- function () { ## point covering line : y <- sin(3*pi*x) plot(x,y,type="l",col="blue", main = "points with bg & legend(*, pt.bg)") points(x,y,pch=21,bg="white") legend(.4,1,"sin(c x)",pch=21,pt.bg="white",lty=1, col = "blue") }
legend10 <- function(){ par(mar=c(3,3,3,6)) #後で凡例用に右側をあける x <- seq(-pi, pi, len = 65) plot(x, sin(x),ylab="",xlab="",ylim=c(-1,+1), type="l", col=3, lty=2) par(new=TRUE) plot(x, cos(x),ylab="",xlab="",ylim=c(-1,+1), pch=3, col = 4) par(new=TRUE) plot(x, tan(x),ylab="",xlab="",ylim=c(-1,+1), type="b", lty=1, pch=4, col=6) par(xpd=TRUE) # 図外に描画許可 # par()$usrの2,4で右上の座標取得 legend(par()$usr[2], par()$usr[4], c("sin", "cos", "tan"), col = c(3,4,6), lty = c(2, -1, 1), pch = c(-1, 3, 4), merge = TRUE, bg='gray90') }