Re: [問題] 如何標注多條geom_abline線的名稱

作者: celestialgod (天)   2015-05-14 10:41:43
※ 引述《ntme (one shot one kill)》之銘言:
: [問題類型]:
: 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
: [軟體熟悉度]:
: 入門(寫過其他程式,只是對語法不熟悉)
: [問題敘述]:
: 在ggplot2套件裡面用geom_abline以不同的斜率跟截距畫出了兩條不同的線
: 一條是紅線,一條是黑線 如圖:http://i.imgur.com/vK2Yt5j.png
: 現在想在畫面右邊提供線的訊息,像是紅線是Line 1,黑線是Line2
: 但不熟ggplot套件,不知有無方面的指令可用呢?
: [程式範例]:
: library(ggplot2)
: data(iris)
: iris.plot <- ggplot(data=iris, aes(x = Sepal.Length,
: y = Sepal.Width,
: color = Species)) + geom_point(size=3)
: iris.plot + geom_abline(intercept = -0.2, slope = 0.6, colour="red") +
: geom_abline(intercept = -0.3, slope = 0.7)
lattice 畫法
library(lattice)
library(latticeExtra)
library(grid)
xyplot(Sepal.Width ~ Sepal.Length, groups = Species, data = iris, cex = 1.5,
key = list(space = "top", columns = 3, text = list(levels(iris$Species)),
points = list(cex = 1.5, pch=16,
col=ggplot2like(n = 3)$superpose.symbol$col)),
par.settings = ggplot2like(n = 3),
lattice.options = ggplot2like.opts()) +
layer(panel.abline(a = -0.2,b = 0.7, col=1, lwd=2)) +
layer(panel.abline(a = -0.3,b = 0.6, col=2, lwd=2))
draw.key(list(columns = 2, text = list(paste("Line", 1:2)),
lines = list(lwd=2,col=1:2)), draw = TRUE, vp = viewport(.5, .93))
preview: http://i.imgur.com/rhFrfbC.png?1
好讀的程式碼:http://pastebin.com/1eBturzh
作者: obarisk (OSWALT)   2015-05-14 18:25:00
lattice把grid 從depend修掉了?
作者: celestialgod (天)   2015-05-14 20:04:00
這我用lattice寫的XDDDD
作者: obarisk (OSWALT)   2015-05-16 06:50:00
我只是疑惑為什麼要library(grid)果然用import
作者: celestialgod (天)   2015-05-16 14:37:00
喔喔,我誤會了

Links booklink

Contact Us: admin [ a t ] ucptt.com