在R基础图中绘制透明线

时间:2017-04-16 23:51:21

标签: r plot

一些例子:

scan_track <- function(x, y, time=seq_along(x), col=1, alpha=0.5, cex=0.5, ...)
{
  layout(rbind(c(1,2), c(1,3)))
  op <- par(mar = c(0,4,0,0), oma = c(4,0,4,4), xpd = NA)

  #scales::alpha prevents lines from drawing. i.e. ignores type='o', maybe a bug
  #plot(x,y,asp=1, type="o", pch=19, col=scales::alpha(col,alpha), cex=cex, ...)
  #lines(x,y)
  plot(x, y, asp=1, type=type, pch=pch, cex=cex, col=col, ...)
  points(x[1], y[1], bg="green", pch=21)
  points(x[length(x)], y[length(x)], bg="red", pch=23)
  legend("topright",c("start","end"),pt.bg=c("green","red"),pch=c(21,23),bty='n',bg = "transparent")

  plot(time,x, type="o", pch=19, col=alpha(col,alpha), xaxt="n", xlab="", cex=cex, ...)
  #lines(time,x)
  plot(time,y, type="o", pch=19, col=alpha(col,alpha), cex=cex, ...)
  #lines(time,y)

  par(op,mfrow=c(1,1))
}

x11()
scan_track(rnorm(100),rnorm(100),col=1:100)

运行上面的代码,然后调整x11窗口的大小,然后线条消失。它们不会再出现,除非你产生另一个x11设备。

这是一个错误吗?

1 个答案:

答案 0 :(得分:1)

您必须使用col=alpha(rgb(0,0,0), 0.5)

plot(rnorm(100),rnorm(100),col=alpha(rgb(0,0,0), 0.5),type='o',cex=.5)

enter image description here