Skip to content
Prev 377351 / 398502 Next

Line with linearly changing thickness – installation issues

Dear David and Ferri,

Here's a simple implementation using polygon() (as David suggested). It's much less sophisticated than Paul Murrell's -- in particular, the ends of the line are simply vertical (but, with a bit more work, that too could be addressed) -- and uses standard R graphics rather than grid.

tline <- function(x, y, thickness, col="black", unit=0.005){
    # line of varying thickness
    #   x: vector of x coordinates
    #   y: vector of y coordinates
    #   thickness: units of thickness at each set of coordinates
    #   col: line colour
    #   unit: unit of thickness as fraction of vertical axis
    tl <- function(x1, x2, y1, y2, start, end){
        polygon(x=c(x1, x1, x2, x2, x1), 
                y=c(y1 - start*units, y1 + start*units, 
                    y2 + end*units, y2 - end*units, 
                    y1 - start*units),
                col=col, border=col)
    }
    if (length(x) != length(y)) "x and y are of different lengths"
    if (length(x) != length(thickness)) 
        "length of thickness is different from x and y"
    if (length(x) < 2) "x and y are too short"
    usr <- par("usr")
    units <- (usr[4] - usr[3])*unit/2
    for (i in 2:length(x)){
        tl(x[i - 1], x[i], y[i - 1], y[i], thickness[i - 1], thickness[i])
    }
}

# example:

plot(c(-1, 1), c(0, 100), type="n")
tline(seq(-1, 1, by=0.1), y=(seq(1, 9.5, length=21))^2, 
      thickness=seq(1, 20, length=21), col="blue")

I hope this helps,
 John

--------------------------------------
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: socialsciences.mcmaster.ca/jfox/
Message-ID: <ACD1644AA6C67E4FBD0C350625508EC836933505@FHSDB2D11-2.csu.mcmaster.ca>
In-Reply-To: <15467_1541957002_wABHNKVm024865_603ecaaf-9711-7238-ba41-4510bbb37b85@comcast.net>