作者celestialgod (攸藍)
看板R_Language
標題Re: [問題] 如何標注多條geom_abline線的名稱
時間Thu May 14 10:41:43 2015
※ 引述《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
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.205.27.107
※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1431571305.A.444.html
※ 編輯: celestialgod (123.205.27.107), 05/14/2015 10:52:32
→ obarisk: lattice把grid 從depend修掉了? 05/14 18:25
→ celestialgod: 這我用lattice寫的XDDDD 05/14 20:04
推 obarisk: 我只是疑惑為什麼要library(grid) 05/16 06:50
→ obarisk: 果然用import 05/16 06:52
→ celestialgod: 喔喔,我誤會了 05/16 14:37