create an arrow with gradient color

后端 未结 2 1873
臣服心动
臣服心动 2020-12-06 14:04

how can I create an arrow or segment with gradient color using R?

2条回答
  •  伪装坚强ぢ
    2020-12-06 14:27

    Try this,

    library(grid)
    
    png("mask.png")
    grid.polygon(c(-0.06, 0.06, 0.06, 0.15, 0, -0.15, -0.06),
                 c(-5, -5, 2.5, 2, 5, 2, 2.5), gp=gpar(fill="black"),
                 def="native",
                 vp=viewport(xs=c(-0.15, 0.15), ys=c(-5, 5)))
    dev.off()
    
    library(png)
    m <- readPNG("mask.png", native=FALSE)
    mask <- matrix(rgb(m[,,1],m[,,2],m[,,3]),
                   nrow=nrow(m))
    
    rmat <- matrix(rgb(colorRamp(c("blue","white","red"))(seq(0,1,length=nrow(m))), maxColorValue=255),
                   nrow=nrow(m), ncol=ncol(m))
    rmat[mask == "#FFFFFF"] <- NA
    grid.newpage()
    grid.raster(rmat)
    

    scrnsht

    Edit: you can reuse it in a plot, e.g.

    library(ggplot2)
    ggplot(iris) + geom_path(aes(Sepal.Length, Petal.Length, colour = Petal.Width)) +
      guides(colour = guide_colourbar()) +
      annotation_custom(rasterGrob(rmat, width=unit(1,"npc"), height=unit(1, "npc")),
                        x = 6, xmax=6.2, y=2.5, ymax=4)
    

    enter image description here

提交回复
热议问题