R中系统发育图上的五彩线

时间:2013-06-19 19:54:02

标签: r graphics line phylogeny

在R中我想制作一些图表,其中我使用多色线条,如下面的示例所示。也许我可以使用彼此相邻的不同线条来做到这一点,但问题是很难使用正确的线宽,以便它们彼此紧挨着放置而中间没有任何空白区域(因为lwd参数) R图形包中的行不在绝对坐标中)。是否有任何其他方式可以指定我想用两种或三种(或更多种)不同颜色绘制一条线? (理想情况下,角落和线条看起来应该看起来不错)

欢呼声, 汤姆

PS我正在研究的应用程序是能够绘制具有多态的系统发育,如下图所示

enter image description here

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

根据我在par的帮助下收集的内容,lwd参数因设备而异。对于x11,它指出“由par(lwd =)控制的线宽是1/96英寸的倍数”。基于定义的lwd,我需要将此宽度转换为图形的x和y单位,以便正确地偏移以下行。

所以现在我的线条能够转弯 - 仍然需要对线条进行一些调整,以便以相同的长度将它们全部结束(例如,从系列中的最后一个值中减去偏移量)。

实施例

x <- c(1:10, rep(1, 10))
y <- c(rep(1, 10), 1:10)
lwd <- 20

x11() #lwd is multiples of 1/96 inches (from help info)

plot(y ~ x, t="l", lend=2, ljoin=2, lwd=lwd, col=3, xlim=c(0,11), ylim=c(0,11))

x.units.per.inch <- (par("usr")[2] - par("usr")[1]) / par("pin")[1]
y.units.per.inch <- (par("usr")[4] - par("usr")[3]) / par("pin")[2]

x.offset <- x.units.per.inch * 1/96 * lwd
y.offset <- y.units.per.inch * 1/96 * lwd

lines(x + x.offset, y + y.offset, lend=2, ljoin=2, lwd=lwd, col=2)
lines(x - x.offset, y - y.offset, lend=2, ljoin=2, lwd=lwd, col=4)

enter image description here