سؤال

In R I would like to make some graphs in which I use multicoloured lines as in the examples below. Perhaps I could do this using different lines placed next to each other, but the problem is that it is hard then to use the correct line width so that they are placed exactly next to each other without any white space in between (since the lwd argument of lines in the R graphics package is not in absolute coordinates). Is there perhaps any other way to specify that I would like to draw a single line with two or three (or more) different colours? (ideally corners and line cappings should look OK)

cheers, Tom

PS the application I am working on is to be able to draw phylogenies with polymorphic states as in the image below

enter image description here

enter image description here

enter image description here

enter image description here

هل كانت مفيدة؟

المحلول

From what I gather from the help of par, the lwd parameter differs from device to device. For x11 it states that "Line widths as controlled by par(lwd =) are in multiples of 1/96inch". Based on the defined lwd, I needed to convert this width to the x and y units of the graph in order to correctly offset the following lines.

So now I have your lines able to turn a corner - some adjustments to the lines are still needed in order to get them to all end at the same length (e.g. subtract the offset from the last value in the series).

Example:

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top