※ 引述《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