Farid Cheraghi
Farid Cheraghi

Reputation: 747

Draw transparent line in R base plot

Some example:

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)

run the above code and then resize the x11 windows and then the lines get vanished. They won't show up again, unless you spawn another x11 device.

Is it a bug?

Upvotes: 2

Views: 10890

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

You have to use 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

Upvotes: 4

Related Questions